Skip to content

Commit

Permalink
Phpunit tests update (#259)
Browse files Browse the repository at this point in the history
* Remove Content-Length header from form-data du to multipart-stream-builder 1.4.0 - 2024-09-01 update

* Replace deprecated assertObjectNotHasAttribute with ReflectionObject to check for 'unmapped' property existence.

* Fixed phpunit tests for "prefer-lowest" composer settings.

---------

Co-authored-by: pavelvais <[email protected]>
  • Loading branch information
PavelVais and pavelvais authored Sep 5, 2024
1 parent a1d0924 commit ecb8647
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
7 changes: 5 additions & 2 deletions src/DigiSignClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,13 @@ private function createRequest(string $method, UriInterface $uri, array $options

$multipartBuilder = new MultipartStreamBuilder($this->streamFactory);
foreach ($options['multipart'] as $name => $resource) {
$resourceOptions = [];
$resourceOptions = [
'headers' => [],
'filename' => '',
];

if ($resource instanceof FileStream) {
$resourceOptions['filename'] = $resource->getFilename();
$resourceOptions['filename'] = $resource->getFilename() ?? '';
$resource = $resource->getHandle();
}

Expand Down
22 changes: 18 additions & 4 deletions tests/DigiSignClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,16 @@ public function testRequestWithMultipart(): void
$contentType = $lastRequest->getHeaderLine('Content-Type');
$boundary = trim(substr($contentType, 30), '"');
self::assertStringStartsWith("multipart/form-data; boundary=\"$boundary\"", $contentType);
self::assertSame(
"--$boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\nContent-Length: 3\r\n\r\nbar\r\n--$boundary--\r\n",
self::assertThat(
(string)$lastRequest->getBody(),
self::logicalOr(
self::equalTo(
"--$boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\nContent-Length: 3\r\n\r\nbar\r\n--$boundary--\r\n",
),
self::equalTo(
"--$boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n--$boundary--\r\n",
),
),
);
}

Expand All @@ -220,9 +227,16 @@ public function testRequestWithMultipartStream(): void
$contentType = $lastRequest->getHeaderLine('Content-Type');
$boundary = trim(substr($contentType, 30), '"');
self::assertStringStartsWith("multipart/form-data; boundary=\"$boundary\"", $contentType);
self::assertStringStartsWith(
"--$boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"dummy.pdf\"\r\nContent-Length: 13264\r\nContent-Type: application/pdf",
self::assertThat(
(string)$lastRequest->getBody(),
self::logicalOr(
self::stringStartsWith(
"--$boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"dummy.pdf\"\r\nContent-Length: 13264\r\nContent-Type: application/pdf",
),
self::stringStartsWith(
"--$boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"dummy.pdf\"\r\nContent-Type: application/pdf",
),
),
);
}

Expand Down
28 changes: 20 additions & 8 deletions tests/Endpoint/FilesEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ public function testUpload(): void
$contentType = $lastRequest->getHeaderLine('Content-Type');
$boundary = trim(substr($contentType, 30), '"');
self::assertSame("multipart/form-data; boundary=\"$boundary\"", $contentType);
self::assertSame(
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"file\"; filename=\"dummy.pdf\"\r\n" .
"Content-Length: " . $file->getSize() . "\r\n" .
"Content-Type: application/pdf\r\n" .
"\r\n" .
file_get_contents(TESTS_DIR . '/dummy.pdf') . "\r\n" .
"--$boundary--\r\n",
self::assertThat(
(string)$lastRequest->getBody(),
self::logicalOr(
self::equalTo(
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"file\"; filename=\"dummy.pdf\"\r\n" .
"Content-Length: " . $file->getSize() . "\r\n" .
"Content-Type: application/pdf\r\n" .
"\r\n" .
file_get_contents(TESTS_DIR . '/dummy.pdf') . "\r\n" .
"--$boundary--\r\n",
),
self::equalTo(
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"file\"; filename=\"dummy.pdf\"\r\n" .
"Content-Type: application/pdf\r\n" .
"\r\n" .
file_get_contents(TESTS_DIR . '/dummy.pdf') . "\r\n" .
"--$boundary--\r\n",
),
),
);
}

Expand Down
42 changes: 29 additions & 13 deletions tests/Endpoint/ImagesEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,36 @@ public function testUpload(): void
$contentType = $lastRequest->getHeaderLine('Content-Type');
$boundary = trim(substr($contentType, 30), '"');
self::assertSame("multipart/form-data; boundary=\"$boundary\"", $contentType);
self::assertSame(
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"image\"; filename=\"dummy.png\"\r\n" .
"Content-Length: " . $image->getSize() . "\r\n" .
"Content-Type: image/png\r\n" .
"\r\n" .
file_get_contents(TESTS_DIR . '/dummy.png') . "\r\n" .
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"public\"\r\n" .
"Content-Length: 4\r\n" .
"\r\n" .
"true\r\n" .
"--$boundary--\r\n",
self::assertThat(
(string)$lastRequest->getBody(),
self::logicalOr(
self::equalTo(
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"image\"; filename=\"dummy.png\"\r\n" .
"Content-Length: " . $image->getSize() . "\r\n" .
"Content-Type: image/png\r\n" .
"\r\n" .
file_get_contents(TESTS_DIR . '/dummy.png') . "\r\n" .
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"public\"\r\n" .
"Content-Length: 4\r\n" .
"\r\n" .
"true\r\n" .
"--$boundary--\r\n",
),
self::equalTo(
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"image\"; filename=\"dummy.png\"\r\n" .
"Content-Type: image/png\r\n" .
"\r\n" .
file_get_contents(TESTS_DIR . '/dummy.png') . "\r\n" .
"--$boundary\r\n" .
"Content-Disposition: form-data; name=\"public\"\r\n" .
"\r\n" .
"true\r\n" .
"--$boundary--\r\n",
),
),
);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Resource/BaseResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Nyholm\NSA;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use ReflectionObject;

/**
* @covers \DigitalCz\DigiSign\Resource\BaseResource
Expand Down Expand Up @@ -86,7 +87,7 @@ public function testHydration(): void
self::assertSame('moo', $resource->collection[0]->string);
self::assertInstanceOf(DummyResource::class, $resource->collection[1]);
self::assertSame('baz', $resource->collection[1]->string);
self::assertObjectNotHasAttribute('unmapped', $resource);
self::assertTrue((new ReflectionObject($resource))->hasProperty('unmapped') === false);
self::assertSame('goo', $resource->unmapped);
self::assertSame('#foobar', $resource->self());
self::assertSame(DummyResource::ID, $resource->id());
Expand Down

0 comments on commit ecb8647

Please sign in to comment.