1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?php declare(strict_types = 1);
namespace DesignPatterns\Structural\Flyweight\Tests;
use DesignPatterns\Structural\Flyweight\TextFactory; use PHPUnit\Framework\TestCase;
class FlyweightTest extends TestCase { private array $characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
private array $fonts = ['Arial', 'Times New Roman', 'Verdana', 'Helvetica'];
public function testFlyweight() { $factory = new TextFactory();
for ($i = 0; $i <= 10; $i++) { foreach ($this->characters as $char) { foreach ($this->fonts as $font) { $flyweight = $factory->get($char); $rendered = $flyweight->render($font);
$this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered); } } }
foreach ($this->fonts as $word) { $flyweight = $factory->get($word); $rendered = $flyweight->render('foobar');
$this->assertSame(sprintf('Word %s with font foobar', $word), $rendered); }
$this->assertCount(count($this->characters) + count($this->fonts), $factory); } }
|