phpspec tips&tricks
Embed Size (px)
TRANSCRIPT
- 1. PHPSPEC TIPS & TRICKS PHPERS SILESIA 24.05.2016
- 2. Filip Golonka Team leader & developer at Schibsted Tech Polska FilipGolonka http://github.com/lipgolonka Krakw
- 3. PHPSPEC - TIPS&TRICKS PHPSPEC SpecBDD Framework for PHP A php toolset to drive emergent design by specication. phpspec is a tool which can help you write clean and working PHP code using behaviour driven development
- 4. & community
- 5. PHPSTORM SUPPORT
- 6. namespace specAppBundleApi; use PhpSpecObjectBehavior; use AppBundleApiListParameters; /** * @mixin ListParameters */ class ListParametersSpec extends ObjectBehavior { function it_is_initializable() { $this- >shouldHaveType(ListParameters::class); } }
- 7. NOT COMMON MATCHERS
- 8. https://github.com/yvoyer/phpspec-cheat-sheet
- 9. PHPSPEC - TIPS&TRICKS ARRAYS & STRINGS MATCHERS $this->getArrayValue()->shouldContain('Jane Smith'); $this->getArrayValue()->shouldHaveKeyWithValue('key', 'value'); $this->getArrayValue()->shouldHaveKey(key'); $this->getStringValue()->shouldContain('value'); $this->getStringValue()->shouldStartWith('value'); $this->getStringValue()->shouldEndWith('value'); $this->getStringValue()->shouldMatch(/pattern/');
- 10. PHPSPEC - TIPS&TRICKS BOOLEAN MATCHERS class Event { private $active; public function isActive(): bool { return $this->active; } } class EventSpec extends ObjectBehavior { function it_is_active() { $this->setActive(); $this->shouldBeActive(); // or $this->isActive()->shouldBe(true); } function it_is_not_active() { $this->shouldNotBeActive(); // or $this->isActive()->shouldReturn(false); } }
- 11. PHPSPEC - TIPS&TRICKS SCALAR MATCHERS $this->getBool()->shouldBeBool(); $this->getObject()->shouldBeObject(); $this->getString()->shouldBeString(); $this->getInteger()->shouldBeInteger(); $this->getDecimal()->shouldBeDecimal(); $this->getCollection()->shouldBeArray();
- 12. CUSTOM MATCHERS
- 13. class Event { private $id; private $rate; private $sum; public function getRating(): array { return [ 'id' => $this->id, 'rate' => $this->rate, 'sum' => $this->sum, ]; } } class EventSpec extends ObjectBehavior { function it_has_rating() { $this->getRating()->shouldBeRating(); } public function getMatchers() { return [ 'beRating' => function($subject) { return array_key_exists('id', $subject) && array_key_exists('rate', $subject) && array_key_exists('sum', $subject); }, ]; } }
- 14. class Event { public $name; public $description; public static function withNameAndDescription(string $name, string $description): Event { $instance = new self(); $instance->name = $name; $instance->description = $description; return $instance; } } class EventSpec extends ObjectBehavior { const NAME = 'Name'; const DESCRIPTION = 'Description'; function it_has_name_and_description() { $this::withNameAndDescription(self::NAME, self::DESCRIPTION) ->shouldHaveNameAndDescription(self::NAME, self::DESCRIPTION); } public function getMatchers() { return [ 'haveNameAndDescription' => function($subject, $name, $description) { return $subject->name == $name && $subject->description == $description; }, ]; } }
- 15. WORKING WITH COLLABORATORS
- 16. http://blogophp.com
- 17. PHPSPEC - TIPS&TRICKS CALL METHOD MULTIPLE TIMES class SUS { private $object; public function something() { return $this->object->rand() + $this->object->rand() + $this->object->rand(); } } class SUSSpec extends ObjectBehavior { function let(Object $object) { $this->beConstructedWith($object); } function it_returns_something(Object $object) { $object->rand()->willReturn(1, 2, 3); $this->something()->shouldReturn(6); } }
- 18. PHPSPEC - TIPS&TRICKS STRONG COMPARISON ON COLLABORATOR METHOD class SUS { private $object; public function getNumber($number) { return $this->object->something($number); } } class SUSSpec extends ObjectBehavior { function let(Object $object) { $this->beConstructedWith($object); } function it_returns_1_for_1(Object $object) { $object->getNumber(1)->willReturn(1); $this->getNumber("1")->shouldReturn(1); } }
- 19. PHPSPEC - TIPS&TRICKS STRONG COMPARISON ON COLLABORATOR METHOD class SUS { private $object; public function getNumber($number) { return $this->object->something($number); } } class SUSSpec extends ObjectBehavior { function let(Object $object) { $this->beConstructedWith($object); } function it_returns_1_for_1(Object $object) { $object->getNumber(Argument::is(1))->willReturn(1); $this->getNumber("1")->shouldReturn(1); } }
- 20. TEMPLATES
- 21. PHPSPEC - TIPS&TRICKS SPECIFICATION TEMPLATE .phpspec/specication.tpl