mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 04:00:18 +02:00
#232 removed Iterator classes
This commit is contained in:
@@ -9,66 +9,68 @@ use DesignPatterns\Behavioral\Iterator\BookListReverseIterator;
|
||||
|
||||
class IteratorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var BookList
|
||||
*/
|
||||
protected $bookList;
|
||||
|
||||
protected function setUp()
|
||||
public function testCanIterateOverBookList()
|
||||
{
|
||||
$this->bookList = new BookList();
|
||||
$this->bookList->addBook(new Book('Learning PHP Design Patterns', 'William Sanders'));
|
||||
$this->bookList->addBook(new Book('Professional Php Design Patterns', 'Aaron Saray'));
|
||||
$this->bookList->addBook(new Book('Clean Code', 'Robert C. Martin'));
|
||||
}
|
||||
$bookList = new BookList();
|
||||
$bookList->addBook(new Book('Learning PHP Design Patterns', 'William Sanders'));
|
||||
$bookList->addBook(new Book('Professional Php Design Patterns', 'Aaron Saray'));
|
||||
$bookList->addBook(new Book('Clean Code', 'Robert C. Martin'));
|
||||
|
||||
public function expectedAuthors()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'Learning PHP Design Patterns by William Sanders',
|
||||
'Professional Php Design Patterns by Aaron Saray',
|
||||
'Clean Code by Robert C. Martin',
|
||||
),
|
||||
),
|
||||
$books = [];
|
||||
|
||||
foreach ($bookList as $book) {
|
||||
$books[] = $book->getAuthorAndTitle();
|
||||
}
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'Learning PHP Design Patterns by William Sanders',
|
||||
'Professional Php Design Patterns by Aaron Saray',
|
||||
'Clean Code by Robert C. Martin',
|
||||
],
|
||||
$books
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider expectedAuthors
|
||||
*/
|
||||
public function testUseAIteratorAndValidateAuthors($expected)
|
||||
public function testCanIterateOverBookListAfterRemovingBook()
|
||||
{
|
||||
$iterator = new BookListIterator($this->bookList);
|
||||
$book = new Book('Clean Code', 'Robert C. Martin');
|
||||
$book2 = new Book('Professional Php Design Patterns', 'Aaron Saray');
|
||||
|
||||
while ($iterator->valid()) {
|
||||
$expectedBook = array_shift($expected);
|
||||
$this->assertEquals($expectedBook, $iterator->current()->getAuthorAndTitle());
|
||||
$iterator->next();
|
||||
$bookList = new BookList();
|
||||
$bookList->addBook($book);
|
||||
$bookList->addBook($book2);
|
||||
$bookList->removeBook($book);
|
||||
|
||||
$books = [];
|
||||
foreach ($bookList as $book) {
|
||||
$books[] = $book->getAuthorAndTitle();
|
||||
}
|
||||
|
||||
$this->assertEquals(
|
||||
['Professional Php Design Patterns by Aaron Saray'],
|
||||
$books
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider expectedAuthors
|
||||
*/
|
||||
public function testUseAReverseIteratorAndValidateAuthors($expected)
|
||||
public function testCanAddBookToList()
|
||||
{
|
||||
$iterator = new BookListReverseIterator($this->bookList);
|
||||
$book = new Book('Clean Code', 'Robert C. Martin');
|
||||
|
||||
while ($iterator->valid()) {
|
||||
$expectedBook = array_pop($expected);
|
||||
$this->assertEquals($expectedBook, $iterator->current()->getAuthorAndTitle());
|
||||
$iterator->next();
|
||||
}
|
||||
$bookList = new BookList();
|
||||
$bookList->addBook($book);
|
||||
|
||||
$this->assertCount(1, $bookList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test BookList Remove.
|
||||
*/
|
||||
public function testBookRemove()
|
||||
public function testCanRemoveBookFromList()
|
||||
{
|
||||
$this->bookList->removeBook($this->bookList->getBook(0));
|
||||
$this->assertEquals($this->bookList->count(), 2);
|
||||
$book = new Book('Clean Code', 'Robert C. Martin');
|
||||
|
||||
$bookList = new BookList();
|
||||
$bookList->addBook($book);
|
||||
$bookList->removeBook($book);
|
||||
|
||||
$this->assertCount(0, $bookList);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user