diff --git a/Library/Phalcon/Avatar/Gravatar.php b/Library/Phalcon/Avatar/Gravatar.php index a69462b76..243373225 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. @@ -244,7 +244,7 @@ public function setSize($size) )); } - $this->size = $size; + $this->size = (int) $size; return $this; } @@ -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) 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()); + } +}