1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-20 23:39:51 +01:00

Added hexcolor, rgbcolor and rgbColorAsArray

Changed readme, with the 3 new methods
Added 3 new tests
This commit is contained in:
lsv 2013-06-17 00:20:30 +02:00
parent 4ad4bc4b5c
commit ecd58f0217
3 changed files with 57 additions and 0 deletions

View File

@ -154,6 +154,9 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
locale // en_UK
countryCode // UK
languageCode // en
hexcolor // #fa3cc2
rgbcolor() // 0,255,122
rgbColorAsArray() // array(0,255,122)
### `Faker\Provider\Base`

View File

@ -148,4 +148,35 @@ class Miscellaneous extends \Faker\Provider\Base
{
return static::randomElement(static::$languageCode);
}
/**
* @example '
*/
public static function hexColor()
{
$hexcodes = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
return sprintf('#%s%s%s%s%s%s',
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes)
);
}
public static function rgbColorAsArray()
{
$rgb = array();
$color = str_replace('#', '', static::hexColor());
$rgb[] = hexdec(substr($color,0,2));
$rgb[] = hexdec(substr($color,2,2));
$rgb[] = hexdec(substr($color,4,2));
return $rgb;
}
public static function rgbColor()
{
return implode(',', static::rgbColorAsArray());
}
}

View File

@ -3,6 +3,7 @@
namespace Faker\Test\Provider;
use Faker\Provider\Base as BaseProvider;
use Faker;
class BaseTest extends \PHPUnit_Framework_TestCase
{
@ -124,4 +125,26 @@ class BaseTest extends \PHPUnit_Framework_TestCase
{
$this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r'));
}
public function testHexColor()
{
$faker = Faker\Factory::create();
$color = $faker->hexColor;
$this->assertRegExp('/^#[a-f0-9]{6}$/i', $color);
}
public function testRgbColorAsArray()
{
$faker = Faker\Factory::create();
$color = $faker->rgbColorAsArray;
$this->assertEquals(3, count($color));
}
public function testRgbColor()
{
$faker = Faker\Factory::create();
$color = $faker->rgbColor(false);
$regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
$this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', $color);
}
}