-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MessageReporterAware and SpyMessageReporter
- Loading branch information
Showing
5 changed files
with
179 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
|
||
} |