Top Banner
Proposal for xSpec BDD Framework for PHP Beyond PHPUnit and PHPSpec @yuya_takeyama
53

Proposal for xSpep BDD Framework for PHP

Jan 13, 2015

Download

Technology

Yuya Takeyama

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Proposal for xSpep BDD Framework for PHP

Proposal for xSpec BDD Framework

for PHPBeyond PHPUnit and PHPSpec

@yuya_takeyama

Page 2: Proposal for xSpep BDD Framework for PHP

Recent Activities•Making DSL with [] (#phpcon2011 LT)

http://blog.yuyat.jp/archives/1355

• PHPUnit でよりよくテストを書くためにhttp://blog.yuyat.jp/archives/1386

• phpenv で複数の PHP 環境を管理するhttp://blog.yuyat.jp/archives/1446

• Building Development Environmentwith php-build and phpenvhttp://blog.yuyat.jp/archives/1461

•例えば, Singleton を避ける (New! #TddAdventJp)

http://blog.yuyat.jp/archives/1500

Page 3: Proposal for xSpep BDD Framework for PHP

xSpec?

Page 4: Proposal for xSpep BDD Framework for PHP

xUnit?

Page 5: Proposal for xSpep BDD Framework for PHP

xUnit (Unit Testing Frameworks)

•SUnit (Smalltalk)

•JUnit (Java)•Test::Unit (Ruby)•PHPUnit (PHP)

Page 6: Proposal for xSpep BDD Framework for PHP
Page 7: Proposal for xSpep BDD Framework for PHP

xSpec?reprise

Page 8: Proposal for xSpep BDD Framework for PHP

xSpec (Testing Frameworks for BDD)

•RSpec (Ruby)•Jasmine (JavaScript)

•specs (Scala)•PHPSpec (PHP)

Page 9: Proposal for xSpep BDD Framework for PHP

BDD?

Page 10: Proposal for xSpep BDD Framework for PHP

BDD (Behaviour Driven Development)

•Not test, specify!•“It should ...”•Expressive•Test as Documentation

Page 11: Proposal for xSpep BDD Framework for PHP

RSpec exampledescribe Bowling do  describe '#score' do    context 'all gutter game' do      before do        @bowling = Bowling.new        20.times { @bowling.hit(0) }      end

      it 'should equal 0' do        @bowling.score.should eq(0)      end    end  endend

Page 12: Proposal for xSpep BDD Framework for PHP

RSpec exampledescribe Bowling do  describe '#score' do    context 'all gutter game' do      before do        @bowling = Bowling.new        20.times { @bowling.hit(0) }      end

      it 'should equal 0' do        @bowling.score.should eq(0)      end    end  endend Not Test, Specify

Page 13: Proposal for xSpep BDD Framework for PHP

RSpec exampledescribe Bowling do  describe '#score' do    context 'all gutter game' do      before do        @bowling = Bowling.new        20.times { @bowling.hit(0) }      end

      it 'should equal 0' do        @bowling.score.should eq(0)      end    end  endend “It should ...”

Page 14: Proposal for xSpep BDD Framework for PHP

RSpec exampledescribe Bowling do  describe '#score' do    context 'all gutter game' do      before do        @bowling = Bowling.new        20.times { @bowling.hit(0) }      end

      it 'should equal 0' do        @bowling.score.should eq(0)      end    end  endend Expressive

Page 15: Proposal for xSpep BDD Framework for PHP

RSpec exampledescribe Bowling do  describe '#score' do    context 'all gutter game' do      before do        @bowling = Bowling.new        20.times { @bowling.hit(0) }      end

      it 'should equal 0' do        @bowling.score.should eq(0)      end    end  endend

Test as Documentation

Page 16: Proposal for xSpep BDD Framework for PHP

I ❤RSpec

Page 17: Proposal for xSpep BDD Framework for PHP
Page 18: Proposal for xSpep BDD Framework for PHP

PHPUnit exampleclass BowlingTest extends PHPUnit_Framwork_TestCase{    private $_bowling;

    public function setUp()    {        $this->_bowling = new Bowling;    }

    public function testScore0ForAllGutterGame()    {        for ($i = 0; $i <= 20; $i++) {            $this->_bowling->hit(0);        }        $this->assertEquals(0, $this->_bowling->score);    }}

Page 19: Proposal for xSpep BDD Framework for PHP

PHPSpec exampleclass DescribeBowling extends \PHPSpec\Context{    private $_bowling;

    public function before()    {        $this->_bowling = $this->spec(new Bowling);    }

    public function itShouldScore0ForAllGutterGame()    {        for ($i=1; $i<=20; $i++) {            $this->_bowling->hit(0);        }        $this->_bowling->score->should->equal(0);    }}

Page 20: Proposal for xSpep BDD Framework for PHP

PHPSpec exampleclass DescribeBowling extends \PHPSpec\Context{    private $_bowling;

    public function before()    {        $this->_bowling = $this->spec(new Bowling);    }

    public function itShouldScore0ForAllGutterGame()    {        for ($i=1; $i<=20; $i++) {            $this->_bowling->hit(0);        }        $this->_bowling->score->should->equal(0);    }} Not Test, Specify

Page 21: Proposal for xSpep BDD Framework for PHP

PHPSpec exampleclass DescribeBowling extends \PHPSpec\Context{    private $_bowling;

    public function before()    {        $this->_bowling = $this->spec(new Bowling);    }

    public function itShouldScore0ForAllGutterGame()    {        for ($i=1; $i<=20; $i++) {            $this->_bowling->hit(0);        }        $this->_bowling->score->should->equal(0);    }} “It should ...”

Page 22: Proposal for xSpep BDD Framework for PHP

PHPSpec exampleclass DescribeBowling extends \PHPSpec\Context{    private $_bowling;

    public function before()    {        $this->_bowling = $this->spec(new Bowling);    }

    public function itShouldScore0ForAllGutterGame()    {        for ($i=1; $i<=20; $i++) {            $this->_bowling->hit(0);        }        $this->_bowling->score->should->equal(0);    }} Expressive

Page 23: Proposal for xSpep BDD Framework for PHP

PHPSpec exampleclass DescribeBowling extends \PHPSpec\Context{    private $_bowling;

    public function before()    {        $this->_bowling = $this->spec(new Bowling);    }

    public function itShouldScore0ForAllGutterGame()    {        for ($i=1; $i<=20; $i++) {            $this->_bowling->hit(0);        }        $this->_bowling->score->should->equal(0);    }}

Test as Documentation

Page 24: Proposal for xSpep BDD Framework for PHP

I ❤PHPSpec

Page 25: Proposal for xSpep BDD Framework for PHP

I ❤PHPUnit

Page 26: Proposal for xSpep BDD Framework for PHP

But...

Page 27: Proposal for xSpep BDD Framework for PHP

Issue of PHPUnit/PHPSpec

•No nested context•Specification with camelCase or lower_case_with_underscore

Page 28: Proposal for xSpep BDD Framework for PHP

Issue of PHPUnit/PHPSpec

•No nested context•Specification with camelCase or lower_case_with_underscore

I want moreexpressiveness!

Page 29: Proposal for xSpep BDD Framework for PHP

Isolved

Page 30: Proposal for xSpep BDD Framework for PHP

Speciphy

•Nested context•Specification with string•PHPSpec matchers

https://github.com/yuya-takeyama/Speciphy

Page 31: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

Page 32: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

Not Test, Specify

Page 33: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

“It should...”

Page 34: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

Expressive

Page 35: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

Test as Documentation?

Page 36: Proposal for xSpep BDD Framework for PHP

Proposal #1Use functionin namespace,Not methodin class

Page 37: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

\Speciphy\DSL\describe

Page 38: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

\Speciphy\DSL\context

Page 39: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

\Speciphy\DSL\it

Page 40: Proposal for xSpep BDD Framework for PHP

Proposal #2

Use arrayto build

nested context.

Page 41: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

Nested context

Page 42: Proposal for xSpep BDD Framework for PHP

Array,A Strange

Data Structure

Page 43: Proposal for xSpep BDD Framework for PHP

[ "foo", "bar", "hoge" => "piyo"]

Page 44: Proposal for xSpep BDD Framework for PHP

array(3) { [0]=> string(3) "foo" [1]=> string(3) "bar" ["hoge"]=> string(4) "piyo"}

Page 45: Proposal for xSpep BDD Framework for PHP

Likekeywordarguments

Page 46: Proposal for xSpep BDD Framework for PHP

public $id = [ 'int', 'primary' => true, 'serial' => true];public $name = [ 'string', 'required' => true, 'unique' => true];public $birthday = ['date'];

Model Definition

Page 47: Proposal for xSpep BDD Framework for PHP

["div",  ["ul",    ["li", "Foo"]    ["li", "Bar"]    ["li", "Baz"]]]

Pamlhttps://github.com/yuya-takeyama/php-HTML_Paml

Page 48: Proposal for xSpep BDD Framework for PHP

<div>  <ul>    <li>Foo</li>    <li>Bar</li>    <li>Baz</li>  </ul></div>

HTML Generation

Page 49: Proposal for xSpep BDD Framework for PHP

['begin', ['define', 'fib', ['lambda', ['x'], ['if', ['<', ':x', 2], ':x', ['+', ['fib', ['-', ':x', 2]], ['fib', ['-', ':x', 1]]]]]], ['print', 'fib(10) = '], ['println', ['fib', 10]]] => fib(10) = 55

LisPHPhttps://github.com/yuya-takeyama/LisPHP

Page 50: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

$arr[‘subject’]

Page 51: Proposal for xSpep BDD Framework for PHP

Speciphy examplenamespace Speciphy\DSL;

return describe('Bowling', array(    describe('->score', array(        context('all gutter game', array(            'subject' => function () {                $bowling = new Bowling;                for ($i = 1; $i <= 20; $i++) {                    $bowling->hit(0);                }                return $bowling;            },

            it('should equal 0', function ($bowling) {                $bowling->score->should->equal(0);            }),        )),    )),));

$arr[0]

Page 52: Proposal for xSpep BDD Framework for PHP

Conclusion•Use function in namespace,Not method in class

•Use array to build nested structure

•Enjoy BDD with xSpec

Page 53: Proposal for xSpep BDD Framework for PHP

Questions?or

Claims?