Created
December 2, 2019 16:38
-
-
Save demircan-s/92ccc4d43b338536749da0fce59814fe to your computer and use it in GitHub Desktop.
First example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
class ColorToRGB | |
{ | |
private $color; | |
private function __construct(string $color) | |
{ | |
if($this->checkIsValid($color) == true) | |
$this->color = $color; | |
else | |
throw new \InvalidArgumentException("Unsupported Color Name"); | |
} | |
private function checkIsValid(string $color) | |
{ | |
$isValid = false; | |
if($color == "black" || $color == "white") | |
$isValid = true; | |
return $isValid; | |
} | |
public static function createFromColor(string $color) : self | |
{ | |
return new self($color); | |
} | |
public function getRGB() : string | |
{ | |
switch ($this->color){ | |
case "black": | |
return "0,0,0"; | |
break; | |
case "white": | |
return "255,255,255"; | |
break; | |
default: | |
throw new \InvalidArgumentException("Unsupported Color Name"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use PHPUnit\Framework\TestCase; | |
class ColorToRGBTest extends TestCase | |
{ | |
/** | |
* @dataProvider supportedColorsProvider | |
*/ | |
public function testCanBeCreatedForSupportedColors($supportedColor) | |
{ | |
$this->assertInstanceOf( | |
ColorToRGB::class, | |
ColorToRGB::createFromColor($supportedColor) | |
); | |
} | |
/** | |
* @dataProvider unsupportedColorsProvider | |
*/ | |
public function testCannotBeCreatedForUnsupportedColors($unsupportedColor) | |
{ | |
$this->expectException(\InvalidArgumentException::class); | |
$colorToRgb = ColorToRGB::createFromColor($unsupportedColor); | |
} | |
/** | |
* @dataProvider colorRgbMatchProvider | |
*/ | |
public function testGetRgbMatchFromColor($color,$rgbVal) | |
{ | |
$colorToRgb = ColorToRGB::createFromColor($color); | |
$this->assertEquals($rgbVal, $colorToRgb->getRGB()); | |
} | |
public function unsupportedColorsProvider() | |
{ | |
return [ | |
["red"], | |
["yellow"], | |
["blue"], | |
["green"], | |
["grey"], | |
["purple"], | |
]; | |
} | |
public function supportedColorsProvider() | |
{ | |
return [ | |
["black"], | |
["white"] | |
]; | |
} | |
public function colorRgbMatchProvider() | |
{ | |
return [ | |
["black","0,0,0"], | |
["white","255,255,255"] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment