bdd для php проектов

51
BEHAVIOR DRIVEN DEVELOPMENT ДЛЯ PHP ПРОЕКТОВ @everzet

Upload: konstantin-kudryashov

Post on 10-May-2015

4.579 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: BDD для PHP проектов

BEHAVIORDRIVENDEVELOPMENTДЛЯ PHP ПРОЕКТОВ

@everzet

Page 2: BDD для PHP проектов

КТО Я

ПРОГРАММИСТС РОЖДЕНИЯ

DEV.BYВЕДУЩИЙ РАЗРАБОТЧИК

symfony-разработчикС 2007 годаСОЗДАТЕЛЬBEHAT

http://everzet.com

Page 3: BDD для PHP проектов

РАЗРАБОТКАINSIDE-OUT

Page 4: BDD для PHP проектов

Тесты двигателя

Тесты электрики

Тесты колес

Тесты соединений

ТЕСТЫUNIT

Page 5: BDD для PHP проектов

TDD

=качественныйпродукт?+

разработкаunit-тесты

Page 6: BDD для PHP проектов

=+разработкаunit-тесты

качественный-продукт

TDD

Page 7: BDD для PHP проектов

ОТДЕЛЬНЫХ МОДУЛЕЙНЕ ГАРАНТИРУЮТ КАЧЕСТВОРАБОТЫ СИСТЕМЫ В ЦЕЛОМ

UNIT-ТЕСТЫ

Page 8: BDD для PHP проектов

РАЗРАБОТКАOUTSIDE-IN

Page 9: BDD для PHP проектов

ВХОДНЫЕ/ВЫХОДНЫЕ

ДАННЫЕ

входныевыходные

данныеданные

Page 10: BDD для PHP проектов

RSpec

# bowling_spec.rbrequire 'bowling'

describe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should == 0 endend

Page 11: BDD для PHP проектов

<?phprequire_once 'bowling.php'

class DescribeIndexController extends PHPSpec_Context_Zend{ public function itShouldDisplayHelloWorld() { $this->get(‘index’); $this-> response()-> should-> match(‘/Hello World/’); }}

PHPSpec

Page 12: BDD для PHP проектов

<?php

$browser->get(‘/job/new’)-> with(‘request’)->begin()-> isParameter(‘module’, ‘job’)-> isParameter(‘action’, ‘new’)-> end()->

