Skip to content

Commit

Permalink
Merge pull request #485 from sergeyklay/2.0.x
Browse files Browse the repository at this point in the history
Initial test for Gravatr
  • Loading branch information
sergeyklay committed Nov 19, 2015
2 parents 6b5ed1c + 3aa1b4e commit 4e4af9f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Library/Phalcon/Avatar/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Gravatar implements Avatarable
/**
* @type string
*/
const RATING_X = 'r';
const RATING_X = 'x';

/**
* The default image.
Expand Down Expand Up @@ -244,7 +244,7 @@ public function setSize($size)
));
}

$this->size = $size;
$this->size = (int) $size;

return $this;
}
Expand Down Expand Up @@ -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)
Expand Down
110 changes: 110 additions & 0 deletions tests/unit/Avatar/GravatarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Phalcon\Test\Avatar;

use Phalcon\Avatar\Gravatar;
use Codeception\TestCase\Test;
use Phalcon\Config;
use UnitTester;

/**
* \Phalcon\Test\Avatar\GravatarTest
* Tests for Phalcon\Avatar\Gravatar component
*
* @copyright (c) 2011-2015 Phalcon Team
* @author Serghei Iakovlev <[email protected]>
* @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 [email protected]
* 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());
}
}

0 comments on commit 4e4af9f

Please sign in to comment.