diff --git a/composer.json b/composer.json index 9984158..983dcd1 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "autoload-dev": { "psr-4": { - "Swader\\Diffbot\\Test\\": "tests" + "Swader\\Diffbot\\Test\\": "tests/" } }, "extra": { diff --git a/tests/Abstracts/ApiTest.php b/tests/Abstracts/ApiTest.php index b1adffd..aed5849 100644 --- a/tests/Abstracts/ApiTest.php +++ b/tests/Abstracts/ApiTest.php @@ -4,7 +4,6 @@ use Swader\Diffbot\Abstracts\Api; use Swader\Diffbot\Diffbot; -use Swader\Diffbot\Exceptions\DiffbotException; class ApiTest extends \PHPUnit_Framework_TestCase { @@ -19,103 +18,108 @@ private function buildMock() return $this->getMockForAbstractClass($this->className, [$this->testUrl]); } - public function testSetTimeout() + public function validTimeouts() { - /** @var Api $mock */ - $mock = $this->buildMock(); - - $validTimeouts = [ - 0, - 1000, - 2000, - 3000, - 3000000, - 40000000, - null + return [ + 'zero' => [0], + '1000' => [1000], + '2000' => [2000], + '3000' => [3000], + '3 mil' => [3000000], + '40 mil' => [40000000] ]; + } - $invalidTimeouts = [ - -298979879827, - -4983, - 'abcef', - '', - false + public function invalidTimeouts() + { + return [ + 'negative_big' => [-298979879827], + 'negative_small' => [-4983], + 'string ' => ['abcef'], + 'empty string' => [''], + 'bool' => [false] ]; + } - try { - $mock->setTimeout(); - } catch (\InvalidArgumentException $e) { - $this->fail('Failed with supposedly valid (empty) timeout.'); - } + public function testSetEmptyTimeoutSuccess() + { + /** @var Api $mock */ + $mock = $this->buildMock(); + $mock->setTimeout(); + } - foreach ($validTimeouts as $timeout) { - try { - $mock->setTimeout($timeout); - } catch (\InvalidArgumentException $e) { - $this->fail('Failed with supposedly valid timeout: ' . $timeout); - } - } + /** + * @dataProvider invalidTimeouts + * @param $timeout mixed + */ + public function testSetTimeoutInvalid($timeout) + { + /** @var Api $mock */ + $mock = $this->buildMock(); + $this->setExpectedException('InvalidArgumentException'); + $mock->setTimeout($timeout); + } - foreach ($invalidTimeouts as $timeout) { - try { - $mock->setTimeout($timeout); - } catch (\InvalidArgumentException $e) { - // Got expected exception - continue; - } - $this->fail('Failed, assumed invalid parameter was valid.'); - } + /** + * @dataProvider validTimeouts + * @param $timeout int + */ + public function testSetTimeoutValid($timeout) + { + /** @var Api $mock */ + $mock = $this->buildMock(); + $mock->setTimeout($timeout); } - public function testConstructor() - { - $validUrls = [ - 'http://google.com', - 'http://gigaom.com/cloud/silicon-valley-royalty-pony-up-2m-to-scale-diffbots-visual-learning-robot/', - <<<'TAG' -http://techcrunch.com/2012/05/31/diffbot-raises-2-million-seed-round-for-web-content-extraction-technology/ -TAG - , - 'http://www.theverge.com/2012/5/31/3054444/diffbot-raises-2-million-apps-open-web', - 'http://venturebeat.com/2012/08/16/diffbot-api-links', - 'http://www.wired.co.uk/news/archive/2012-06/01/diffbot', - 'http://www.amazon.com/Oh-The-Places-Youll-Go/dp/0679805273/', - 'http://us.levi.com/product/index.jsp?productId=2076855', - <<<'TAG' -http://www.petsmart.com/dog/grooming-supplies/grreat-choice-soft-slicker-dog-brush-zid36-12094/cat-36-catid-100016 -TAG - , - 'http://instagram.com/p/t879OvgvqS/', - 'http://smittenkitchen.com/blog/2012/01/buckwheat-baby-with-salted-caramel-syrup/', - 'https://twitter.com/NASA/status/525397368116895744', - 'www.example.com', - 'example.com' + public function validUrls() + { + return [ + ['http://google.com'], + ['http://gigaom.com/cloud/silicon-valley-royalty-pony-up-2m-to-scale-diffbots-visual-learning-robot/'], + ['http://techcrunch.com/2012/05/31/diffbot-raises-2-million-seed-round-for-web-content-extraction-technology/'], + ['http://www.theverge.com/2012/5/31/3054444/diffbot-raises-2-million-apps-open-web'], + ['http://venturebeat.com/2012/08/16/diffbot-api-links'], + ['http://www.wired.co.uk/news/archive/2012-06/01/diffbot'], + ['http://www.amazon.com/Oh-The-Places-Youll-Go/dp/0679805273/'], + ['http://us.levi.com/product/index.jsp?productId=2076855'], + ['http://www.petsmart.com/dog/grooming-supplies/grreat-choice-soft-slicker-dog-brush-zid36-12094/cat-36-catid-100016'], + ['http://instagram.com/p/t879OvgvqS/'], + ['http://smittenkitchen.com/blog/2012/01/buckwheat-baby-with-salted-caramel-syrup/'], + ['https://twitter.com/NASA/status/525397368116895744'], + ['www.example.com'], + ['example.com'] ]; + } - $invalidUrls = [ - false, - null, - 12345, - 'abc', - '35tugz---sdf----?//*****/*//*' + public function invalidUrls() + { + return [ + 'bool' => [false], + 'null' => [null], + 'number' => [12345], + 'abc' => ['abc'], + 'misc_string' => ['35tugz---sdf----?//*****/*//*'] ]; + } - foreach ($validUrls as $i => $url) { - try { - $this->getMockForAbstractClass($this->className, [$url]); - } catch (\InvalidArgumentException $e) { - $this->fail('Failed with supposedly valid URL: ' . $url . ' at index ' . $i); - } - } + /** + * @dataProvider validUrls + * @param $url string + */ + public function testValidUrls($url) + { + $mock = $this->getMockForAbstractClass($this->className, [$url]); + $this->assertInstanceOf($this->className, $mock); + } - foreach ($invalidUrls as $i => $url) { - try { - $this->getMockForAbstractClass($this->className, [$url]); - } catch (\InvalidArgumentException $e) { - continue; - } - $this->fail('Did not fail with invalid URL at index ' . $i); - } + /** + * @dataProvider invalidUrls + * @param $url mixed + */ + public function testInvalidUrls($url) + { + $this->setExpectedException('InvalidArgumentException'); + $this->getMockForAbstractClass($this->className, [$url]); } protected function returnApis() @@ -130,31 +134,46 @@ protected function returnApis() ]; } - public function testFieldSettersSuccess() + public function validOptionalFieldValues() { - /** - * @var $name string - * @var $api Api - */ - foreach ($this->returnApis() as $name => $api) { - $fields = $api::getOptionalFields(); - foreach ($fields as $field) { - $setterName = 'set' . ucfirst($field); - $values = [true, false, null]; - $i = 1000; - while ($i--) { - $v = $values[rand(0, count($values) - 1)]; - try { - $api->$setterName($v); - } catch (\InvalidArgumentException $e) { - $this->fail($e->getMessage()); - } - } - } - } + return [ + [true], + [false], + [null] + ]; } - public function testFieldSettersFail() + public function invalidOptionalFieldValues() + { + return [ + 'stringT' => ['true'], + 'stringF' => ['false'], + [5], + [0], + [1], + 'empty' => [''], + 'operation' => [5 + 5], + 'string' => ['hello'], + '1e-arrayT' => [[true]], + '1e-arrayF' => [[false]], + '2e-array_bool' => [[true, false]] + ]; + } + + public function invalidFieldNames() + { + return [ + ['gibberish'], + ['nonsense'], + ['folly'] + ]; + } + + /** + * @dataProvider validOptionalFieldValues + * @param $fieldValue bool + */ + public function testFieldSettersSuccess($fieldValue) { /** * @var $name string @@ -164,25 +183,18 @@ public function testFieldSettersFail() $fields = $api::getOptionalFields(); foreach ($fields as $field) { $setterName = 'set' . ucfirst($field); - $values = ['true', 'false', 5, 0, 1, '', 5 + 5, 'hello', [true], [false], [true, false]]; - $i = 1000; - while ($i--) { - $v = $values[rand(0, count($values) - 1)]; - try { - $api->$setterName($v); - } catch (\InvalidArgumentException $e) { - // All good, we got the exception, next iteration please - continue; - } - $message = 'ProductAPI did not error when given wrong input into ' . $setterName . '. '; - $message .= 'The input was: ' . print_r($v, true); - $this->fail($message); - } + $getterName = 'get' . ucfirst($field); + $api->$setterName($fieldValue); + $this->assertTrue(is_bool($api->$getterName())); } } } - public function testFieldGettersSuccess() + /** + * @dataProvider invalidOptionalFieldValues + * @param $fieldValue mixed + */ + public function testFieldSettersFail($fieldValue) { /** * @var $name string @@ -191,49 +203,54 @@ public function testFieldGettersSuccess() foreach ($this->returnApis() as $name => $api) { $fields = $api::getOptionalFields(); foreach ($fields as $field) { - $getterName = 'get' . ucfirst($field); - $this->assertTrue(is_bool($api->$getterName())); + $setterName = 'set' . ucfirst($field); + try { + $api->$setterName($fieldValue); + } catch (\Exception $e) { + $this->assertInstanceOf('InvalidArgumentException', $e); + continue; + } + $message = 'API did not error when given wrong input into ' . $setterName . '. '; + $message .= 'The input was: ' . print_r($fieldValue, true); + $this->fail($message); } } } - public function testFieldFail() + /** + * @dataProvider invalidFieldNames + * @param string $fieldName + */ + public function testFieldFail($fieldName) { - $invalidFieldNames = ['gibberish', 'nonsense', 'folly']; + $getterName = 'get' . ucfirst($fieldName); + $setterName = 'set' . ucfirst($fieldName); /** * @var $name string * @var $api Api */ foreach ($this->returnApis() as $name => $api) { - $settableValues = [true, false, null]; - foreach ($invalidFieldNames as $field) { - $getterName = 'get' . ucfirst($field); - $setterName = 'set' . ucfirst($field); - - $e1 = false; - try { - $api->$getterName(); - } catch (\BadMethodCallException $e1) { - // Exception should happen, all good - } - if (!$e1) { - $this->fail('No exception raised when getting invalid field: ' . $field); - } + $e1 = false; + try { + $api->$getterName(); + } catch (\BadMethodCallException $e1) { + // Exception should happen, all good + } + if (!$e1) { + $this->fail('No exception raised when getting invalid field: ' . $fieldName); + } - $e2 = false; - foreach ($settableValues as $value) { - try { - $api->$setterName($value); - } catch (\BadMethodCallException $e2) { - // Exception should happen, all good - } - if (!$e2) { - $this->fail('No exception raised when setting invalid field: ' . $field); - } - } + $e2 = false; + try { + $api->$setterName(true); + } catch (\BadMethodCallException $e2) { + // Exception should happen, all good + } + if (!$e2) { + $this->fail('No exception raised when setting invalid field: ' . $fieldName); } - foreach ($api::getOptionalFields() as $field) { - $methodName = 'wet' . ucfirst($field); + foreach ($api::getOptionalFields() as $fieldName) { + $methodName = 'wet' . ucfirst($fieldName); $e3 = false; try { $api->$methodName(); @@ -241,10 +258,24 @@ public function testFieldFail() // Exception should happen, all good } if (!$e3) { - $this->fail('No exception raised when using invalid prefix: ' . $field); + $this->fail('No exception raised when using invalid prefix: ' . $fieldName); } } } + } + public function testFieldGettersSuccess() + { + /** + * @var $name string + * @var $api Api + */ + foreach ($this->returnApis() as $name => $api) { + $fields = $api::getOptionalFields(); + foreach ($fields as $field) { + $getterName = 'get' . ucfirst($field); + $this->assertTrue(is_bool($api->$getterName())); + } + } } } \ No newline at end of file diff --git a/tests/DiffbotTest.php b/tests/DiffbotTest.php index 8a935b7..fe3e560 100644 --- a/tests/DiffbotTest.php +++ b/tests/DiffbotTest.php @@ -5,72 +5,72 @@ use Swader\Diffbot\Diffbot; use Swader\Diffbot\Exceptions\DiffbotException; -/** - * @runTestsInSeparateProcesses - */ class DiffbotTest extends \PHPUnit_Framework_TestCase { - private $invalidTokens = [ - '', - 'a', - 'ab', - 'abc', - 1, - 12, - 123, - true, - array('token') - ]; - - private $validTokens = [ - 'token', - '123456789', - 'akrwejhtn983z420qrzc8397r4' - ]; - - public function testStaticSetToken() + public function invalidTokens() { - foreach ($this->invalidTokens as $value) { - try { - Diffbot::setToken($value); - } catch (\InvalidArgumentException $e) { - // Good, we got an exception! - continue; - } - $this->fail('Expected exception not raised on value: "' . $value . '".'); - } - - foreach ($this->validTokens as $value) { - Diffbot::setToken($value); - } + return [ + 'empty' => [ '' ], + 'a' => [ 'a' ], + 'ab' => [ 'ab' ], + 'abc' => [ 'abc' ], + 'digit' => [ 1 ], + 'double-digit' => [ 12 ], + 'triple-digit' => [ 123 ], + 'bool' => [ true ], + 'array' => [ ['token'] ], + ]; } - public function testInstantiation() + public function validTokens() { - $exceptionTriggered = false; - try { - new Diffbot(); - } catch (DiffbotException $e) { - // Great, got it! - $exceptionTriggered = true; - } - if (!$exceptionTriggered) { - $this->fail('Empty token did not produce exception!'); - } - - try { - Diffbot::setToken('token'); - new Diffbot(); - } catch (DiffbotException $e) { - $this->fail('Scenario failed!'); - } - - try { - new Diffbot('token'); - } catch (DiffbotException $e) { - $this->fail('Scenario failed!'); - } + return [ + 'token' => [ 'token' ], + 'short-hash' => [ '123456789' ], + 'full-hash' => [ 'akrwejhtn983z420qrzc8397r4' ], + ]; + } + + /** + * @dataProvider invalidTokens + */ + public function testSetTokenRaisesExceptionOnInvalidToken($token) + { + $this->setExpectedException('InvalidArgumentException'); + Diffbot::setToken($token); + } + + /** + * @dataProvider validTokens + */ + public function testSetTokenSucceedsOnValidToken($token) + { + Diffbot::setToken($token); + $bot = new Diffbot(); + $this->assertInstanceOf('\Swader\Diffbot\Diffbot', $bot); + } + + /** + * @runInSeparateProcess + */ + public function testInstantiationWithNoGlobalTokenAndNoArgumentRaisesAnException() + { + $this->setExpectedException('\Swader\Diffbot\Exceptions\DiffbotException'); + new Diffbot(); + } + + public function testInstantiationWithGlobalTokenAndNoArgumentSucceeds() + { + Diffbot::setToken('token'); + $bot = new Diffbot(); + $this->assertInstanceOf('Swader\Diffbot\Diffbot', $bot); + } + + public function testInstantiationWithNoGlobalTokenButWithArgumentSucceeds() + { + $bot = new Diffbot('token'); + $this->assertInstanceOf('Swader\Diffbot\Diffbot', $bot); } public function testGetToken()