diff --git a/src/Attachment/AttachmentsCollection.php b/src/Attachment/AttachmentsCollection.php index a7422a6..8da9159 100644 --- a/src/Attachment/AttachmentsCollection.php +++ b/src/Attachment/AttachmentsCollection.php @@ -5,6 +5,7 @@ use Countable; use IteratorAggregate; use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException; +use Traversable; /** * @template-implements \IteratorAggregate @@ -24,7 +25,7 @@ public function __construct(Attachment ... $attachments) $this->attachments = $attachments; } - public function getIterator(): iterable + public function getIterator(): Traversable { yield from $this->attachments; } diff --git a/tests/Unit/Attachment/AttachmentsCollectionTest.php b/tests/Unit/Attachment/AttachmentsCollectionTest.php new file mode 100644 index 0000000..9770ea6 --- /dev/null +++ b/tests/Unit/Attachment/AttachmentsCollectionTest.php @@ -0,0 +1,57 @@ +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'); + } +} diff --git a/tests/Unit/Attachment/IdGeneratorTest.php b/tests/Unit/Attachment/IdGeneratorTest.php new file mode 100644 index 0000000..0c3cc27 --- /dev/null +++ b/tests/Unit/Attachment/IdGeneratorTest.php @@ -0,0 +1,21 @@ +createEncoder( + $storage = $this->createStorage() + ); + $iso = $encoder->iso($this->createContext()); + + $result = $iso->to( + $attachment = new Attachment('foo', 'file.pdf', 'application/pdf', MemoryStream::create()) + ); + + static::assertSame('', $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(''); + + 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([], []), + ); + } +} diff --git a/tests/Unit/Exception/AttachmentNotFoundExceptionTest.php b/tests/Unit/Exception/AttachmentNotFoundExceptionTest.php new file mode 100644 index 0000000..b91bd03 --- /dev/null +++ b/tests/Unit/Exception/AttachmentNotFoundExceptionTest.php @@ -0,0 +1,22 @@ +expectException(AttachmentNotFoundException::class); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Attachment with id "foo" can not be found.'); + throw $exception; + } +} diff --git a/tests/Unit/Exception/SoapMessageNotFoundExceptionTest.php b/tests/Unit/Exception/SoapMessageNotFoundExceptionTest.php new file mode 100644 index 0000000..598ad15 --- /dev/null +++ b/tests/Unit/Exception/SoapMessageNotFoundExceptionTest.php @@ -0,0 +1,23 @@ +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; + } + +} diff --git a/tests/Unit/Middleware/AttachmentsMiddlewareTest.php b/tests/Unit/Middleware/AttachmentsMiddlewareTest.php new file mode 100644 index 0000000..1550d19 --- /dev/null +++ b/tests/Unit/Middleware/AttachmentsMiddlewareTest.php @@ -0,0 +1,10 @@ +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(); + } +}