Skip to content

Commit

Permalink
Add various tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jan 6, 2025
1 parent c0bb979 commit dd73738
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Attachment/AttachmentsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Countable;
use IteratorAggregate;
use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException;
use Traversable;

/**
* @template-implements \IteratorAggregate<int, Attachment>
Expand All @@ -24,7 +25,7 @@ public function __construct(Attachment ... $attachments)
$this->attachments = $attachments;
}

public function getIterator(): iterable
public function getIterator(): Traversable
{
yield from $this->attachments;
}
Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Attachment/AttachmentsCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Attachment;

use Phpro\ResourceStream\Factory\MemoryStream;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
use Soap\Psr18AttachmentsMiddleware\Attachment\AttachmentsCollection;
use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException;

final class AttachmentsCollectionTest extends TestCase
{
#[Test]
public function it_can_contain_attachments(): void
{
$collection = new AttachmentsCollection(
$attachment1 = Attachment::create('filename.pdf', MemoryStream::create()),
$attachment2 = Attachment::create('filename.jpg', MemoryStream::create()),
);

static::assertCount(2, $collection);
static::assertSame([$attachment1, $attachment2], [...$collection]);
}

#[Test]
public function it_can_add_an_item_mutably(): void
{
$collection = new AttachmentsCollection();
$collection->add($attachment = Attachment::create('filename.pdf', MemoryStream::create()));

static::assertCount(1, $collection);
static::assertSame([$attachment], [...$collection]);
}

#[Test]
public function it_can_find_an_attachment_by_id(): void
{
$collection = new AttachmentsCollection(
$attachment1 = Attachment::create('filename.pdf', MemoryStream::create()),
$attachment2 = Attachment::create('filename.jpg', MemoryStream::create()),
);

static::assertSame($attachment1, $collection->findById($attachment1->id));
}

#[Test]
public function it_can_fail_finding_an_attachment_by_id(): void
{
$collection = new AttachmentsCollection(
Attachment::create('filename.pdf', MemoryStream::create()),
);

$this->expectExceptionObject(AttachmentNotFoundException::withId('not-found'));
$collection->findById('not-found');
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Attachment/IdGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Attachment;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Soap\Psr18AttachmentsMiddleware\Attachment\IdGenerator;

final class IdGeneratorTest extends TestCase
{
#[Test]
public function it_can_generate_a_random_id(): void
{
$id1 = IdGenerator::generate();
$id2 = IdGenerator::generate();

static::assertNotSame($id1, $id2);
static::assertSame(16, mb_strlen($id1));
static::assertSame(16, mb_strlen($id2));
}
}
73 changes: 73 additions & 0 deletions tests/Unit/Encoding/Xop/XopIncludeEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Encoding\Xop;

use Phpro\ResourceStream\Factory\MemoryStream;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Soap\Encoding\Encoder\Context;
use Soap\Encoding\EncoderRegistry;
use Soap\Engine\Metadata\Collection\MethodCollection;
use Soap\Engine\Metadata\Collection\TypeCollection;
use Soap\Engine\Metadata\InMemoryMetadata;
use Soap\Engine\Metadata\Model\XsdType;
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
use Soap\Psr18AttachmentsMiddleware\Encoding\Xop\XopIncludeEncoder;
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage;
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorageInterface;
use Soap\WsdlReader\Model\Definitions\Namespaces;

final class XopIncludeEncoderTest extends TestCase
{
#[Test]
public function it_can_encode_xop_include_attachment(): void
{
$encoder = $this->createEncoder(
$storage = $this->createStorage()
);
$iso = $encoder->iso($this->createContext());

$result = $iso->to(
$attachment = new Attachment('foo', 'file.pdf', 'application/pdf', MemoryStream::create())
);

static::assertSame('<xop:Include href="cid:foo" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>', $result);
static::assertSame($attachment, $storage->requestAttachments()->findById('foo'));
}

#[Test]
public function it_can_decode_xop_include_attachment(): void
{
$encoder = $this->createEncoder(
$storage = $this->createStorage()
);
$iso = $encoder->iso($this->createContext());

$storage->responseAttachments()->add(
$attachment = new Attachment('foo', 'file.pdf', 'application/pdf', MemoryStream::create())
);
$result = $iso->from('<xop:Include href="cid:foo" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>');

static::assertSame($attachment, $result);
}

private function createStorage(): AttachmentStorageInterface
{
return new AttachmentStorage();
}

private function createEncoder(AttachmentStorageInterface $storage): XopIncludeEncoder
{
return new XopIncludeEncoder($storage);
}

private function createContext(): Context
{
return new Context(
XsdType::any(),
new InMemoryMetadata(new TypeCollection(), new MethodCollection()),
EncoderRegistry::default(),
new Namespaces([], []),
);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Exception/AttachmentNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Exception;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Soap\Engine\Exception\RuntimeException;
use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException;

final class AttachmentNotFoundExceptionTest extends TestCase
{
#[Test]
public function it_can_throw_by_id(): void
{
$exception = AttachmentNotFoundException::withId('foo');

$this->expectException(AttachmentNotFoundException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Attachment with id "foo" can not be found.');
throw $exception;
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Exception/SoapMessageNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Exception;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Soap\Engine\Exception\RuntimeException;
use Soap\Psr18AttachmentsMiddleware\Exception\SoapMessageNotFoundException;

final class SoapMessageNotFoundExceptionTest extends TestCase
{
#[Test]
public function it_can_throw_from_multipart_context(): void
{
$exception = SoapMessageNotFoundException::insideMultipart('soapmessage', 'application/soap+xml');

$this->expectException(SoapMessageNotFoundException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Soap message with id "soapmessage" and type "application/soap+xml" can not be found inside multipart response.');
throw $exception;
}

}
10 changes: 10 additions & 0 deletions tests/Unit/Middleware/AttachmentsMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Middleware;

use PHPUnit\Framework\TestCase;

final class AttachmentsMiddlewareTest extends TestCase
{

}
10 changes: 10 additions & 0 deletions tests/Unit/Multipart/RequestBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Multipart;

use PHPUnit\Framework\TestCase;

final class RequestBuilderTest extends TestCase
{

}
10 changes: 10 additions & 0 deletions tests/Unit/Multipart/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Multipart;

use PHPUnit\Framework\TestCase;

final class ResponseBuilderTest extends TestCase
{

}
62 changes: 62 additions & 0 deletions tests/Unit/Storage/AttachmentStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Storage;

use Phpro\ResourceStream\Factory\MemoryStream;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
use Soap\Psr18AttachmentsMiddleware\Attachment\AttachmentsCollection;
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage;

final class AttachmentStorageTest extends TestCase
{
#[Test]
public function it_contains_request_attachments(): void
{
$attachments = $this->createStorage()->requestAttachments();

static::assertEquals(new AttachmentsCollection(), $attachments);
}

#[Test]
public function it_contains_response_attachments(): void
{
$attachments = $this->createStorage()->responseAttachments();

static::assertEquals(new AttachmentsCollection(), $attachments);
}

#[Test]
public function it_can_reset_request_attachments(): void
{
$storage = $this->createStorage();
$attachments = $storage->requestAttachments()->add(
Attachment::create('foo.png', MemoryStream::create())
);
$storage->resetRequestAttachments();
$newAttachments = $storage->requestAttachments();

static::assertNotSame($attachments, $newAttachments);
static::assertEquals(new AttachmentsCollection(), $storage->requestAttachments());
}

#[Test]
public function it_can_reset_response_attachments(): void
{
$storage = $this->createStorage();
$attachments = $storage->responseAttachments()->add(
Attachment::create('foo.png', MemoryStream::create())
);
$storage->resetResponseAttachments();
$newAttachments = $storage->responseAttachments();

static::assertNotSame($attachments, $newAttachments);
static::assertEquals(new AttachmentsCollection(), $storage->responseAttachments());
}

private function createStorage(): AttachmentStorage
{
return new AttachmentStorage();
}
}

0 comments on commit dd73738

Please sign in to comment.