|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Encoding\Xop; |
| 4 | + |
| 5 | +use Phpro\ResourceStream\Factory\MemoryStream; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Soap\Encoding\Encoder\Context; |
| 9 | +use Soap\Encoding\EncoderRegistry; |
| 10 | +use Soap\Engine\Metadata\Collection\MethodCollection; |
| 11 | +use Soap\Engine\Metadata\Collection\TypeCollection; |
| 12 | +use Soap\Engine\Metadata\InMemoryMetadata; |
| 13 | +use Soap\Engine\Metadata\Model\XsdType; |
| 14 | +use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment; |
| 15 | +use Soap\Psr18AttachmentsMiddleware\Encoding\Xop\XopIncludeEncoder; |
| 16 | +use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage; |
| 17 | +use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorageInterface; |
| 18 | +use Soap\WsdlReader\Model\Definitions\Namespaces; |
| 19 | + |
| 20 | +final class XopIncludeEncoderTest extends TestCase |
| 21 | +{ |
| 22 | + #[Test] |
| 23 | + public function it_can_encode_xop_include_attachment(): void |
| 24 | + { |
| 25 | + $encoder = $this->createEncoder( |
| 26 | + $storage = $this->createStorage() |
| 27 | + ); |
| 28 | + $iso = $encoder->iso($this->createContext()); |
| 29 | + |
| 30 | + $result = $iso->to( |
| 31 | + $attachment = new Attachment('foo', 'file.pdf', 'application/pdf', MemoryStream::create()) |
| 32 | + ); |
| 33 | + |
| 34 | + static::assertSame('<xop:Include href="cid:foo" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>', $result); |
| 35 | + static::assertSame($attachment, $storage->requestAttachments()->findById('foo')); |
| 36 | + } |
| 37 | + |
| 38 | + #[Test] |
| 39 | + public function it_can_decode_xop_include_attachment(): void |
| 40 | + { |
| 41 | + $encoder = $this->createEncoder( |
| 42 | + $storage = $this->createStorage() |
| 43 | + ); |
| 44 | + $iso = $encoder->iso($this->createContext()); |
| 45 | + |
| 46 | + $storage->responseAttachments()->add( |
| 47 | + $attachment = new Attachment('foo', 'file.pdf', 'application/pdf', MemoryStream::create()) |
| 48 | + ); |
| 49 | + $result = $iso->from('<xop:Include href="cid:foo" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>'); |
| 50 | + |
| 51 | + static::assertSame($attachment, $result); |
| 52 | + } |
| 53 | + |
| 54 | + private function createStorage(): AttachmentStorageInterface |
| 55 | + { |
| 56 | + return new AttachmentStorage(); |
| 57 | + } |
| 58 | + |
| 59 | + private function createEncoder(AttachmentStorageInterface $storage): XopIncludeEncoder |
| 60 | + { |
| 61 | + return new XopIncludeEncoder($storage); |
| 62 | + } |
| 63 | + |
| 64 | + private function createContext(): Context |
| 65 | + { |
| 66 | + return new Context( |
| 67 | + XsdType::any(), |
| 68 | + new InMemoryMetadata(new TypeCollection(), new MethodCollection()), |
| 69 | + EncoderRegistry::default(), |
| 70 | + new Namespaces([], []), |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
0 commit comments