mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
PHP7 FactoryMethod
This commit is contained in:
@@ -6,44 +6,46 @@ use DesignPatterns\Creational\FactoryMethod\FactoryMethod;
|
||||
use DesignPatterns\Creational\FactoryMethod\GermanFactory;
|
||||
use DesignPatterns\Creational\FactoryMethod\ItalianFactory;
|
||||
|
||||
/**
|
||||
* FactoryMethodTest tests the factory method pattern.
|
||||
*/
|
||||
class FactoryMethodTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $type = array(
|
||||
FactoryMethod::CHEAP,
|
||||
FactoryMethod::FAST,
|
||||
);
|
||||
|
||||
public function getShop()
|
||||
public function testCanCreateCheapVehicleInGermany()
|
||||
{
|
||||
return array(
|
||||
array(new GermanFactory()),
|
||||
array(new ItalianFactory()),
|
||||
);
|
||||
$factory = new GermanFactory();
|
||||
$result = $factory->create(FactoryMethod::CHEAP);
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\Bicycle', $result);
|
||||
}
|
||||
|
||||
public function testCanCreateFastVehicleInGermany()
|
||||
{
|
||||
$factory = new GermanFactory();
|
||||
$result = $factory->create(FactoryMethod::FAST);
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\CarMercedes', $result);
|
||||
}
|
||||
|
||||
public function testCanCreateCheapVehicleInItaly()
|
||||
{
|
||||
$factory = new ItalianFactory();
|
||||
$result = $factory->create(FactoryMethod::CHEAP);
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\Bicycle', $result);
|
||||
}
|
||||
|
||||
public function testCanCreateFastVehicleInItaly()
|
||||
{
|
||||
$factory = new ItalianFactory();
|
||||
$result = $factory->create(FactoryMethod::FAST);
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\CarFerrari', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getShop
|
||||
*/
|
||||
public function testCreation(FactoryMethod $shop)
|
||||
{
|
||||
// this test method acts as a client for the factory. We don't care
|
||||
// about the factory, all we know is it can produce vehicle
|
||||
foreach ($this->type as $oneType) {
|
||||
$vehicle = $shop->create($oneType);
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\VehicleInterface', $vehicle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getShop
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage spaceship is not a valid vehicle
|
||||
*/
|
||||
public function testUnknownType(FactoryMethod $shop)
|
||||
public function testUnknownType()
|
||||
{
|
||||
$shop->create('spaceship');
|
||||
(new ItalianFactory())->create('spaceship');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user