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

test: add unit tests for lib/email #735

Merged
merged 3 commits into from
Aug 12, 2020
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
2 changes: 1 addition & 1 deletion lib/mail/SubscriptionTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function getHtml()
*/
public function setSubstitutionTag($substitution_tag)
{
Assert::string($substitution_tag, 'subscription_tag');
Assert::string($substitution_tag, 'substitution_tag');

$this->substitution_tag = $substitution_tag;
}
Expand Down
61 changes: 61 additions & 0 deletions test/unit/AsmTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* This file tests Asm.
*/
namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\Asm;
use SendGrid\Mail\GroupId;
use SendGrid\Mail\GroupsToDisplay;

/**
* This file tests Asm.
*
* @package SendGrid\Tests
*/
class AsmTest extends TestCase
{
public function testConstructorWithIntGroupId()
{
$asm = new Asm(123456, [1, 2, 3, 4]);

$this->assertInstanceOf(Asm::class, $asm);
$this->assertSame(123456, $asm->getGroupId()->getGroupId());
}

public function testConstructorWithGroupIdInstance()
{
$asm = new Asm(new GroupId(123456), [1, 2, 3, 4]);

$this->assertSame(123456, $asm->getGroupId());
}

public function testSetGroupsToDisplay()
{
$asm = new Asm(123456, [1, 2, 3, 4]);
$asm->setGroupsToDisplay(new GroupsToDisplay([1, 2, 3, 4]));

$this->assertSame([1, 2, 3, 4], $asm->getGroupsToDisplay());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$groups_to_display" must be an instance of SendGrid\Mail\GroupsToDisplay or an array.
*/
public function testSetGroupToDisplayOnInvalidValue()
{
$asm = new Asm(123456, [1, 2, 3, 4]);
$asm->setGroupsToDisplay('invalid_array');
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$group_id" must be an instance of SendGrid\Mail\GroupId or an integer.
*/
public function testSetGroupIdOnInvalidGroupId()
{
$asm = new Asm(123456, [1,2,3,4]);
$asm->setGroupId('invalid_group_id');
}
}
4 changes: 2 additions & 2 deletions test/unit/AttachmentsTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
/**
* This file tests attachments.
* This file tests Attachments.
*/
namespace SendGrid\Tests\Unit;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\Attachment;

/**
* This file tests attachments.
* This file tests Attachments.
*
* @package SendGrid\Tests\Unit
*/
Expand Down
49 changes: 49 additions & 0 deletions test/unit/BatchIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This file tests BatchId.
*/
namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\BatchId;

/**
* This file tests BatchId.
*
* @package SendGrid\Tests
*/
class BatchIdTest extends TestCase
{
public function testConstructor()
{
$batchId = new BatchId('this_is_batch_id');

$this->assertInstanceOf(BatchId::class, $batchId);
$this->assertSame('this_is_batch_id', $batchId->getBatchId());
}

public function testSetBatchId()
{
$batchId = new BatchId();
$batchId->setBatchId('this_is_batch_id');

$this->assertSame('this_is_batch_id', $batchId->getBatchId());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$batch_id" must be a string.
*/
public function testSetBatchIdOnInvalidBatchId()
{
$batch_id = new BatchId();
$batch_id->setBatchId(['invalid_batch_id']);
}

public function testJsonSerialize()
{
$batchId = new BatchId();

$this->assertNull($batchId->jsonSerialize());
}
}
71 changes: 71 additions & 0 deletions test/unit/BccSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* This file tests BccSettings.
*/
namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\BccSettings;

/**
* This file tests BccSettings.
*
* @package SendGrid\Tests
*/
class BccSettingsTest extends TestCase
{
public function testConstructor()
{
$bccSettings = new BccSettings(true, '[email protected]');

$this->assertInstanceOf(BccSettings::class, $bccSettings);
$this->assertTrue($bccSettings->getEnable());
$this->assertSame('[email protected]', $bccSettings->getEmail());
}

public function testSetEmail()
{
$bccSettings = new BccSettings();
$bccSettings->setEmail('[email protected]');

$this->assertSame('[email protected]', $bccSettings->getEmail());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$email" must be a valid email address.
*/
public function testSetEmailOnInvalidEmailFormat()
{
$bccSettings = new BccSettings();
$bccSettings->setEmail('invalid_email_address');
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$email" must be a string.
*/
public function testSetEmailOnInvalidType()
{
$bccSettings = new BccSettings();
$bccSettings->setEmail(['invalid_type']);
}

public function testSetEnable()
{
$bccSettings = new BccSettings();
$bccSettings->setEnable(true);

$this->assertTrue($bccSettings->getEnable());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$enable" must be a boolean.
*/
public function testSetEnableOnInvalidType()
{
$bccSettings = new BccSettings();
$bccSettings->setEnable('invalid_bool_type');
}
}
42 changes: 42 additions & 0 deletions test/unit/BypassListManagementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* This file tests BypassListManagement.
*/
namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\BypassListManagement;

/**
* This file tests BypassListManagement.
*
* @package SendGrid\Tests
*/
class BypassListManagementTest extends TestCase
{
public function testConstructor()
{
$bypassListManagement = new BypassListManagement(true);

$this->assertInstanceOf(BypassListManagement::class, $bypassListManagement);
$this->assertTrue($bypassListManagement->getEnable());
}

public function testSetEnable()
{
$bypassListManagement = new BypassListManagement();
$bypassListManagement->setEnable(true);

$this->assertTrue($bypassListManagement->getEnable());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$enable" must be a boolean.
*/
public function testSetEnableOnInvalidType()
{
$bypassListManagement = new BypassListManagement();
$bypassListManagement->setEnable('invalid_bool_type');
}
}
50 changes: 50 additions & 0 deletions test/unit/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* This file tests Category.
*/
namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\Category;

/**
* This file tests Category.
*
* @package SendGrid\Tests
*/
class CategoryTest extends TestCase
{
public function testConstructor()
{
$category = new Category('category');

$this->assertInstanceOf(Category::class, $category);
$this->assertSame('category', $category->getCategory());
}

public function testSetCategory()
{
$category = new Category();
$category->setCategory('category');

$this->assertSame('category', $category->getCategory());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$category" must be a string.
*/
public function testSetCategoryOnInvalidType()
{
$category = new Category();
$category->setCategory(['invalid_category']);
}

public function testJsonSerialize()
{
$category = new Category();
$category->setCategory('category');

$this->assertSame('category', $category->jsonSerialize());
}
}
61 changes: 61 additions & 0 deletions test/unit/ClickTrackingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* This file tests ClickTracking.
*/
namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\ClickTracking;

/**
* This file tests ClickTracking.
*
* @package SendGrid\Tests
*/
class ClickTrackingTest extends TestCase
{
public function testConstructor()
{
$clickTracking = new ClickTracking(true, true);

$this->assertInstanceOf(ClickTracking::class, $clickTracking);
$this->assertTrue($clickTracking->getEnable());
$this->assertTrue($clickTracking->getEnableText());
}

public function testSetEnable()
{
$clickTracking = new ClickTracking();
$clickTracking->setEnable(true);

$this->assertTrue($clickTracking->getEnable());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$enable" must be a boolean.
*/
public function testSetEnableOnInvalidType()
{
$clickTracking = new ClickTracking();
$clickTracking->setEnable('invalid_bool');
}

public function testSetEnableText()
{
$clickTracking = new ClickTracking();
$clickTracking->setEnableText(true);

$this->assertTrue($clickTracking->getEnableText());
}

/**
* @expectedException \SendGrid\Mail\TypeException
* @expectedExceptionMessage "$enable_text" must be a boolean.
*/
public function testSetEnableTextOnInvalidType()
{
$clickTracking = new ClickTracking();
$clickTracking->setEnableText('invalid_bool');
}
}
Loading