Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return types to MimePart so it works on PHP 8 with the newer version of psr/http-message #42

Merged
merged 2 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"license": "MIT",
"require": {
"php": "^7.1 || ^7.2 || ^7.3 || ^7.4 || ^8.0",
"php": "^7.1 || ^7.2 || ^7.3 || ^7.4 || ^8.0 || ^8.1",
"ext-openssl": "*",
"ext-zlib": "*",
"ext-ctype": "*",
Expand All @@ -27,7 +27,7 @@
"psr/log": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
"phpunit/phpunit": "^10.3",
"symfony/var-dumper": "^4.0"
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions src/CryptoHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function calculateMIC($payload, $algo = 'sha256', $includeHeaders
$digest = base64_encode(
hash(
$digestAlgorithm,
$includeHeaders ? $payload : $payload->getBody(),
$includeHeaders ? $payload : $payload->getBodyString(),
true
)
);
Expand Down Expand Up @@ -236,7 +236,7 @@ public static function compress($data, $encoding = null)
public static function decompress($data)
{
if ($data instanceof MimePart) {
$data = $data->getBody();
$data = $data->getBodyString();
}

/** @noinspection CallableParameterUseCaseInTypeContextInspection */
Expand Down
10 changes: 5 additions & 5 deletions src/Management.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function buildMessage(MessageInterface $message, $payload)
$as2headers[$name] = implode(', ', $values);
}

$as2Message = new MimePart($as2headers, $payload->getBody());
$as2Message = new MimePart($as2headers, $payload->getBodyString());

$message->setHeaders($as2Message->getHeaderLines());

Expand All @@ -230,7 +230,7 @@ public function processMessage(MessageInterface $message, MimePart $payload)
]
);

$body = Utils::normalizeBase64($payload->getBody());
$body = Utils::normalizeBase64($payload->getBodyString());

