diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a8e20f9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +php: + - 7.0 + - 7.1 + - 7.2 + +install: + - travis_retry composer install + +script: + - vendor/bin/phpunit diff --git a/composer.json b/composer.json index 8a199a0..f8d65c1 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,16 @@ "psr-4": { "Colors\\": "src" } + }, + "autoload-dev": { + "psr-4": { + "Colors\\Tests\\": "tests" + } + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.5" } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..d0f1ccc --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,22 @@ + + + + + ./tests + + + + + src + + + diff --git a/src/RandomColor.php b/src/RandomColor.php index df96204..2a8386b 100644 --- a/src/RandomColor.php +++ b/src/RandomColor.php @@ -33,11 +33,11 @@ class RandomColor { - static public $dictionary = null; + public static $dictionary = null; private function __construct() {} - static public function one($options = array()) + public static function one($options = array()) { $h = self::_pickHue($options); $s = self::_pickSaturation($h, $options); @@ -46,7 +46,7 @@ static public function one($options = array()) return self::format(compact('h','s','v'), @$options['format']); } - static public function many($count, $options = array()) + public static function many($count, $options = array()) { $colors = array(); @@ -58,7 +58,7 @@ static public function many($count, $options = array()) return $colors; } - static public function format($hsv, $format='hex') + public static function format($hsv, $format='hex') { switch ($format) { @@ -84,7 +84,7 @@ static public function format($hsv, $format='hex') } } - static private function _pickHue($options) + private static function _pickHue($options) { $range = self::_getHueRange($options); @@ -105,7 +105,7 @@ static private function _pickHue($options) return $hue; } - static private function _pickSaturation($h, $options) + private static function _pickSaturation($h, $options) { if (@$options['hue'] === 'monochrome') { @@ -137,7 +137,7 @@ static private function _pickSaturation($h, $options) return self::_rand($range, $options); } - static private function _pickBrightness($h, $s, $options) + private static function _pickBrightness($h, $s, $options) { if (@$options['luminosity'] === 'random') { @@ -165,7 +165,7 @@ static private function _pickBrightness($h, $s, $options) return self::_rand($range, $options); } - static private function _getHueRange($options) + private static function _getHueRange($options) { $ranges = array(); @@ -212,7 +212,7 @@ static private function _getHueRange($options) } } - static private function _getMinimumBrightness($h, $s) + private static function _getMinimumBrightness($h, $s) { $colorInfo = self::_getColorInfo($h); $bounds = $colorInfo['bounds']; @@ -235,7 +235,7 @@ static private function _getMinimumBrightness($h, $s) return 0; } - static private function _getColorInfo($h) + private static function _getColorInfo($h) { // Maps red colors to make picking hue easier if ($h >= 334 && $h <= 360) @@ -252,7 +252,7 @@ static private function _getColorInfo($h) } } - static private function _rand($bounds, $options) + private static function _rand($bounds, $options) { if (isset($options['prng'])) { @@ -264,7 +264,7 @@ static private function _rand($bounds, $options) } } - static public function hsv2hex($hsv) + public static function hsv2hex($hsv) { $rgb = self::hsv2rgb($hsv); $hex = '#'; @@ -277,7 +277,7 @@ static public function hsv2hex($hsv) return $hex; } - static public function hsv2hsl($hsv) + public static function hsv2hsl($hsv) { extract($hsv); @@ -292,7 +292,7 @@ static public function hsv2hsl($hsv) ); } - static public function hsv2rgb($hsv) + public static function hsv2rgb($hsv) { extract($hsv); @@ -387,4 +387,4 @@ static public function hsv2rgb($hsv) 'h' => array(283,334), 's' => array(20,100) ) - ); \ No newline at end of file + ); diff --git a/tests/RandomColorTest.php b/tests/RandomColorTest.php new file mode 100644 index 0000000..9147ad0 --- /dev/null +++ b/tests/RandomColorTest.php @@ -0,0 +1,80 @@ +assertContains('#', $hexColor); + $this->assertSame(7, strlen($hexColor)); + $this->assertRegExp('/#\w{6}/', $hexColor); + } + + public function testManyShouldReturnTenGreenColors() + { + $hexColors = RandomColor::many(10, [ + 'hue' => 'green', + ]); + + $this->assertCount(10, $hexColors); + foreach ($hexColors as $hexColor) { + $this->assertContains('#', $hexColor); + $this->assertSame(7, strlen($hexColor)); + $this->assertRegExp('/#\w{6}/', $hexColor); + } + } + + public function testOneShouldReturnRandomLightBlueColor() + { + $hexColor = RandomColor::one([ + 'luminosity' => 'light', + 'hue' => 'blue', + ]); + + $this->assertContains('#', $hexColor); + $this->assertSame(7, strlen($hexColor)); + $this->assertRegExp('/#\w{6}/', $hexColor); + } + + public function testOneShouldReturnRandomLightBlueOrYellowColor() + { + $hexColor = RandomColor::one([ + 'hue' => ['yellow', 'blue'], + ]); + + $this->assertContains('#', $hexColor); + $this->assertSame(7, strlen($hexColor)); + $this->assertRegExp('/#\w{6}/', $hexColor); + } + + public function testOneShouldReturnTrulyRandomColor() + { + $hexColor = RandomColor::one([ + 'luminosity' => 'random', + 'hue' => 'random', + ]); + + $this->assertContains('#', $hexColor); + $this->assertSame(7, strlen($hexColor)); + $this->assertRegExp('/#\w{6}/', $hexColor); + } + + public function testOneShouldReturnRandomBrightRgbColor() + { + $hexColor = RandomColor::one([ + 'luminosity' => 'bright', + 'format' => 'rgbCss', + ]); + + $this->assertContains('rgb(', $hexColor); + $this->assertContains(')', $hexColor); + $this->assertContains(',', $hexColor); + $this->assertRegExp('/rgb\( *(\d{1,3} *, *\d{1,3} *, *\d{1,3}) *\)/i', $hexColor); + } +}