Skip to content

Commit dd73738

Browse files
committed
Add various tests
1 parent c0bb979 commit dd73738

10 files changed

+290
-1
lines changed

src/Attachment/AttachmentsCollection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Countable;
66
use IteratorAggregate;
77
use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException;
8+
use Traversable;
89

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

27-
public function getIterator(): iterable
28+
public function getIterator(): Traversable
2829
{
2930
yield from $this->attachments;
3031
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Attachment;
4+
5+
use Phpro\ResourceStream\Factory\MemoryStream;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
9+
use Soap\Psr18AttachmentsMiddleware\Attachment\AttachmentsCollection;
10+
use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException;
11+
12+
final class AttachmentsCollectionTest extends TestCase
13+
{
14+
#[Test]
15+
public function it_can_contain_attachments(): void
16+
{
17+
$collection = new AttachmentsCollection(
18+
$attachment1 = Attachment::create('filename.pdf', MemoryStream::create()),
19+
$attachment2 = Attachment::create('filename.jpg', MemoryStream::create()),
20+
);
21+
22+
static::assertCount(2, $collection);
23+
static::assertSame([$attachment1, $attachment2], [...$collection]);
24+
}
25+
26+
#[Test]
27+
public function it_can_add_an_item_mutably(): void
28+
{
29+
$collection = new AttachmentsCollection();
30+
$collection->add($attachment = Attachment::create('filename.pdf', MemoryStream::create()));
31+
32+
static::assertCount(1, $collection);
33+
static::assertSame([$attachment], [...$collection]);
34+
}
35+
36+
#[Test]
37+
public function it_can_find_an_attachment_by_id(): void
38+
{
39+
$collection = new AttachmentsCollection(
40+
$attachment1 = Attachment::create('filename.pdf', MemoryStream::create()),
41+
$attachment2 = Attachment::create('filename.jpg', MemoryStream::create()),
42+
);
43+
44+
static::assertSame($attachment1, $collection->findById($attachment1->id));
45+
}
46+
47+
#[Test]
48+
public function it_can_fail_finding_an_attachment_by_id(): void
49+
{
50+
$collection = new AttachmentsCollection(
51+
Attachment::create('filename.pdf', MemoryStream::create()),
52+
);
53+
54+
$this->expectExceptionObject(AttachmentNotFoundException::withId('not-found'));
55+
$collection->findById('not-found');
56+
}
57+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Attachment;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use Soap\Psr18AttachmentsMiddleware\Attachment\IdGenerator;
8+
9+
final class IdGeneratorTest extends TestCase
10+
{
11+
#[Test]
12+
public function it_can_generate_a_random_id(): void
13+
{
14+
$id1 = IdGenerator::generate();
15+
$id2 = IdGenerator::generate();
16+
17+
static::assertNotSame($id1, $id2);
18+
static::assertSame(16, mb_strlen($id1));
19+
static::assertSame(16, mb_strlen($id2));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Exception;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use Soap\Engine\Exception\RuntimeException;
8+
use Soap\Psr18AttachmentsMiddleware\Exception\AttachmentNotFoundException;
9+
10+
final class AttachmentNotFoundExceptionTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_can_throw_by_id(): void
14+
{
15+
$exception = AttachmentNotFoundException::withId('foo');
16+
17+
$this->expectException(AttachmentNotFoundException::class);
18+
$this->expectException(RuntimeException::class);
19+
$this->expectExceptionMessage('Attachment with id "foo" can not be found.');
20+
throw $exception;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Exception;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use Soap\Engine\Exception\RuntimeException;
8+
use Soap\Psr18AttachmentsMiddleware\Exception\SoapMessageNotFoundException;
9+
10+
final class SoapMessageNotFoundExceptionTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_can_throw_from_multipart_context(): void
14+
{
15+
$exception = SoapMessageNotFoundException::insideMultipart('soapmessage', 'application/soap+xml');
16+
17+
$this->expectException(SoapMessageNotFoundException::class);
18+
$this->expectException(RuntimeException::class);
19+
$this->expectExceptionMessage('Soap message with id "soapmessage" and type "application/soap+xml" can not be found inside multipart response.');
20+
throw $exception;
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Middleware;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AttachmentsMiddlewareTest extends TestCase
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Multipart;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class RequestBuilderTest extends TestCase
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Multipart;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ResponseBuilderTest extends TestCase
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SoapTest\Psr18AttachmentsMiddleware\Unit\Storage;
4+
5+
use Phpro\ResourceStream\Factory\MemoryStream;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
use Soap\Psr18AttachmentsMiddleware\Attachment\Attachment;
9+
use Soap\Psr18AttachmentsMiddleware\Attachment\AttachmentsCollection;
10+
use Soap\Psr18AttachmentsMiddleware\Storage\AttachmentStorage;
11+
12+
final class AttachmentStorageTest extends TestCase
13+
{
14+
#[Test]
15+
public function it_contains_request_attachments(): void
16+
{
17+
$attachments = $this->createStorage()->requestAttachments();
18+
19+
static::assertEquals(new AttachmentsCollection(), $attachments);
20+
}
21+
22+
#[Test]
23+
public function it_contains_response_attachments(): void
24+
{
25+
$attachments = $this->createStorage()->responseAttachments();
26+
27+
static::assertEquals(new AttachmentsCollection(), $attachments);
28+
}
29+
30+
#[Test]
31+
public function it_can_reset_request_attachments(): void
32+
{
33+
$storage = $this->createStorage();
34+
$attachments = $storage->requestAttachments()->add(
35+
Attachment::create('foo.png', MemoryStream::create())
36+
);
37+
$storage->resetRequestAttachments();
38+
$newAttachments = $storage->requestAttachments();
39+
40+
static::assertNotSame($attachments, $newAttachments);
41+
static::assertEquals(new AttachmentsCollection(), $storage->requestAttachments());
42+
}
43+
44+
#[Test]
45+
public function it_can_reset_response_attachments(): void
46+
{
47+
$storage = $this->createStorage();
48+
$attachments = $storage->responseAttachments()->add(
49+
Attachment::create('foo.png', MemoryStream::create())
50+
);
51+
$storage->resetResponseAttachments();
52+
$newAttachments = $storage->responseAttachments();
53+
54+
static::assertNotSame($attachments, $newAttachments);
55+
static::assertEquals(new AttachmentsCollection(), $storage->responseAttachments());
56+
}
57+
58+
private function createStorage(): AttachmentStorage
59+
{
60+
return new AttachmentStorage();
61+
}
62+
}

0 commit comments

Comments
 (0)