// Force encode binary data to base64, `openssl_pkcs7_` doesn't work with binary data
if (! Utils::decodeBase64($body)) {
Expand Down Expand Up @@ -366,7 +366,7 @@ public function sendMessage(MessageInterface $message, $payload)
try {
$options = [
'headers' => $payload->getHeaders(),
'body' => $payload->getBody(),
'body' => $payload->getBodyString(),
// 'cert' => '' // TODO: partner https cert ?
];

Expand Down Expand Up @@ -452,7 +452,7 @@ public function processMdn(MessageInterface $message, $payload)
if ($part->getParsedHeader('content-type', 0, 0) === 'message/disposition-notification') {
$this->getLogger()->debug('Found MDN report for message', [$messageId]);
try {
$bodyPayload = MimePart::fromString($part->getBody());
$bodyPayload = MimePart::fromString($part->getBodyString());
if ($bodyPayload->hasHeader('disposition')) {
$mdnStatus = $bodyPayload->getParsedHeader('Disposition', 0, 1);
if ($mdnStatus === 'processed') {
Expand Down Expand Up @@ -651,7 +651,7 @@ public function sendMdn(MessageInterface $message)
$mdn = MimePart::fromString($message->getMdnPayload());

$options = [
'body' => $mdn->getBody(),
'body' => $mdn->getBodyString(),
'headers' => $mdn->getHeaders(),
];

Expand Down
25 changes: 18 additions & 7 deletions src/MimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace AS2;

use GuzzleHttp\Psr7\MessageTrait;
use Psr\Http\Message\MessageInterface as PsrMessageInterface;
use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\Utils as PsrUtils;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\MessageInterface as PsrMessageInterface;

class MimePart implements PsrMessageInterface
{
Expand Down Expand Up @@ -116,9 +118,9 @@ public static function createIfBinaryPart(self $message): ?self

$temp = new self($message->getHeaders());
foreach ($message->getParts() as $part) {
if (Utils::isBinary($part->getBody())) {
if (Utils::isBinary($part->getBodyString())) {
$hasBinary = true;
$recreatedPart = new self($part->getHeaders(), Utils::encodeBase64($part->getBody()));
$recreatedPart = new self($part->getHeaders(), Utils::encodeBase64($part->getBodyString()));
$temp->addPart($recreatedPart);
} else {
$temp->addPart($part);
Expand Down Expand Up @@ -303,9 +305,9 @@ public function getParsedHeader($header, $index = null, $param = null)
/**
* Return the currently set message body.
*
* @return string
* @return StreamInterface Returns the body as a stream.
*/
public function getBody()
public function getBody(): StreamInterface
{
$body = $this->body;
if (count($this->parts) > 0) {
Expand All @@ -321,7 +323,16 @@ public function getBody()
}
}

return $body;
return PsrUtils::streamFor($body);
}

/**
* Return the currently set message body as a string.
*
* @return string Returns the body as a string.
*/
public function getBodyString(): string {
return PsrUtils::copyToString($this->getBody());
}

/**
Expand Down Expand Up @@ -393,7 +404,7 @@ public function toString()
return $this->rawMessage;
}

return $this->getHeaderLines() . self::EOL . $this->getBody();
return $this->getHeaderLines() . self::EOL . $this->getBodyString();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function execute(ServerRequestInterface $request = null)
$origMessageId = null;
foreach ($payload->getParts() as $part) {
if ($part->getParsedHeader('content-type', 0, 0) === 'message/disposition-notification') {
$bodyPayload = MimePart::fromString($part->getBody());
$bodyPayload = MimePart::fromString($part->getBodyString());
$origMessageId = trim($bodyPayload->getParsedHeader('original-message-id', 0, 0), '<>');
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public function execute(ServerRequestInterface $request = null)
)
);
$responseHeaders = $mdn->getHeaders();
$responseBody = $mdn->getBody();
$responseBody = $mdn->getBodyString();
} else {
$this->getLogger()->debug(
sprintf(
Expand Down Expand Up @@ -195,7 +195,7 @@ public function execute(ServerRequestInterface $request = null)
// Build the mdn for the message based on processing status
$mdn = $this->manager->buildMdn($message, null, $e->getMessage());
$responseHeaders = $mdn->getHeaders();
$responseBody = $mdn->getBody();
$responseBody = $mdn->getBodyString();
} else {
$responseStatus = 500;
$responseBody = $e->getMessage();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/CryptoHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testCompress()
'compressed-data',
$payload->getParsedHeader('content-type', 0, 'smime-type')
);
$body = base64_decode($payload->getBody());
$body = base64_decode($payload->getBodyString());
self::assertEquals(0x30, ord($body[0]));
self::assertEquals(0x82, ord($body[1]));
self::assertEquals(0x02, ord($body[2]));
Expand All @@ -114,6 +114,6 @@ public function testDecompress()
$payload = CryptoHelper::decompress($payload);

self::assertEquals('Application/EDI-X12', $payload->getHeaderLine('content-type'));
self::assertEquals(2247, strlen($payload->getBody()));
self::assertEquals(2247, strlen($payload->getBodyString()));
}
}
6 changes: 3 additions & 3 deletions tests/Unit/ManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testBuildMdn()

$report = $this->management->buildMdn($message);

self::assertNull($report->getBody());
self::assertEmpty($report->getBodyString());

$message->setHeaders('disposition-notification-to: test@example.com');

Expand All @@ -135,10 +135,10 @@ public function testBuildMdn()
self::assertTrue($report->isReport());
self::assertEquals($report->getHeaderLine('as2-to'), $sender->getAs2Id());
self::assertEquals($report->getHeaderLine('as2-from'), $receiver->getAs2Id());
self::assertEquals('custom', trim($report->getPart(0)->getBody()));
self::assertEquals('custom', trim($report->getPart(0)->getBodyString()));

$headers = new MimePart(
Utils::parseHeaders($report->getPart(1)->getBody())
Utils::parseHeaders($report->getPart(1)->getBodyString())
);

self::assertEquals('<test>', $headers->getHeaderLine('Original-Message-ID'));
Expand Down
13 changes: 6 additions & 7 deletions tests/Unit/MimePartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public function testIsMultipart()

$mimePart = new MimePart(
[
'Content-Type' => 'multipart/mixed;
boundary="'.$boundary.'"',
'Content-Type' => 'multipart/mixed; boundary="'.$boundary.'"',
]
);
$mimePart->addPart('1');
Expand All @@ -106,13 +105,13 @@ public function testIsMultipart()
public function testBody(): void
{
$mimePart = MimePart::fromString("content-type:text/plain;\n\ntest");
self::assertEquals('test', $mimePart->getBody());
self::assertEquals('test', $mimePart->getBodyString());

$mimePart->setBody('test2');
self::assertEquals('test2', $mimePart->getBody());
self::assertEquals('test2', $mimePart->getBodyString());

$mimePart = MimePart::fromString("content-type:multipart/mixed;\r\n\r\ntest");
self::assertEquals('test', $mimePart->getBody());
self::assertEquals('test', $mimePart->getBodyString());

$mimePart->setBody(new MimePart([], '1'));
self::assertEquals(1, $mimePart->getCountParts());
Expand All @@ -131,15 +130,15 @@ public function testMultipart(): void
self::assertStringStartsWith('application/pkcs7-signature', $mime->getPart(1)->getHeaderLine('content-type'));
self::assertEquals('application/EDI-Consent', $mime->getPart(0)->getHeaderLine('content-type'));
self::assertEquals('binary', $mime->getPart(0)->getHeaderLine('Content-Transfer-Encoding'));
self::assertStringStartsWith('UNB+UNOA', $mime->getPart(0)->getBody());
self::assertStringStartsWith('UNB+UNOA', $mime->getPart(0)->getBodyString());
}

public function testBodyWithoutHeaders(): void
{
$res = MimePart::fromString($this->loadFixture('test.edi'));

self::assertEmpty($res->getHeaders());
self::assertStringStartsWith('UNB+UNOA', $res->getBody());
self::assertStringStartsWith('UNB+UNOA', $res->getBodyString());
}

public function testCreateIfBinaryPartNotBinary(): void
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public function testSignedMdn()
self::assertTrue($report->isReport());

$content = $report->getPart(0);
self::assertEquals("Your message was successfully received and processed.\r\n", $content->getBody());
self::assertEquals("Your message was successfully received and processed.\r\n", $content->getBodyString());
self::assertEquals('7bit', $content->getHeaderLine('Content-Transfer-Encoding'));

$disposition = $report->getPart(1);
self::assertStringContainsString('Original-Recipient: rfc822; B', $disposition->getBody());
self::assertStringContainsString('Original-Recipient: rfc822; B', $disposition->getBody());
self::assertStringContainsString('Final-Recipient: rfc822; B', $disposition->getBody());
self::assertStringContainsString('Original-Message-ID: <test>', $disposition->getBody());
self::assertStringContainsString('Original-Recipient: rfc822; B', $disposition->getBodyString());
self::assertStringContainsString('Original-Recipient: rfc822; B', $disposition->getBodyString());
self::assertStringContainsString('Final-Recipient: rfc822; B', $disposition->getBodyString());
self::assertStringContainsString('Original-Message-ID: <test>', $disposition->getBodyString());
self::assertStringContainsString('Disposition: automatic-action/MDN-sent-automatically; processed',
$disposition->getBody());
self::assertStringContainsString('oVDpnrSnpq+V99dXaarQ9HFyRUaFNsp9tdBBSmRhX4s=, sha256', $disposition->getBody());
$disposition->getBodyString());
self::assertStringContainsString('oVDpnrSnpq+V99dXaarQ9HFyRUaFNsp9tdBBSmRhX4s=, sha256', $disposition->getBodyString());
}

protected function setUp(): void
Expand Down