From 700cc0a1574466d50a35ab44368ef18aa1a216df Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Thu, 19 Nov 2015 22:33:37 +0200 Subject: [PATCH 1/3] Fixed Gravatar::RATING_X and error message --- Library/Phalcon/Avatar/Gravatar.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Library/Phalcon/Avatar/Gravatar.php b/Library/Phalcon/Avatar/Gravatar.php index a69462b76..8b51a02dc 100644 --- a/Library/Phalcon/Avatar/Gravatar.php +++ b/Library/Phalcon/Avatar/Gravatar.php @@ -79,7 +79,7 @@ class Gravatar implements Avatarable /** * @type string */ - const RATING_X = 'r'; + const RATING_X = 'x'; /** * The default image. @@ -273,8 +273,9 @@ public function setRating($rating) if (!isset($this->validRatings[$rating])) { - $allowed = implode(', ', array_keys($this->validRatings)); + $allowed = array_keys($this->validRatings); $last = array_pop($allowed); + $allowed = join(',', $allowed); throw new InvalidArgumentException( sprintf("Invalid rating '%s' specified. Available for use only: %s or %s", $rating, $allowed, $last) From 2bde78090d2974b87631fb86aa500b30b52d65db Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Thu, 19 Nov 2015 22:42:30 +0200 Subject: [PATCH 2/3] Minor improvements for Gravatar::setSize --- Library/Phalcon/Avatar/Gravatar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Phalcon/Avatar/Gravatar.php b/Library/Phalcon/Avatar/Gravatar.php index 8b51a02dc..243373225 100644 --- a/Library/Phalcon/Avatar/Gravatar.php +++ b/Library/Phalcon/Avatar/Gravatar.php @@ -244,7 +244,7 @@ public function setSize($size) )); } - $this->size = $size; + $this->size = (int) $size; return $this; } From 3aa1b4e29567bb606b5828d314edc3f1f8cd275d Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Thu, 19 Nov 2015 22:42:56 +0200 Subject: [PATCH 3/3] Initial test for Gravatar --- tests/unit/Avatar/GravatarTest.php | 110 +++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 tests/unit/Avatar/GravatarTest.php diff --git a/tests/unit/Avatar/GravatarTest.php b/tests/unit/Avatar/GravatarTest.php new file mode 100644 index 000000000..dd5558789 --- /dev/null +++ b/tests/unit/Avatar/GravatarTest.php @@ -0,0 +1,110 @@ + + * @link http://phalconphp.com/ + * @package Phalcon\Test\Avatar + * @group Avatar + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class GravatarTest extends Test +{ + /** + * UnitTester Object + * @var UnitTester + */ + protected $tester; + + /** + * executed before each test + */ + protected function _before() + { + } + + /** + * executed after each test + */ + protected function _after() + { + } + + /** + * @dataProvider incorrectConfigProvider + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Config must be either an array or \Phalcon\Config instance + * @param mixed $config + */ + public function testShouldThrowExceptionIfDbIsMissingOrInvalid($config) + { + new Gravatar($config); + } + + public function incorrectConfigProvider() + { + return [ + 'string' => [__CLASS__], + 'null' => [null], + 'true' => [true], + 'false' => [false], + 'object' => [new \stdClass()], + 'float' => [microtime(true)], + 'int' => [PHP_INT_MAX], + 'callable' => [function () {}], + 'resource' => [tmpfile()], + ]; + } + + public function testShouldUseConfigInstance() + { + $gravatar = new Gravatar(new Config([])); + $this->assertInstanceOf('Phalcon\Avatar\Gravatar', $gravatar); + } + + public function testShouldUseArrayAsConfig() + { + $gravatar = new Gravatar([]); + $this->assertInstanceOf('Phalcon\Avatar\Gravatar', $gravatar); + } + + public function testShouldSetUseDefaultValues() + { + $gravatar = new Gravatar([]); + + $this->assertEquals(Gravatar::RATING_G, $gravatar->getRating()); + $this->assertEquals(80, $gravatar->getSize()); + $this->assertNotTrue($gravatar->isUseSecureURL()); + } + + public function testShouldSetOptionsThroughConfig() + { + $gravatar = new Gravatar([ + 'default_image' => 'retro', + 'rating' => 'x', + 'size' => 60, + 'use_https' => true + ]); + + $this->assertEquals('retro', $gravatar->getDefaultImage()); + $this->assertEquals(Gravatar::RATING_X, $gravatar->getRating()); + $this->assertEquals(60, $gravatar->getSize()); + $this->assertTrue($gravatar->isUseSecureURL()); + } +}