mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-17 21:31:15 +02:00
Merge remote-tracking branch 'Trismegiste/unit-test-for-observer'
This commit is contained in:
@@ -1,30 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DesignPatterns;
|
namespace DesignPatterns\Observer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Observer pattern
|
* Observer pattern : The observed object (the subject)
|
||||||
*
|
*
|
||||||
* Purpose:
|
* The subject maintains a list of Observers and sends notifications.
|
||||||
* to implement a publish/subscribe behaviour to an object, whenever a "Subject" object changes it's state, the attached
|
|
||||||
* "Observers" will be notified. It is used to shorten the amount of coupled objects and uses loose coupling instead
|
|
||||||
*
|
|
||||||
* Examples:
|
|
||||||
* - a message queue system is observed to show the progress of a job in a GUI
|
|
||||||
*
|
|
||||||
* PHP already defines two interfaces that can help to implement this pattern: SplObserver and SplSubject
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class UserObserver implements \SplObserver
|
|
||||||
{
|
|
||||||
public function update(\SplSubject $subject)
|
|
||||||
{
|
|
||||||
echo get_class($subject) . ' has been updated';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class User implements \SplSubject
|
class User implements \SplSubject
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $_data = array();
|
protected $_data = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,9 +72,6 @@ class User implements \SplSubject
|
|||||||
// notify the observers, that user has been updated
|
// notify the observers, that user has been updated
|
||||||
$this->notify();
|
$this->notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = new User();
|
|
||||||
$user->attach(new UserObserver());
|
|
||||||
|
|
||||||
$user->notify();
|
|
32
Observer/UserObserver.php
Normal file
32
Observer/UserObserver.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Observer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observer pattern
|
||||||
|
*
|
||||||
|
* Purpose:
|
||||||
|
* to implement a publish/subscribe behaviour to an object, whenever a "Subject" object changes it's state, the attached
|
||||||
|
* "Observers" will be notified. It is used to shorten the amount of coupled objects and uses loose coupling instead
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* - a message queue system is observed to show the progress of a job in a GUI
|
||||||
|
*
|
||||||
|
* PHP already defines two interfaces that can help to implement this pattern: SplObserver and SplSubject
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class UserObserver implements \SplObserver
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the only method to implement as an observer.
|
||||||
|
* It is called by the Subject (usually by SplSubject::notify() )
|
||||||
|
*
|
||||||
|
* @param \SplSubject $subject
|
||||||
|
*/
|
||||||
|
public function update(\SplSubject $subject)
|
||||||
|
{
|
||||||
|
echo get_class($subject) . ' has been updated';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
62
Tests/Observer/ObserverTest.php
Normal file
62
Tests/Observer/ObserverTest.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Tests\Observer;
|
||||||
|
|
||||||
|
use DesignPatterns\Observer\UserObserver;
|
||||||
|
use DesignPatterns\Observer\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ObserverTest tests the Observer pattern
|
||||||
|
*/
|
||||||
|
class ObserverTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $observer;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->observer = new UserObserver();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the notification
|
||||||
|
*/
|
||||||
|
public function testNotify()
|
||||||
|
{
|
||||||
|
$this->expectOutputString('DesignPatterns\Observer\User has been updated');
|
||||||
|
$subject = new User();
|
||||||
|
|
||||||
|
$subject->attach($this->observer);
|
||||||
|
$subject->property = 123;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the subscribing
|
||||||
|
*/
|
||||||
|
public function testAttachDetach()
|
||||||
|
{
|
||||||
|
$subject = new User();
|
||||||
|
$this->assertAttributeEmpty('_observers', $subject);
|
||||||
|
$subject->attach($this->observer);
|
||||||
|
$this->assertAttributeNotEmpty('_observers', $subject);
|
||||||
|
$subject->detach($this->observer);
|
||||||
|
$this->assertAttributeEmpty('_observers', $subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the update() invocation on a mockup
|
||||||
|
*/
|
||||||
|
public function testUpdateCalling()
|
||||||
|
{
|
||||||
|
$subject = new User();
|
||||||
|
$observer = $this->getMock('SplObserver');
|
||||||
|
$subject->attach($observer);
|
||||||
|
|
||||||
|
$observer->expects($this->once())
|
||||||
|
->method('update')
|
||||||
|
->with($subject);
|
||||||
|
|
||||||
|
$subject->notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user