mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-06-08 06:55:10 +02:00
78 lines
2.1 KiB
PHP
78 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Behavioral\Iterator\Tests;
|
|
|
|
use DesignPatterns\Behavioral\Iterator\Book;
|
|
use DesignPatterns\Behavioral\Iterator\BookList;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class IteratorTest extends TestCase
|
|
{
|
|
public function testCanIterateOverBookList()
|
|
{
|
|
$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'));
|
|
|
|
$books = [];
|
|
|
|
foreach ($bookList as $book) {
|
|
$books[] = $book->getAuthorAndTitle();
|
|
}
|
|
|
|
$this->assertSame(
|
|
[
|
|
'Learning PHP Design Patterns by William Sanders',
|
|
'Professional Php Design Patterns by Aaron Saray',
|
|
'Clean Code by Robert C. Martin',
|
|
],
|
|
$books
|
|
);
|
|
}
|
|
|
|
public function testCanIterateOverBookListAfterRemovingBook()
|
|
{
|
|
$book = new Book('Clean Code', 'Robert C. Martin');
|
|
$book2 = new Book('Professional Php Design Patterns', 'Aaron Saray');
|
|
|
|
$bookList = new BookList();
|
|
$bookList->addBook($book);
|
|
$bookList->addBook($book2);
|
|
$bookList->removeBook($book);
|
|
|
|
$books = [];
|
|
foreach ($bookList as $book) {
|
|
$books[] = $book->getAuthorAndTitle();
|
|
}
|
|
|
|
$this->assertSame(
|
|
['Professional Php Design Patterns by Aaron Saray'],
|
|
$books
|
|
);
|
|
}
|
|
|
|
public function testCanAddBookToList()
|
|
{
|
|
$book = new Book('Clean Code', 'Robert C. Martin');
|
|
|
|
$bookList = new BookList();
|
|
$bookList->addBook($book);
|
|
|
|
$this->assertCount(1, $bookList);
|
|
}
|
|
|
|
public function testCanRemoveBookFromList()
|
|
{
|
|
$book = new Book('Clean Code', 'Robert C. Martin');
|
|
|
|
$bookList = new BookList();
|
|
$bookList->addBook($book);
|
|
$bookList->removeBook($book);
|
|
|
|
$this->assertCount(0, $bookList);
|
|
}
|
|
}
|