Last active
January 16, 2016 15:16
-
-
Save teklakct/ae1cfd8d03ceee4a9063 to your computer and use it in GitHub Desktop.
Przykładowa specyfikacja do opisania zachowania Named Constructors wykorzystując PHPSpec
This file contains hidden or 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 | |
class Person | |
{ | |
private $name; | |
private $surname; | |
private function __construct($name, $surname) | |
{ | |
$this->name = $name; | |
$this->surname = $surname; | |
} | |
public static function born($name, $surname) | |
{ | |
$person = new Person($name, $surname); | |
// TODO: write other logic here | |
return $person; | |
} | |
public static function bornAsCloneOf(Person $original) | |
{ | |
$name = $original->getName(); | |
$surname = $original->getSurname(); | |
$person = new Person($name, $surname); | |
// TODO: write other logic here | |
return $person; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getSurname() | |
{ | |
return $this->surname; | |
} | |
public function changeName($newName) | |
{ | |
$this->name = $newName; | |
} | |
} |
This file contains hidden or 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 | |
namespace spec; | |
use Person; | |
use PhpSpec\ObjectBehavior; | |
use Prophecy\Argument; | |
/** | |
* Class PersonSpec | |
* @package spec | |
* @mixin Person | |
*/ | |
class PersonSpec extends ObjectBehavior | |
{ | |
function let() | |
{ | |
$name = 'asia'; | |
$surname = 'asiowa'; | |
$this->beConstructedThroughBorn($name, $surname); | |
} | |
function its_person() | |
{ | |
$this->shouldHaveType(Person::class); | |
} | |
function it_create_person_clone(Person $original) | |
{ | |
$original->getName()->willReturn('Jola'); | |
$original->getSurname()->willReturn('Jolanta'); | |
$this->beConstructedThroughBornAsCloneOf($original); | |
$this->getName()->shouldBe('Jola'); | |
$this->getSurname()->shouldBe('Jolanta'); | |
} | |
function it_allow_to_change_person_name() | |
{ | |
$this->changeName('miriam'); | |
$this->getName()->shouldBe('miriam'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment