Skip to content

Instantly share code, notes, and snippets.

@Ilyes512
Forked from ramsey/Bar.php
Created October 3, 2018 20:24
Show Gist options
  • Select an option

  • Save Ilyes512/a28ec9ad2feb863e87ebff590a8ebb3a to your computer and use it in GitHub Desktop.

Select an option

Save Ilyes512/a28ec9ad2feb863e87ebff590a8ebb3a to your computer and use it in GitHub Desktop.

Revisions

  1. @ramsey ramsey revised this gist Jan 15, 2016. No changes.
  2. @ramsey ramsey created this gist Jan 15, 2016.
    12 changes: 12 additions & 0 deletions Bar.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <?php
    namespace Ramsey\Talks;

    class Bar
    {
    public function getSomething(Foo $foo, $arg1, $arg2)
    {
    $result = $foo->bar()->baz($arg1)->qux()->quux($arg2);

    return "Now, we're {$result}";
    }
    }
    44 changes: 44 additions & 0 deletions FluentTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <?php
    namespace Ramsey\Talks\Test;

    class FluentTest extends \PHPUnit_Framework_TestCase
    {
    public function tearDown()
    {
    \Mockery::close();
    }

    public function testFluentInterfacesWithParameterAtEndOfChain()
    {
    $mock = \Mockery::mock('Ramsey\\Talks\\Foo');
    $mock->shouldReceive('bar->baz->qux->quux')
    ->with(123)
    ->andReturn('awesome!');

    $mock->shouldReceive('bar->baz->qux->quux')
    ->with(321)
    ->andReturn('cool!');

    $bar = new \Ramsey\Talks\Bar;

    $this->assertEquals("Now, we're awesome!", $bar->getSomething($mock, null, 123));
    $this->assertEquals("Now, we're cool!", $bar->getSomething($mock, null, 321));
    }

    public function testFluentInterfacesWithInterveningParameters()
    {
    $baz = \Mockery::mock('baz');
    $baz->shouldReceive('qux->quux')
    ->with(321)
    ->andReturn('rad!');

    $mock = \Mockery::mock('Ramsey\\Talks\\Foo');
    $mock->shouldReceive('bar->baz')
    ->with(123)
    ->andReturn($baz);

    $bar = new \Ramsey\Talks\Bar;

    $this->assertEquals("Now, we're rad!", $bar->getSomething($mock, 123, 321));
    }
    }