getMockBuilder(Message::class) ->disableOriginalConstructor() ->setMethods(['send', 'sendMime', 'show']) ->getMock(); $messageApi->method('send') ->willReturn(SendResponse::create(['id' => '4711', 'message' => 'Message sent'])); $this->batchMessage = $messageApi->getBatchMessage('example.com'); } public function testAddRecipient() { $this->batchMessage->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(['to' => ['"Test User" ']], $message); $counter = NSA::getProperty($this->batchMessage, 'counters'); $this->assertEquals(1, $counter['recipients']['to']); } public function testAddRecipientWithoutFirstAndLastName() { $this->batchMessage->addToRecipient('test@samples.mailgun.org'); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(['to' => ['test@samples.mailgun.org']], $message); $counter = NSA::getProperty($this->batchMessage, 'counters'); $this->assertEquals(1, $counter['recipients']['to']); } public function testRecipientVariablesOnTo() { $this->batchMessage->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(['to' => ['"Test User" ']], $message); $attributes = NSA::getProperty($this->batchMessage, 'batchRecipientAttributes'); $this->assertEquals('Test', $attributes['test@samples.mailgun.org']['first']); $this->assertEquals('User', $attributes['test@samples.mailgun.org']['last']); } public function testRecipientVariablesOnCc() { $this->batchMessage->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(['cc' => ['"Test User" ']], $message); $attributes = NSA::getProperty($this->batchMessage, 'batchRecipientAttributes'); $this->assertEquals('Test', $attributes['test@samples.mailgun.org']['first']); $this->assertEquals('User', $attributes['test@samples.mailgun.org']['last']); } public function testRecipientVariablesOnBcc() { $this->batchMessage->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(['bcc' => ['"Test User" ']], $message); $attributes = NSA::getProperty($this->batchMessage, 'batchRecipientAttributes'); $this->assertEquals('Test', $attributes['test@samples.mailgun.org']['first']); $this->assertEquals('User', $attributes['test@samples.mailgun.org']['last']); } public function testAddMultipleBatchRecipients() { for ($i = 0; $i < 100; ++$i) { $this->batchMessage->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]); } $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(100, count($message['to'])); } public function testMaximumBatchSize() { $this->batchMessage->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setSubject('This is the subject of the message!'); $this->batchMessage->setTextBody('This is the text body of the message!'); for ($i = 0; $i < 1001; ++$i) { $this->batchMessage->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]); } $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals(1, count($message['to'])); } public function testRecipientAttributeResetOnEndBatchMessage() { $this->batchMessage->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setSubject('This is the subject of the message!'); $this->batchMessage->setTextBody('This is the text body of the message!'); $this->batchMessage->finalize(); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertTrue(empty($message['to'])); $this->assertNotEmpty($message); } public function testDefaultIDInVariables() { $this->batchMessage->addToRecipient('test-to@samples.mailgun.org'); $this->batchMessage->addCcRecipient('test-cc@samples.mailgun.org'); $attributes = NSA::getProperty($this->batchMessage, 'batchRecipientAttributes'); $this->assertEquals('to_1', $attributes['test-to@samples.mailgun.org']['id']); $this->assertEquals('cc_1', $attributes['test-cc@samples.mailgun.org']['id']); } public function testGetMessageIds() { $this->batchMessage->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setSubject('This is the subject of the message!'); $this->batchMessage->setTextBody('This is the text body of the message!'); $this->batchMessage->finalize(); $this->assertEquals(['4711'], $this->batchMessage->getMessageIds()); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom() { $this->batchMessage->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setSubject('This is the subject of the message!'); $this->batchMessage->setTextBody('This is the text body of the message!'); $this->expectException(MissingRequiredParameter::class); $this->batchMessage->finalize(); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo() { $this->batchMessage->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setSubject('This is the subject of the message!'); $this->batchMessage->setTextBody('This is the text body of the message!'); $this->expectException(MissingRequiredParameter::class); $this->batchMessage->finalize(); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject() { $this->batchMessage->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setTextBody('This is the text body of the message!'); $this->expectException(MissingRequiredParameter::class); $this->batchMessage->finalize(); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml() { $this->batchMessage->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); $this->batchMessage->setSubject('This is the subject of the message!'); $this->expectException(MissingRequiredParameter::class); $this->batchMessage->finalize(); } public function testEmptyRecipientForBatchMessage(): void { $params = [ 'from' => 'BATCH ', 'to' => ['example@gmail.com'], 'subject' => 'Hey %recipient.first%', 'text' => 'If you wish to unsubscribe, click http://example.com/unsubscribe/%recipient.id%', 'recipient-variables' => [], ]; $this->batchMessage->setMessage($params); $this->batchMessage->finalize(); $message = NSA::getProperty($this->batchMessage, 'message'); $this->assertEquals([], $message['recipient-variables']); } }