click(‘Preview your job’, array(‘job’ => array( ‘company’ => ‘Sensio Labs’, ‘url‘ => ‘http://sensio.com’, ‘logo‘ => ‘/uploads/jobs/sensio-labs.gif’, ));

Symfony Functional Test

Page 13: BDD для PHP проектов

BDD

) =качественныйпродукт

+ (

разработка

unit-тесты

+ПОВЕДЕНИЕ

Page 14: BDD для PHP проектов

ТЕХНИКАКОММУНИКАЦИЙ

Page 15: BDD для PHP проектов

ИДЕЯ

# bowling_spec.rbrequire 'bowling'

describe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should == 0 endend

Page 16: BDD для PHP проектов

ИДЕЯ

# bowling_spec.rbrequire 'bowling'

describe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should == 0 endend

NO!

Page 17: BDD для PHP проектов

ИДЕЯХочу онлайн-боулинг

с блэкджеком и...

и он будетПЛАТНЫМ

Page 18: BDD для PHP проектов

КОММУНИКАЦИИ

Хочу кастомный Porsche

написание спек

разработка

идея

# bowling_spec.rbrequire 'bowling'

describe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should == 0 endend

Page 19: BDD для PHP проектов
Page 20: BDD для PHP проектов

ФУНКЦИОНАЛ

Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers

Scenario: Add two numbers Given I have entered 10 into the calculator And I have entered 5 into the calculator When I press ‘plus’ Then the result should be 15

Page 21: BDD для PHP проектов

ФУНКЦИОНАЛ

Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers

Scenario: Add two numbers Given I have entered 10 into the calculator And I have entered 5 into the calculator When I press ‘plus’ Then the result should be 15

Page 22: BDD для PHP проектов

СЦЕНАРИИ

Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers

Scenario: Add two numbers Given I have entered 10 into the calculator And I have entered 5 into the calculator When I press ‘plus’ Then the result should be 15

Page 23: BDD для PHP проектов

ШАГИ

Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers

Scenario: Add two numbers Given I have entered 10 into the calculator And I have entered 5 into the calculator When I press ‘plus’ Then the result should be 15

Page 24: BDD для PHP проектов

БИБЛИОТЕКИ

ASSERTIONS

RUNNER

BROWSER

Test::Unit, RSpec

Webrat, Capybara, Selenium

Autotest, RStakeout, Watchr

Page 25: BDD для PHP проектов

require 'calculator'

Given /I have entered (\d+) into the calculator/ do|n| @calc.push n.to_iend

When /I press (\w+)/ do |op| @result = @calc.send opend

Then /the resoulr should be (.*)/ do |result| @result.should == result.to_fend

ОПРЕДЕЛЕНИЯ

Page 26: BDD для PHP проектов

require 'calculator'

Given /I have entered (\d+) into the calculator/ do|n| @calc.push n.to_iend

When /I press (\w+)/ do |op| @result = @calc.send opend

Then /the resoulr should be (.*)/ do |result| @result.should == result.to_fend

ОПРЕДЕЛЕНИЯ

RubyПИШУТСЯ на

Page 27: BDD для PHP проектов

МИНУСЫ

1. Не все PHP-разработчики знают/хотят знать Ruby2. Сложность при описании входных условий

4. Скорость работы кросс-языкового решения3. Невозможность использования PHP библиотек

Page 28: BDD для PHP проектов
Page 29: BDD для PHP проектов

1. Написан с нуля на PHP5.32. Написан с применением Symfony Components

4. Полностью нативное решение на PHP3. Старается быть Cucumber’ом с входными/выходными данными

6. Поддерживает полную и17ю7. Полностью расширяем и настраиваем8. Поддерживает различные типы лоадеров

5. Столь же быстр, что и Cucumber (проверено)

Page 30: BDD для PHP проектов

Feature: Serve coffee In order to earn money Customers should be able to buy coffee at all times

Scenario: Buy last coffee Given there are 1 coffees left in the machine And I have deposited 1$ When I press the coffee button Then I should be served a coffee

ФУНКЦИОНАЛ

Page 31: BDD для PHP проектов

<?php

$steps->Then(‘/^it should (fail|pass)$/’, function($world, $status) { if (‘fail’ === $success) { assertNotEquals(0, $world->return); } else { assertEquals(0, $world->return); } });

ОПРЕДЕЛЕНИЯ

Page 32: BDD для PHP проектов

<?php

$steps->Then(‘/^it should (fail|pass)$/’, function($world, $status) { if (‘fail’ === $success) { assertNotEquals(0, $world->return); } else { assertEquals(0, $world->return); } });

ОПРЕДЕЛЕНИЯ

ТИП ОПРЕДЕЛЕНИЯ

Page 33: BDD для PHP проектов

<?php

$steps->Then(‘/^it should (fail|pass)$/’, function($world, $status) { if (‘fail’ === $success) { assertNotEquals(0, $world->return); } else { assertEquals(0, $world->return); } });

ОПРЕДЕЛЕНИЯ

РЕГУЛЯРНОЕ ВЫРАЖЕНИЕ

Page 34: BDD для PHP проектов

<?php

$steps->Then(‘/^it should (fail|pass)$/’, function($world, $status) { if (‘fail’ === $success) { assertNotEquals(0, $world->return); } else { assertEquals(0, $world->return); } });

CALLBACK

ОПРЕДЕЛЕНИЯ

Page 35: BDD для PHP проектов

<?php

$steps->Then(‘/^it should (fail|pass)$/’, function($world, $status) { ... });

Feature: Behat Console Runner Scenario: Run feature from CLI Given I have default Behat configuration When I call ‘behat -f progress’ Then it should pass

ИСПОЛНЕНИЕ

Page 36: BDD для PHP проектов

<?php

$steps->Then(‘/^it should (fail|pass)$/’, function($world, $status) { // $status == ‘pass’ });

Feature: Behat Console Runner Scenario: Run feature from CLI Given I have default Behat configuration When I call ‘behat -f progress’ Then it should pass

ИСПОЛНЕНИЕ

Page 37: BDD для PHP проектов

<?php

$steps->Then(‘/^it should pass$/’, function($world) { return true;});

$steps->Then(‘/^it passes$/’, function($world) { return false;});

$steps->Then(‘/^it should fail$/’, function($world) { throw new Exception();});

$steps->Then(‘/^it should pend$/’, function($world) { throw new Everzet\Behat\Exception\Pending();});

СТАТУСЫ

Page 38: BDD для PHP проектов

<?php

$steps->Then(‘/^it should pass$/’, function($world) { ...});

$steps->Then(‘/^it should pass$/’, function($world) { // Will throw exception on definitions read});

$steps->Then(‘/^it’s good$/’, function($world) { ...});

$steps->Then(‘/^it’s \w+$/’, function($world) { // Will throw exception on ‘it should pass call’});

СТАТУСЫ

Page 39: BDD для PHP проектов

ПРИМЕРИСПОЛЬЗОВАНИЯ

Page 40: BDD для PHP проектов

УСТАНОВКА

$> pear channel-discover pear.everzet.com$> pear install everzet/behat-beta

Page 41: BDD для PHP проектов

<?php # ./user.php

class User{ public function __construct($username, $age = 1) { }

public function getName() {}

public function getAge() {}}

HOW MUCH IS THE FISH?

Page 42: BDD для PHP проектов

# ./features/user.feature# language: ru

Функционал: Базовый Пользователь Чтобы работать с пользователями Как разработчик сайта Я хочу иметь доступ к пользовательской модели

Сценарий: Создание пользователя Допустим у нас нет пользователей Если мы добавим пользователя ‘everzet’ То у нас должно быть 1 пользователей И имя у первого пользователя ‘everzet’

FEATURE

Page 43: BDD для PHP проектов

GET SNIPPETS

Page 44: BDD для PHP проектов

<?php # ./features/steps/user_steps.php

require_once ‘PHPUnit/Autoload.php’;require_once ‘PHPUnit/Framework/Assert/Functions.php’;require_once __DIR__ . ‘/../../user.php’;

$steps->

Допустим(‘/^у нас нет пользователей$/’, function($world) { $world->users = array();})->

Если(‘/^мы добавим пользователя \’([^\’]+)\’$/’, function($world, $username) { $world->users[] = new User($username);})->

To(‘/^у нас должно быть (\d+) пользователей$/’, function($world, $count) { assertEquals($count, count($world->users));})->

То(‘/^имя у первого пользователя \’([^\’]+)\’$/’, function($world, $username) { assertEquals($username, $world->users[0]->getName());});

ОПРЕДЕЛЕНИЯ

Page 45: BDD для PHP проектов

WATCH IT FAILS

Page 46: BDD для PHP проектов

РЕАЛИЗАЦИЯ<?php # ./user.php

class User{ protected $name, $age;

public function __construct($username, $age = 1) { $this->name = $username; $this->age = $age; }

public function getName() { return $this->name; }

public function getAge() { return $this->age; }}

Page 47: BDD для PHP проектов

WATCH IT PASSES

Page 48: BDD для PHP проектов

1. CommonWebSteps2. Annotated class step definitions and hooks

4. PDF/HTML formatter3. JUnit formatter

5. Selenium integration

TODO

Page 49: BDD для PHP проектов

ССЫЛКИ

ОФФИЦИАЛЬНЫЙ САЙТhttp://everzet.com/Behat

ОФФИЦИАЛЬНЫЙ РЕПОЗИТОРИЙhttp://github.com/everzet/Behat

ОФФИЦИАЛЬНАЯ GOOGLE GROUPhttp://groups.google.com/group/behat

Page 50: BDD для PHP проектов

ВОПРОСЫ?