mailgun-php/tests/Message/BatchMessageTest.php
David Garcia 66fd858a02
PHP 8.x support (#727)
* Set dependencies for 7.3 and 8.x

* Set GitHub Actions for PHP 7.3, 7.4 and 8.0

* Set Ubuntu version that supports PHP from 7.1 to 7.4

According to warnings raised by GitHub Actions, the alias for the latest
Ubuntu version will upgrade soon to the newer 20.04 LTS instead of 18.04.

Aiming to ensure the compatibility with previous PHP versions, it's safer
to force the Ubuntu 18.04 that has the previous PHP version as built-in
versions in the OS

https://github.com/marketplace/actions/setup-php-action#github-hosted-runners

* Fix dependencies for PHP ^7.3 and ^8.0 support

* Run test suite with PHP versions from 7.3 to 8.1

* Broken Prophecy dependency for PHPUnit on PHP 8.1

This broken dependency doesn't allow us to run PHPUnit tests and ensure it works. Keeping the ^8.0 version should allow installations for non-maintainers on newer versions too.

https://packagist.org/packages/phpspec/prophecy#1.12.2

requires
- php: ^7.2 || ~8.0, <8.1
- ...

* Use the latest Ubuntu version instead of a specific version

* Prepare Changelog for PHP 8.0
2021-01-30 07:39:30 +01:00

185 lines
8.2 KiB
PHP

<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Message;
use Mailgun\Api\Message;
use Mailgun\Message\BatchMessage;
use Mailgun\Message\Exceptions\MissingRequiredParameter;
use Mailgun\Model\Message\SendResponse;
use Mailgun\Tests\MailgunTestCase;
use Nyholm\NSA;
class BatchMessageTest extends MailgunTestCase
{
/**
* @var BatchMessage
*/
private $batchMessage;
public function setUp(): void
{
$messageApi = $this->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" <test@samples.mailgun.org>']], $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" <test@samples.mailgun.org>']], $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" <test@samples.mailgun.org>']], $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" <test@samples.mailgun.org>']], $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();
}
}