consulting support conversation advertisements policy promotional request.
" . "Option purpose programming
"; - public function setUp() : void + public function setUp(): void { parent::setUp(); // Avoid using TestMailer for this test @@ -70,7 +74,8 @@ public function setUp() : void /** * Test that the API domain configured is maintained */ - public function testApiDomain() { + public function testApiDomain(): void + { $currentValue = Config::inst()->get(Base::class, 'api_domain'); $value = "testing.example.org"; Config::modify()->set(Base::class, 'api_domain', $value); @@ -83,12 +88,12 @@ public function testApiDomain() { /** * Test that the API endpoint configured is maintained */ - public function testApiEndpoint() { - + public function testApiEndpoint(): void + { $value = 'API_ENDPOINT_EU'; Config::modify()->set(Base::class, 'api_endpoint_region', $value); $connector = MessageConnector::create(); - $domains = $connector->getClient(); + $connector->getClient(); // assert that the expected URL value is what was set on the client $this->assertEquals(constant(Base::class . "::{$value}"), $connector->getApiEndpointRegion()); @@ -96,13 +101,14 @@ public function testApiEndpoint() { $value = ''; Config::modify()->set(Base::class, 'api_endpoint_region', $value); $connector = MessageConnector::create(); - $domains = $connector->getClient(); + $connector->getClient(); // when no value is set, the default region URL is used $this->assertEquals('', $connector->getApiEndpointRegion()); } - protected function getCustomParameters($to_address, $send_in) : array { + protected function getCustomParameters($to_address, $send_in): array + { $variables = [ 'test' => 'true', 'foo' => 'bar', @@ -129,18 +135,19 @@ protected function getCustomParameters($to_address, $send_in) : array { 'headers' => $headers, 'recipient-variables' => $recipient_variables ]; - if($send_in > 0) { + if ($send_in > 0) { $customParameters['send-in'] = $send_in; } + return $customParameters; } /** * test mailer delivery only, no sync or event checking, just that we get the expected response + * @return mixed[] */ - public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = 0) + public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = 0): array { - $to_address = self::config()->get('to_address'); $to_name = self::config()->get('to_name'); $this->assertNotEmpty($to_address); @@ -158,6 +165,8 @@ public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = $email = Email::create(); + $this->assertInstanceOf(MailgunEmail::class, $email); + $email->setFrom($from); $email->setTo($to); $email->setCc(["cc@example.com" => "Cc Person"]); @@ -166,16 +175,18 @@ public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = if ($cc = self::config()->get('cc_address')) { $email->setCc($cc); } + $htmlBody = self::config()->get('test_body'); - $email->setBody( $htmlBody ); + $email->setBody($htmlBody); $customParameters = $this->getCustomParameters($to_address, $send_in); + /** @var \NSWDPC\Messaging\Mailgun\MailgunEmail $email */ $email->setCustomParameters($customParameters); // send the email, returns a message_id if delivered $response = $email->send(); - if(Config::inst()->get(Base::class, 'send_via_job') == 'no') { + if (Config::inst()->get(Base::class, 'send_via_job') == 'no') { $this->assertEquals($response, TestMessage::MSG_ID); } else { // via job @@ -186,7 +197,7 @@ public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = $this->assertEquals( "{$from_name} <{$from_address}>", - $sendData['parameters']['from'] , + $sendData['parameters']['from'], "From: mismatch" ); @@ -208,21 +219,21 @@ public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = "Bcc: mismatch" ); - foreach($customParameters['options'] as $k=>$v) { - $this->assertEquals( $sendData['parameters']["o:{$k}"], $v, "Option $k failed"); + foreach ($customParameters['options'] as $k=>$v) { + $this->assertEquals($sendData['parameters']["o:{$k}"], $v, "Option {$k} failed"); } - foreach($customParameters['variables'] as $k=>$v) { - $this->assertEquals( $sendData['parameters']["v:{$k}"], $v , "Variable $k failed"); + foreach ($customParameters['variables'] as $k=>$v) { + $this->assertEquals($sendData['parameters']["v:{$k}"], $v, "Variable {$k} failed"); } - foreach($customParameters['headers'] as $k=>$v) { - $this->assertEquals( $sendData['parameters']["h:{$k}"], $v , "Header $k failed"); + foreach ($customParameters['headers'] as $k=>$v) { + $this->assertEquals($sendData['parameters']["h:{$k}"], $v, "Header {$k} failed"); } - $this->assertEquals( json_encode($customParameters['recipient-variables']), $sendData['parameters']['recipient-variables'] ); + $this->assertEquals(json_encode($customParameters['recipient-variables']), $sendData['parameters']['recipient-variables']); - $this->assertEquals($htmlBody, $sendData['parameters']['html'] ); + $this->assertEquals($htmlBody, $sendData['parameters']['html']); return $sendData; } @@ -230,7 +241,8 @@ public function testMailerDelivery($subject = "test_mailer_delivery", $send_in = /** * Test delivery via a Job */ - public function testJobMailerDelivery() { + public function testJobMailerDelivery(): void + { Config::modify()->set(Base::class, 'send_via_job', 'yes'); // send message $subject = "test_mailer_delivery_job"; @@ -244,7 +256,8 @@ public function testJobMailerDelivery() { /** * Test delivery via a Job */ - public function testJobMailerDeliveryInFuture() { + public function testJobMailerDeliveryInFuture(): void + { Config::modify()->set(Base::class, 'send_via_job', 'yes'); // send message $subject = "test_mailer_delivery_job_future"; @@ -258,9 +271,8 @@ public function testJobMailerDeliveryInFuture() { } - protected function checkJobData(QueuedJobDescriptor $job, $subject, $send_in) { - - + protected function checkJobData(QueuedJobDescriptor $job, $subject, $send_in) + { $this->assertEquals(SendJob::class, $job->Implementation); $data = @unserialize($job->SavedJobData ?? ''); @@ -290,27 +302,26 @@ protected function checkJobData(QueuedJobDescriptor $job, $subject, $send_in) { $customParameters = $this->getCustomParameters(self::config()->get('to_address'), $send_in); - foreach($customParameters['options'] as $k=>$v) { - $this->assertEquals( $data->parameters["o:{$k}"], $v, "Option $k failed"); + foreach ($customParameters['options'] as $k=>$v) { + $this->assertEquals($data->parameters["o:{$k}"], $v, "Option {$k} failed"); } - foreach($customParameters['variables'] as $k=>$v) { - $this->assertEquals( $data->parameters["v:{$k}"], $v , "Variable $k failed"); + foreach ($customParameters['variables'] as $k=>$v) { + $this->assertEquals($data->parameters["v:{$k}"], $v, "Variable {$k} failed"); } - foreach($customParameters['headers'] as $k=>$v) { - $this->assertEquals( $data->parameters["h:{$k}"], $v , "Header $k failed"); + foreach ($customParameters['headers'] as $k=>$v) { + $this->assertEquals($data->parameters["h:{$k}"], $v, "Header {$k} failed"); } - $this->assertEquals( json_encode($customParameters['recipient-variables']), $data->parameters['recipient-variables'] ); - + $this->assertEquals(json_encode($customParameters['recipient-variables']), $data->parameters['recipient-variables']); } /** * Test always from setting */ - public function testAlwaysFrom() { - + public function testAlwaysFrom(): void + { $alwaysFromEmail = 'alwaysfrom@example.com'; Config::modify()->set(MailgunMailer::class, 'always_from', $alwaysFromEmail); @@ -352,23 +363,26 @@ public function testAlwaysFrom() { /** * test API delivery only */ - public function testAPIDelivery() + public function testAPIDelivery(): void { - Config::modify()->set(Base::class, 'send_via_job', 'no'); $connector = MessageConnector::create(); - $to = $to_address = self::config()->get('to_address'); + $to = self::config()->get('to_address'); + $to_address = $to; $to_name = self::config()->get('to_name'); if ($to_name) { $to = $to_name . ' <' . $to_address . '>'; } + $this->assertNotEmpty($to_address); - $from = $from_address = self::config()->get('from_address'); + $from = self::config()->get('from_address'); + $from_address = $from; $from_name = self::config()->get('from_name'); if ($from_name) { $from = $from_name . ' <' . $from_address . '>'; } + $this->assertNotEmpty($from_address); $subject = "test_api_delivery"; @@ -399,7 +413,7 @@ public function testAPIDelivery() $this->assertArrayHasKey('parameters', $sendData); - foreach(['o:testmode','o:tag','from','to','subject','text','html'] as $key) { + foreach (['o:testmode','o:tag','from','to','subject','text','html'] as $key) { $this->assertEquals($parameters[ $key ], $sendData['parameters'][ $key ]); } } @@ -407,8 +421,8 @@ public function testAPIDelivery() /** * Test sending with default values set */ - public function testSendWithDefaultConfiguration() { - + public function testSendWithDefaultConfiguration(): void + { $overrideTo = 'allemails@example.com'; $overrideFrom = 'allemailsfrom@example.com'; $overrideCc = 'ccallemailsto@example.com'; @@ -448,7 +462,7 @@ public function testSendWithDefaultConfiguration() { $sendData = TestMessage::getSendData(); - foreach(['domain','parameters','sentVia','client','in'] as $key) { + foreach (['domain','parameters','sentVia','client','in'] as $key) { $this->assertArrayHasKey($key, $sendData); } @@ -459,15 +473,15 @@ public function testSendWithDefaultConfiguration() { $this->assertEquals($overrideTo, $sendData['parameters']['to']); $this->assertEquals($overrideFrom, $sendData['parameters']['from']); - $this->assertContains( $overrideCc, explode(",", $sendData['parameters']['cc']) ); - $this->assertContains( "{$overrideBccName} <{$overrideBcc}>", explode(",", $sendData['parameters']['bcc']) ); - + $this->assertContains($overrideCc, explode(",", (string) $sendData['parameters']['cc'])); + $this->assertContains("{$overrideBccName} <{$overrideBcc}>", explode(",", (string) $sendData['parameters']['bcc'])); } /** * test a message with attachments */ - public function testAttachmentDelivery() { + public function testAttachmentDelivery(): void + { $to_address = self::config()->get('to_address'); $to_name = self::config()->get('to_name'); $this->assertNotEmpty($to_address); @@ -490,24 +504,25 @@ public function testAttachmentDelivery() { $email->setFrom($from); $email->setTo($to); $email->setSubject($subject); + $htmlBody = self::config()->get('test_body'); - $email->setBody( $htmlBody ); + $email->setBody($htmlBody); $files = [ "test_attachment.pdf" => 'application/pdf', "test_attachment.txt" => 'text/plain' ]; $f = 1; - foreach($files as $file => $mimetype) { + foreach ($files as $file => $mimetype) { $email->addAttachment( - dirname(__FILE__) . "/attachments/{$file}", + __DIR__ . "/attachments/{$file}", $file, $mimetype ); $f++; } - $response = $email->send(); + $email->send(); $sendData = TestMessage::getSendData(); @@ -517,22 +532,22 @@ public function testAttachmentDelivery() { $f = 1; $this->assertEquals(count($files), count($attachments)); - foreach($attachments as $attachment) { - $this->assertArrayHasKey( 'filename', $attachment ); - $this->assertArrayHasKey( 'mimetype', $attachment ); - $this->assertArrayHasKey( 'fileContent', $attachment ); - foreach($files as $file => $mimetype) { - if($file == $attachment['filename']) { + foreach ($attachments as $attachment) { + $this->assertArrayHasKey('filename', $attachment); + $this->assertArrayHasKey('mimetype', $attachment); + $this->assertArrayHasKey('fileContent', $attachment); + foreach ($files as $file => $mimetype) { + if ($file == $attachment['filename']) { $this->assertEquals($mimetype, $attachment['mimetype']); $this->assertNotEmpty($attachment['fileContent']); $this->assertEquals( - file_get_contents( dirname(__FILE__) . "/attachments/{$file}" ), + file_get_contents(__DIR__ . "/attachments/{$file}"), $attachment['fileContent'] ); } } + $f++; } } - } diff --git a/tests/TestMessage.php b/tests/TestMessage.php index 7d57bf0..eb48c47 100644 --- a/tests/TestMessage.php +++ b/tests/TestMessage.php @@ -1,4 +1,5 @@ queueAndSend($domain, $parameters, $in); break; } + // no break case 'no': default: $this->sentVia = 'direct-to-api'; @@ -88,15 +88,16 @@ protected function sendMessage(array $parameters) { /** * Set data that would be used */ - public function setSendData(array $data) { + public function setSendData(array $data) + { self::$sendData = $data; } /** * Get data that would be used */ - public static function getSendData() : array { + public static function getSendData(): array + { return self::$sendData; } - } diff --git a/tests/WebhookTest.php b/tests/WebhookTest.php index 46a0e80..2a47695 100644 --- a/tests/WebhookTest.php +++ b/tests/WebhookTest.php @@ -15,13 +15,14 @@ */ class WebhookTest extends FunctionalTest { + private string $webhook_filter_variable = 'skjhgiehg943753-"'; - private $webhook_filter_variable = 'skjhgiehg943753-"'; - private $webhook_previous_filter_variable = 'snsd875bslw['; + private string $webhook_previous_filter_variable = 'snsd875bslw['; protected $usesDatabase = true; - public function setUp() : void { + public function setUp(): void + { parent::setUp(); Config::modify()->set(Base::class, 'webhook_filter_variable', $this->webhook_filter_variable); Config::modify()->set(Base::class, 'webhook_previous_filter_variable', $this->webhook_previous_filter_variable); @@ -31,21 +32,24 @@ public function setUp() : void { /** * Get test data from disk */ - protected function getWebhookRequestData($event_type) { - return file_get_contents( dirname(__FILE__) . "/webhooks/{$event_type}.json"); + protected function getWebhookRequestData($event_type): string|false + { + return file_get_contents(__DIR__ . "/webhooks/{$event_type}.json"); } /** * Our configured endpoint for submitting POST data */ - protected function getSubmissionUrl() { + protected function getSubmissionUrl(): string + { return '_wh/submit'; } /** * Webhook Mailgun API connector */ - protected function getConnector() { + protected function getConnector() + { return Webhook::create(); } @@ -53,7 +57,8 @@ protected function getConnector() { * Set a signing key in Configuration * @param string $signing_key */ - protected function setSigningKey($signing_key) { + protected function setSigningKey($signing_key) + { Config::modify()->set(Base::class, 'webhook_signing_key', $signing_key); } @@ -63,7 +68,8 @@ protected function setSigningKey($signing_key) { * @param string $request_data * @return array */ - protected function setSignatureOnRequest($signing_key, $request_data) { + protected function setSignatureOnRequest($signing_key, $request_data) + { $decoded = json_decode($request_data, true); $connector = $this->getConnector(); $signature = $connector->sign_token($decoded['signature']); @@ -71,7 +77,8 @@ protected function setSignatureOnRequest($signing_key, $request_data) { return $decoded; } - protected function setWebhookFilterVariable($data, $value) { + protected function setWebhookFilterVariable(array $data, $value): array + { $data['event-data']['user-variables']['wfv'] = $value; return $data; } @@ -81,8 +88,8 @@ protected function setWebhookFilterVariable($data, $value) { * and one that should fail * @param string $type */ - protected function sendWebhookRequest($type) { - + protected function sendWebhookRequest($type) + { $signing_key = "TEST_SHOULD_PASS"; $this->setSigningKey($signing_key); @@ -93,6 +100,7 @@ protected function sendWebhookRequest($type) { $session = null; $data = $this->setSignatureOnRequest($signing_key, $this->getWebhookRequestData($type)); $data = $this->setWebhookFilterVariable($data, $this->webhook_filter_variable); + $cookies = null; $body = json_encode($data, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); @@ -108,7 +116,7 @@ protected function sendWebhookRequest($type) { // test if the event was saved $record = MailgunEvent::get()->filter('EventId', $event->getId())->first(); - $this->assertTrue( $record && $record->exists() , "DB Mailgun event does not exist for event {$event->getId()}"); + $this->assertTrue($record && $record->exists(), "DB Mailgun event does not exist for event {$event->getId()}"); // change the webhook config variable to the previous var $data = $this->setWebhookFilterVariable($data, $this->webhook_previous_filter_variable); @@ -130,7 +138,7 @@ protected function sendWebhookRequest($type) { ); // remove webhook variable and test - unset( $data['event-data']['user-variables']['wfv'] ); + unset($data['event-data']['user-variables']['wfv']); Config::modify()->set(Base::class, 'webhook_filter_variable', ''); Config::modify()->set(Base::class, 'webhook_previous_filter_variable', ''); @@ -143,35 +151,40 @@ protected function sendWebhookRequest($type) { $response->getStatusCode(), 'Expected failed response code 406 with incorrect signing_key but got ' . $response->getStatusCode() . "/" . $response->getStatusDescription() ); - } - public function testWebookDelivered() { + public function testWebookDelivered(): void + { $this->sendWebhookRequest("delivered"); } - public function testWebookClick() { + public function testWebookClick(): void + { $this->sendWebhookRequest("clicked"); } - public function testWebookOpened() { + public function testWebookOpened(): void + { $this->sendWebhookRequest("opened"); } - public function testWebookFailedPermanent() { + public function testWebookFailedPermanent(): void + { $this->sendWebhookRequest("failed_permanent"); } - public function testWebookFailedTemporary() { + public function testWebookFailedTemporary(): void + { $this->sendWebhookRequest("failed_temporary"); } - public function testWebookUnsubscribed() { + public function testWebookUnsubscribed(): void + { $this->sendWebhookRequest("unsubscribed"); } - public function testWebookComplained() { + public function testWebookComplained(): void + { $this->sendWebhookRequest("complained"); } - }