diff options
author | Hannes Kindströmmer <hannes@kindstrommer.se> | 2017-03-17 10:21:19 +0100 |
---|---|---|
committer | Hannes Kindströmmer <hannes@kindstrommer.se> | 2017-03-17 10:21:19 +0100 |
commit | c9f274be08039838ab9b5e518dba5fc2c9c7c1b8 (patch) | |
tree | b0a701fa2deef0e145b9df97f71893d39f0a0ea1 | |
parent | 464896df4c8e31b93121fff41a974532bdf4ba11 (diff) | |
download | ip1-php-sdk-c9f274be08039838ab9b5e518dba5fc2c9c7c1b8.zip ip1-php-sdk-c9f274be08039838ab9b5e518dba5fc2c9c7c1b8.tar.gz ip1-php-sdk-c9f274be08039838ab9b5e518dba5fc2c9c7c1b8.tar.bz2 |
Add tests
Signed-off-by: Hannes Kindströmmer <hannes@kindstrommer.se>
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | composer.lock | 30 | ||||
-rw-r--r-- | phpunit.xml | 11 | ||||
-rw-r--r-- | src/Recipient/Group.php | 8 | ||||
-rw-r--r-- | src/SMS/LoggedOutGoingSMS.php | 3 | ||||
-rw-r--r-- | tests/Core/AbstractEnviromentProvider.php | 12 | ||||
-rw-r--r-- | tests/General/ConstructorTest.php | 35 | ||||
-rw-r--r-- | tests/Recipient/ContactTest.php | 24 | ||||
-rw-r--r-- | tests/Recipient/GroupTest.php | 61 | ||||
-rw-r--r-- | tests/Recipient/ProcessedContactTest.php | 10 | ||||
-rw-r--r-- | tests/Recipient/ProcessedGroupTest.php | 33 | ||||
-rw-r--r-- | tests/SMS/LoggedOutGoingSMSTest.php | 6 | ||||
-rw-r--r-- | tests/Util/Util.php | 32 |
13 files changed, 217 insertions, 51 deletions
diff --git a/composer.json b/composer.json index da0e070..a69f6bc 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ ], "autoload": { "psr-4": { - "IP1\\RESTClient\\": "src" + "IP1\\RESTClient\\": "src", + "IP1\\RESTClient\\Test\\": "tests" } }, "require" : { diff --git a/composer.lock b/composer.lock index 6013d45..3eb3c1b 100644 --- a/composer.lock +++ b/composer.lock @@ -1723,16 +1723,16 @@ }, { "name": "phpunit/phpunit", - "version": "6.0.8", + "version": "6.0.9", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "47ee3fa1bca5c50f1d25105201eb20df777bd7b6" + "reference": "9bd36d990884d8fb3313232e0002ed4cdf79f428" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/47ee3fa1bca5c50f1d25105201eb20df777bd7b6", - "reference": "47ee3fa1bca5c50f1d25105201eb20df777bd7b6", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9bd36d990884d8fb3313232e0002ed4cdf79f428", + "reference": "9bd36d990884d8fb3313232e0002ed4cdf79f428", "shasum": "" }, "require": { @@ -1743,18 +1743,18 @@ "ext-xml": "*", "myclabs/deep-copy": "^1.3", "php": "^7.0", - "phpspec/prophecy": "^1.6.2", + "phpspec/prophecy": "^1.7", "phpunit/php-code-coverage": "^5.0", "phpunit/php-file-iterator": "^1.4", "phpunit/php-text-template": "^1.2", "phpunit/php-timer": "^1.0.6", "phpunit/phpunit-mock-objects": "^4.0", - "sebastian/comparator": "^1.2.4 || ^2.0", + "sebastian/comparator": "^2.0", "sebastian/diff": "^1.2", "sebastian/environment": "^2.0", - "sebastian/exporter": "^2.0 || ^3.0", + "sebastian/exporter": "^3.0", "sebastian/global-state": "^1.1 || ^2.0", - "sebastian/object-enumerator": "^2.0 || ^3.0", + "sebastian/object-enumerator": "^3.0.2", "sebastian/resource-operations": "^1.0", "sebastian/version": "^2.0" }, @@ -1801,7 +1801,7 @@ "testing", "xunit" ], - "time": "2017-03-02T15:24:03+00:00" + "time": "2017-03-15T13:04:13+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -2382,16 +2382,16 @@ }, { "name": "sebastian/object-reflector", - "version": "1.0.0", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/object-reflector.git", - "reference": "2201553542d60d25db9c5b2c54330df776648008" + "reference": "afd5797e7af7c9f529879ad5e8e8abe126c89dab" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/2201553542d60d25db9c5b2c54330df776648008", - "reference": "2201553542d60d25db9c5b2c54330df776648008", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/afd5797e7af7c9f529879ad5e8e8abe126c89dab", + "reference": "afd5797e7af7c9f529879ad5e8e8abe126c89dab", "shasum": "" }, "require": { @@ -2403,7 +2403,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0-dev" + "dev-master": "1.1-dev" } }, "autoload": { @@ -2423,7 +2423,7 @@ ], "description": "Allows reflection of object attributes, including inherited and non-public ones", "homepage": "https://github.com/sebastianbergmann/object-reflector/", - "time": "2017-03-12T15:10:22+00:00" + "time": "2017-03-16T14:05:21+00:00" }, { "name": "sebastian/recursion-context", diff --git a/phpunit.xml b/phpunit.xml index 2c49f33..3de797e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,16 +4,19 @@ convertWarningsToExceptions="true"> <testsuites> <testsuite name="recipient"> - <directory>tests/recipient</directory> + <directory>tests/Recipient</directory> </testsuite> <testsuite name="sms"> - <directory>tests/sms</directory> + <directory>tests/SMS</directory> </testsuite> <testsuite name="general"> - <directory>tests/general</directory> + <directory>tests/General</directory> </testsuite> <testsuite name="core"> - <directory>tests/core</directory> + <directory>tests/Core</directory> + </testsuite> + <testsuite name="apicalls"> + <directory>tests/APICalls</directory> </testsuite> </testsuites> </phpunit> diff --git a/src/Recipient/Group.php b/src/Recipient/Group.php index d074398..3cb3f3e 100644 --- a/src/Recipient/Group.php +++ b/src/Recipient/Group.php @@ -64,13 +64,13 @@ class Group implements ProcessableComponent /** * Sets the Group's name to the given string. * @param string $name Name that the Group should have. - * @throws InvalidArgumentException When $name is empty. + * @throws \InvalidArgumentException When $name is empty. * @return void */ public function setName(string $name):void { if (empty($name)) { - throw new InvalidArgumentException("Group name can not be empty"); + throw new \InvalidArgumentException("Group name can not be empty"); } $this->name = $name; } @@ -79,12 +79,12 @@ class Group implements ProcessableComponent * @param string $color A hexadecimal color code. * @example #5E5E5E * @return void - * @throws InvalidArgumentException When $color isn't a valid hexadecimal color. + * @throws \InvalidArgumentException When $color isn't a valid hexadecimal color. */ public function setColor(string $color): void { if (!preg_match("/^#([A-Fa-f0-9]{6})$/", $color)) { - throw new InvalidArgumentException($color. " is not a valid hexadecimal color"); + throw new \InvalidArgumentException($color. " is not a valid hexadecimal color"); } $this->color = $color; } diff --git a/src/SMS/LoggedOutGoingSMS.php b/src/SMS/LoggedOutGoingSMS.php index 9fc39b1..62d255b 100644 --- a/src/SMS/LoggedOutGoingSMS.php +++ b/src/SMS/LoggedOutGoingSMS.php @@ -9,10 +9,11 @@ * @link http://api.ip1sms.com/Help * @link https://github.com/iP1SMS/ip1-php-sdk */ -use IP1\RESTClient\Core\UpdatableComponent; namespace IP1\RESTClient\SMS; +use IP1\RESTClient\Core\UpdatableComponent; + /** * The response you will get when sending an OutGoingSMS. One instance per recipient. */ diff --git a/tests/Core/AbstractEnviromentProvider.php b/tests/Core/AbstractEnviromentProvider.php index 20a4924..aa9ca15 100644 --- a/tests/Core/AbstractEnviromentProvider.php +++ b/tests/Core/AbstractEnviromentProvider.php @@ -8,9 +8,17 @@ use IP1\RESTClient\Core\Communicator; abstract class AbstractEnviromentProvider extends TestCase { protected $communicator; + private $communicatorEnabled = false; public function __construct() { - // Uses Enviroment Variables provided by Travis CI for security reasons. - $this->communicator = new Communicator(getenv("USER"), getenv("PASS")); + if (getenv("USER") || getenv("PASS")) { + // Uses Enviroment Variables provided by Travis CI for security reasons. + $this->communicator = new Communicator(getenv("USER"), getenv("PASS")); + $this->communicatorEnabled = true; + } + } + public function isCommunicatorEnabled(): bool + { + return $this->communicatorEnabled; } } diff --git a/tests/General/ConstructorTest.php b/tests/General/ConstructorTest.php index b36a42f..fba3975 100644 --- a/tests/General/ConstructorTest.php +++ b/tests/General/ConstructorTest.php @@ -24,24 +24,31 @@ use \DateTime; class ConstructorTest extends TestCase { - /** - * @covers Contact::__construct - * @covers ProcessedContact::__construct - */ + public function testRecipientConstructors() { - new Contact("Jack", "12025550161"); - new ProcessedContact("Jack", "12025550161", 13, "Sparrow", "Captain", "Black Pearl Co.", "", ""); - new Group("Crew men", "#ffffff"); - new ProcessedGroup("Crew men", "#ffffff", 12, new DateTime(), new DateTime()); - new Membership(12, 22); - new ProcessedMembership(12, 22, 43, new DateTime()); - new ProcessedMembership(1, 2, 3, new DateTime()); + $this->_require_all("src"); $this->addToAssertionCount(1); } - public function testSMSConstructors() + /** + * Scan the api path, recursively including all PHP files + * + * @param string $dir + * @param int $depth (optional) + * @author mrashad10 at github.com + * @author pwenzel at github.com + * @link https://gist.github.com/mrashad10/807456e12a6811f644ca + */ + protected function _require_all($dir, $depth = 0) { - new ProcessedOutGoingSMS("Jack", "Why is the rum gone?", "12025550109", 1, new DateTime(), new DateTime(), 22); - $this->addToAssertionCount(1); + // require all php files + $scan = glob("$dir" . DIRECTORY_SEPARATOR . "*"); + foreach ($scan as $path) { + if (preg_match('/\.php$/', $path)) { + require_once $path; + } elseif (is_dir($path)) { + $this->_require_all($path, $depth+1); + } + } } } diff --git a/tests/Recipient/ContactTest.php b/tests/Recipient/ContactTest.php index fdab721..bfa1232 100644 --- a/tests/Recipient/ContactTest.php +++ b/tests/Recipient/ContactTest.php @@ -9,12 +9,13 @@ * @link http://api.ip1sms.com/Help * @link https://github.com/iP1SMS/ip1-php-sdk */ - namespace IP1\RESTClient\Test\Recipient; +//require_once('tests/Core/AbstractEnviromentProvider.php'); use IP1\RESTClient\Recipient\Contact; use IP1\RESTClient\Recipient\RecipientFactory; use IP1\RESTClient\Recipient\ProcessedContact; +use PHPUnit\Framework\TestCase; use IP1\RESTClient\Test\Core\AbstractEnviromentProvider; /** @@ -40,8 +41,14 @@ class ContactTest extends AbstractEnviromentProvider $this->incompleteContactStd = json_decode($this->incompleteContactString); $this->minimalContactStd = json_decode($this->minimalContactString); } + /** + * @group api + */ public function testAPI() { + if (!$this->isCommunicatorEnabled()) { + $this->markTestSkipped("Communicator is not enabled skipping test"); + } $contact = RecipientFactory::createContactFromJSON($this->completeContactString); $newContact = $this->communicator->add($contact); $this->assertEquals(ProcessedContact::class, get_class($newContact)); @@ -79,6 +86,21 @@ class ContactTest extends AbstractEnviromentProvider $this->assertEquals($newContact->getTitle(), $deletedContact->getTitle()); $this->assertEquals($newContact->getID(), $deletedContact->getID()); } + /** + * @group api + */ + public function testIsContactBookEmpty() + { + if (!$this->isCommunicatorEnabled()) { + $this->markTestSkipped("Communicator is not enabled skipping test"); + } + $contacts = RecipientFactory::createProcessedContactFromStdClassArray( + json_decode( + $this->communicator->get("api/contacts") + ) + ); + $this->assertEquals([], $contacts->getArrayCopy); + } public function testCreateFromStdClass() { $contact = RecipientFactory::createContactFromStdClass($this->completeContactStd); diff --git a/tests/Recipient/GroupTest.php b/tests/Recipient/GroupTest.php new file mode 100644 index 0000000..a305125 --- /dev/null +++ b/tests/Recipient/GroupTest.php @@ -0,0 +1,61 @@ +<?php +namespace IP1\RESTClient\Test\Recipient; + +use PHPUnit\Framework\TestCase; +use IP1\RESTClient\Recipient\Group; + +class GroupTest extends TestCase +{ + /** + * @dataProvider getValidGroupInputs + */ + public function testValidInputs($name, $color) + { + $group = new Group($name, $color); + $this->addToAssertionCount(1); + } + /** + * @dataProvider getInValidGroupInputs + */ + public function testInvalidInputs($name, $color) + { + $this->expectException(\InvalidArgumentException::class); + $group = new Group($name, $color); + } + /** + * @dataProvider getValidGroupInputs + */ + public function testGettersAndSetters($name, $color) + { + $group = new Group("Jack", "#ffddff"); + $group->setName($name); + $group->setColor($color); + + $this->assertEquals($name, $group->getName()); + + $this->assertEquals($color, $group->getColor()); + } + public function getValidGroupInputs(): array + { + return [ + ['Jack', '#930ba8'], + ['Sparrow', '#b0bf65'], + ['Elizabeth', '#d42049'], + ['Swann', '#a584b9'], + ['Davy', '#29d75b'], + ['Jones','#a7a385'], + ]; + } + + public function getInValidGroupInputs(): array + { + return [ + ['', '#930ba8'], + ['Sparrow', 'b0bf65'], + ['Elizabeth', '#d4209'], + ['Swann', '#'], + ['Davy', '#2'], + ['Jones','#a7'], + ]; + } +} diff --git a/tests/Recipient/ProcessedContactTest.php b/tests/Recipient/ProcessedContactTest.php index a580813..ef061f9 100644 --- a/tests/Recipient/ProcessedContactTest.php +++ b/tests/Recipient/ProcessedContactTest.php @@ -44,15 +44,7 @@ class ProcessedContactTest extends AbstractEnviromentProvider $this->incompleteContactStd = json_decode($this->incompleteContactString); $this->minimalContactStd = json_decode($this->minimalContactString); } - public function testIsContactBookEmpty() - { - $contacts = RecipientFactory::createProcessedContactFromStdClassArray( - json_decode( - $this->communicator->get("api/contacts") - ) - ); - $this->assertEquals([], $contacts->getArrayCopy); - } + public function testCreateCompleteFromStdClass() { $contact = RecipientFactory::createProcessedContactFromStdClass($this->completeContactStd); diff --git a/tests/Recipient/ProcessedGroupTest.php b/tests/Recipient/ProcessedGroupTest.php new file mode 100644 index 0000000..52cc71b --- /dev/null +++ b/tests/Recipient/ProcessedGroupTest.php @@ -0,0 +1,33 @@ +<?php + +namespace IP1\RESTClient\Test\Recipient; + +use PHPUnit\Framework\TestCase; +use IP1\RESTClient\Recipient\ProcessedGroup; +use IP1\RESTClient\Test\Util\Util; + +class ProcessedGroupTest extends TestCase +{ + /** + * @dataProvider getProcessedGroups + */ + public function testGetters(ProcessedGroup $group) + { + $this->assertTrue(is_int($group->getID())); + $this->assertTrue(0 < $group->getID()); + } + public static function getProcessedGroups(): array + { + $retval = []; + for ($i = 0; $i < 50; $i++) { + $retval[] = new ProcessedGroup( + Util::getRandomAlphaString(), + Util::getRandomHex(), + random_int(1, PHP_INT_MAX), + Util::getRandomDateTime(), + Util::getRandomDateTime() + ); + } + return [$retval]; + } +} diff --git a/tests/SMS/LoggedOutGoingSMSTest.php b/tests/SMS/LoggedOutGoingSMSTest.php index 7675c22..0abe2ff 100644 --- a/tests/SMS/LoggedOutGoingSMSTest.php +++ b/tests/SMS/LoggedOutGoingSMSTest.php @@ -7,8 +7,14 @@ use IP1\RESTClient\Recipient\RecipientFactory; class LoggedOutGoingSMSTest extends AbstractEnviromentProvider { + /** + * @group api + */ public function testIsSMSListEmpty() { + if (!$this->isCommunicatorEnabled()) { + $this->markTestSkipped("Communicator is not enabled skipping test"); + } $sms = json_decode($this->communicator->get('api/sms/sent')); $this->assertEquals([], $sms); } diff --git a/tests/Util/Util.php b/tests/Util/Util.php new file mode 100644 index 0000000..0cde0fa --- /dev/null +++ b/tests/Util/Util.php @@ -0,0 +1,32 @@ +<?php +namespace IP1\RESTClient\Test\Util; + +class Util +{ + public static function getRandomDateTime() + { + $randomTimeStamp = rand(0, 1489655402); + $date = new \DateTime(null, new \DateTimeZone("UTC")); + $date->setTimestamp($randomTimeStamp); + return $date; + } + public static function getRandomAlphaString(int $length = 30) + { + $characters = 'abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $characterLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $characterLength - 1)]; + } + return $randomString; + } + public static function getRandomHex() + { + $chars = 'ABCDEF0123456789'; + $color = "#"; + for ($i = 0; $i < 6; $i++) { + $color .= $chars[rand(0, strlen($chars) - 1)]; + } + return $color; + } +} |