design-patterns/tests/ObserverTest.php

45 lines
1.1 KiB
PHP

<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-06
* @time: 13:31
*/
declare(strict_types = 1);
namespace Tests;
use Pattern\Behavioral\Observer\User;
use Pattern\Behavioral\Observer\UserObserver;
use PHPUnit\Framework\TestCase;
class ObserverTest extends TestCase
{
public function testObserver(): void
{
$user = new User();
/** These names will be skipped and not be added to the log of names */
$user->setName("Just a name");
$user->setName("Some new name");
$userObserver = new UserObserver();
$user->attach($userObserver);
/** After creating a new instance of UserObserver::class
* and attached it to User::class instance,
* these names will be added to the log of names */
$user->setName("Another new name");
$user->setName("Once again new name");
$user->setName("Possibly other new name");
$this->assertEquals(
["Another new name", "Once again new name", "Possibly other new name"],
$userObserver->getNamesLog()
);
}
}