Skip to content

Instantly share code, notes, and snippets.

@recca0120
Last active June 14, 2017 04:07
Show Gist options
  • Save recca0120/da983e9903bb6d7435060794b1d3f99b to your computer and use it in GitHub Desktop.
Save recca0120/da983e9903bb6d7435060794b1d3f99b to your computer and use it in GitHub Desktop.
Flunt API
<?php
class Fluent
{
protected $messages = [];
public function hello()
{
$this->messages[] = 'hello';
return $this;
}
public function world()
{
$this->messages[] = 'world';
return $this;
}
public function say()
{
return implode(' ', $this->messages);
}
}
<?php
use PHPUnit\Framework\TestCase;
class FluentTest extends TestCase
{
public function test_fluent_api()
{
$fluent = (new TesterFluent())
->hello()
->assert(function ($messages) {
$this->assertSame([
'hello',
], $messages);
})
->world()
->assert(function ($messages) {
$this->assertSame([
'hello',
'world',
], $messages);
});
$this->assertSame('hello world', $fluent->say());
}
}
class FluentTest2 extends TestCase
{
public function test_fluent_api()
{
$fluent = new Fluent();
$fluent->hello();
$this->assertSame('hello', $fluent->say());
$fluent->world();
$this->assertSame('hello world', $fluent->say());
}
}
class FluentTest3 extends TestCase
{
public function test_hello()
{
$fluent = new Fluent();
$fluent->hello();
$this->assertSame('hello', $fluent->say());
}
public function test_world()
{
$fluent = new Fluent();
$fluent->world();
$this->assertSame('world', $fluent->say());
}
public function test_hello_world()
{
$fluent = new Fluent();
$fluent
->hello()
->world();
$this->assertSame('hello world', $fluent->say());
}
public function test_world_hello()
{
$fluent = new Fluent();
$fluent
->world()
->hello();
$this->assertSame('world hello', $fluent->say());
}
}
class TesterFluent extends Fluent
{
public function assert(Closure $closure)
{
$closure($this->messages);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment