From 975979b9a2334eb1ef81ace4efb3b231344ff760 Mon Sep 17 00:00:00 2001 From: mwjames Date: Mon, 1 Aug 2016 17:44:57 +0200 Subject: [PATCH] Add MessageReporterAware and SpyMessageReporter --- README.md | 68 ++++++++++++------------ src/MessageReporterAware.php | 23 ++++++++ src/MessageReporterFactory.php | 9 ++++ src/SpyMessageReporter.php | 52 ++++++++++++++++++ tests/phpunit/SpyMessageReporterTest.php | 60 +++++++++++++++++++++ 5 files changed, 179 insertions(+), 33 deletions(-) create mode 100644 src/MessageReporterAware.php create mode 100644 src/SpyMessageReporter.php create mode 100644 tests/phpunit/SpyMessageReporterTest.php diff --git a/README.md b/README.md index 862fb30..5dfc949 100644 --- a/README.md +++ b/README.md @@ -32,57 +32,56 @@ The message reporter specifies `MessageReporter` as an interface for all interac - `MessageReporterFactory` - `ObservableMessageReporter` - `NullMessageReporter` +- `MessageReporterAware` +- `SpyMessageReporter` ```php -use Onoi\MessageReporter\MessageReporterFactory -use Onoi\MessageReporter\MessageReporter +use Onoi\MessageReporter\MessageReporterFactory; +use Onoi\MessageReporter\MessageReporterAware; +use Onoi\MessageReporter\MessageReporter; - class Bar { +class Bar implements MessageReporterAware { - private $messageReporter; + private $messageReporter; - public function __construct() { - $this->messageReporter = MessageReporterFactory::getInstance()->newNullMessageReporter(); - } + public function __construct() { + $this->messageReporter = MessageReporterFactory::getInstance()->newNullMessageReporter(); + } - public function setMessageReporter( MessageReporter $messageReporter ) { - $this->messageReporter = $messageReporter; - } + public function setMessageReporter( MessageReporter $messageReporter ) { + $this->messageReporter = $messageReporter; + } - public function doSomethingElse() { - $this->messageReporter->reportMessage( 'Now ...' ); - } + public function doSomething() { + $this->messageReporter->reportMessage( 'Doing ...' ); } +} ``` ```php -use Onoi\MessageReporter\MessageReporterFactory -use Onoi\MessageReporter\MessageReporter - - class Foo implements MessageReporter { +use Onoi\MessageReporter\MessageReporterFactory; +use Onoi\MessageReporter\MessageReporter; - public function doSomething( Bar $bar ) { +class Foo implements MessageReporter { - $messageReporterFactory = new MessageReporterFactory(); + public function reportMessage( $message ) { + // output + } +} - $observableMessageReporter = $messageReporterFactory->newObservableMessageReporter(); - $observableMessageReporter->registerReporterCallback( array( $this, 'reportMessage' ) ); +$foo = new Foo(); - or +$messageReporterFactory = new MessageReporterFactory(); - // If the class implements the MessageReporter - $observableMessageReporter->registerMessageReporter( $this ); +$observableMessageReporter = $messageReporterFactory->newObservableMessageReporter(); +$observableMessageReporter->registerReporterCallback( array( $foo, 'reportMessage' ) ); - $bar->setMessageReporter( $observableMessageReporter ); - $bar->doSomethingElse(); - } +or - public function reportMessage( $message ) { - // output - } - } +// If the class implements the MessageReporter +$observableMessageReporter->registerMessageReporter( $foo ); - $instance = new Foo(); - $instance->doSomething( new Bar() ); +$bar = new Bar(); +$bar->setMessageReporter( $observableMessageReporter ); ``` ## Contribution and support @@ -99,6 +98,9 @@ The library provides unit tests that covers the core-functionality normally run ### Release notes +* 1.2.0 (2016-08-02) + - Added `MessageReporterAware` and `SpyMessageReporter` + * 1.1.0 (2016-04-13) - `ObservableMessageReporter::registerReporterCallback` to register only callable handlers diff --git a/src/MessageReporterAware.php b/src/MessageReporterAware.php new file mode 100644 index 0000000..2361a18 --- /dev/null +++ b/src/MessageReporterAware.php @@ -0,0 +1,23 @@ +messages[] = $message; + } + + /** + * @since 1.2 + * + * @return array + */ + public function getMessages() { + return $this->messages; + } + + /** + * @since 1.2 + * + * @return string + */ + public function getMessagesAsString() { + return implode( ', ', $this->messages ); + } + + /** + * @since 1.2 + */ + public function clearMessages() { + $this->messages = array(); + } + +} diff --git a/tests/phpunit/SpyMessageReporterTest.php b/tests/phpunit/SpyMessageReporterTest.php new file mode 100644 index 0000000..fb5088c --- /dev/null +++ b/tests/phpunit/SpyMessageReporterTest.php @@ -0,0 +1,60 @@ +assertInstanceOf( + '\Onoi\MessageReporter\SpyMessageReporter', + new SpyMessageReporter() + ); + } + + public function testSpyOnReportedMessages() { + + $instance = new SpyMessageReporter(); + $instance->reportMessage( 'foo' ); + + $this->assertEquals( + array( 'foo' ), + $instance->getMessages() + ); + + $instance->reportMessage( 'Bar' ); + + $this->assertEquals( + 'foo, Bar', + $instance->getMessagesAsString() + ); + } + + public function testClearMessages() { + + $instance = new SpyMessageReporter(); + $instance->reportMessage( 'foo' ); + + $this->assertNotEmpty( + $instance->getMessages() + ); + + $instance->clearMessages(); + + $this->assertEmpty( + $instance->getMessages() + ); + } + +}