Skip to content

Commit

Permalink
Add MessageReporterAware and SpyMessageReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Aug 2, 2016
1 parent 910bf7e commit 975979b
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 33 deletions.
68 changes: 35 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
23 changes: 23 additions & 0 deletions src/MessageReporterAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Onoi\MessageReporter;

/**
* @license GNU GPL v2+
* @since 1.2
*
* @author mwjames
*/
interface MessageReporterAware {

/**
* Allows to inject a MessageReporter and make an object aware of its
* existence.
*
* @since 1.2
*
* @param MessageReporter $messageReporter
*/
public function setMessageReporter( MessageReporter $messageReporter );

}
9 changes: 9 additions & 0 deletions src/MessageReporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ public function newObservableMessageReporter() {
return new ObservableMessageReporter();
}

/**
* @since 1.2
*
* @return SpyMessageReporter
*/
public function newSpyMessageReporter() {
return new SpyMessageReporter();
}

}
52 changes: 52 additions & 0 deletions src/SpyMessageReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Onoi\MessageReporter;

/**
* @license GNU GPL v2+
* @since 1.2
*
* @author mwjames
*/
class SpyMessageReporter implements MessageReporter {

/**
* @var array
*/
private $messages = array();

/**
* @since 1.2
*
* {@inheritDoc}
*/
public function reportMessage( $message ) {
$this->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();
}

}
60 changes: 60 additions & 0 deletions tests/phpunit/SpyMessageReporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Onoi\MessageReporter\Tests;

use Onoi\MessageReporter\SpyMessageReporter;

/**
* @covers \Onoi\MessageReporter\SpyMessageReporter
* @group onoi-message-reporter
*
* @license GNU GPL v2+
* @since 1.2
*
* @author mwjames
*/
class SpyMessageReporterTest extends \PHPUnit_Framework_TestCase {

public function testCanConstruct() {

$this->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()
);
}

}

0 comments on commit 975979b

Please sign in to comment.