Last active
December 24, 2015 03:59
-
-
Save glenjamin/6741033 to your computer and use it in GitHub Desktop.
Shared behaviours in PHPUnit with Traits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example; | |
interface Iterable { | |
/** | |
* @return Iterator iterator over all items | |
*/ | |
public function items(); | |
/** | |
* @return int number of items in collection | |
*/ | |
public function length(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example\Single; | |
class AppleList | |
{ | |
private $data = []; | |
private $sort = []; | |
public function __construct(array $data, array $sort) | |
{ | |
$this->data = $data; | |
$this->sort = $sort; | |
} | |
public function items() | |
{ | |
foreach ($this->sort as $key) { | |
yield new Apple($this->data[$key]); | |
} | |
} | |
public function length() | |
{ | |
return count($this->data); | |
} | |
} | |
class Apple | |
{ | |
private $id; | |
public function __construct(array $data) { | |
$this->id = $data['id']; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example\Single; | |
require __DIR__ . '/Single_AppleList.php'; | |
class AppleListTest extends \PHPUnit_Framework_TestCase | |
{ | |
public function test_empty_collection_has_zero_length() | |
{ | |
$collection = $this->createAppleList(); | |
$this->assertEquals(0, $collection->length()); | |
} | |
public function test_length_reports_collection_length() | |
{ | |
$collection = $this->createAppleList( | |
[ | |
$this->createAppleListItem(), | |
$this->createAppleListItem(), | |
$this->createAppleListItem(), | |
] | |
); | |
$this->assertEquals(3, $collection->length()); | |
} | |
public function test_empty_collection_items_doesnt_iterate() | |
{ | |
$collection = $this->createAppleList(); | |
foreach ($collection->items() as $item) { | |
$this->fail('Did not expect items() to iterate'); | |
} | |
} | |
public function test_collection_items_have_correct_type() | |
{ | |
$collection = $this->createAppleList( | |
[ | |
$this->createAppleListItem(), | |
$this->createAppleListItem(), | |
$this->createAppleListItem(), | |
] | |
); | |
foreach ($collection->items() as $item) { | |
$this->assertInstanceOf('Example\\Single\\Apple', $item); | |
} | |
} | |
public function test_collection_items_iterates_in_order() | |
{ | |
$collection = $this->createAppleList( | |
[ | |
1 => $this->createAppleListItem(1), | |
2 => $this->createAppleListItem(2), | |
3 => $this->createAppleListItem(3), | |
], | |
$order = [2, 3, 1] | |
); | |
$i = 0; | |
foreach ($collection->items() as $item) { | |
$this->assertEquals($order[$i++], $item->getId()); | |
} | |
} | |
protected function createAppleList(array $data=[ ], array $order=[ ]) | |
{ | |
if (empty($order)) { | |
$order = array_keys($data); | |
} | |
return new AppleList($data, $order); | |
} | |
protected function createAppleListItem($id = 0) | |
{ | |
return ['id' => $id]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment