Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add mock::didNotReceiveAnyMessage() and mock::hasReceivedMessages().
  • Loading branch information
mageekguy committed Mar 13, 2015
1 parent da4a692 commit b377408
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
41 changes: 41 additions & 0 deletions classes/asserters/mock.php
Expand Up @@ -11,6 +11,19 @@

class mock extends adapter
{
function __get($property)
{
switch (strtolower($property))
{
case 'hasreceivedmessages':
case 'didnotreceiveanymessage':
return $this->{$property}();

default:
return parent::__get($property);
}
}

public function setWith($mock)
{
if ($mock instanceof \mageekguy\atoum\mock\aggregator === false)
Expand All @@ -32,6 +45,34 @@ public function receive($function)
return $this->call($function);
}

public function hasReceivedMessages($failMessage = null)
{
if ($this->adapterIsSet()->adapter->getCallsNumber() > 0)
{
$this->pass();
}
else
{
$this->fail($failMessage ?: $this->_('%s did not receive any message', $this->adapter->getMockClass()));
}

return $this;
}

public function didNotReceiveAnyMessage($failMessage = null)
{
if ($this->adapterIsSet()->adapter->getCallsNumber() <= 0)
{
$this->pass();
}
else
{
$this->fail($failMessage ?: $this->_('%s receive some message', $this->adapter->getMockClass()));
}

return $this;
}

public function wasCalled($failMessage = null)
{
if ($this->adapterIsSet()->adapter->getCallsNumber() > 0)
Expand Down
76 changes: 76 additions & 0 deletions tests/units/classes/asserters/mock.php
Expand Up @@ -135,6 +135,44 @@ public function testWasCalled()
;
}

public function testHasReceivedMessages()
{
$this
->if($asserter = $this->newTestedInstance)
->then
->exception(function() use ($asserter) { $asserter->hasReceivedMessages(); })
->isInstanceOf('mageekguy\atoum\exceptions\logic')
->hasMessage('Mock is undefined')

->if($asserter
->setWith($mock = new \mock\foo($controller = new \mock\atoum\mock\controller()))
->setLocale($locale = new \mock\atoum\locale()),
$this->calling($locale)->_ = $didNotReceiveAnyMessage = uniqid(),
$this->calling($controller)->getCallsNumber = 0,
$this->calling($controller)->getMockClass = $mockClass = uniqid()
)
->then
->exception(function() use ($asserter) { $asserter->hasReceivedMessages(); })
->isInstanceOf('mageekguy\atoum\asserter\exception')
->hasMessage($didNotReceiveAnyMessage)
->mock($locale)->call('_')->withArguments('%s did not receive any message', $mockClass)->once

->exception(function() use ($asserter) { $asserter->hasReceivedMessages; })
->isInstanceOf('mageekguy\atoum\asserter\exception')
->hasMessage($didNotReceiveAnyMessage)
->mock($locale)->call('_')->withArguments('%s did not receive any message', $mockClass)->twice

->exception(function() use ($asserter, & $failMessage) { $asserter->hasReceivedMessages($failMessage = uniqid()); })
->isInstanceOf('mageekguy\atoum\asserter\exception')
->hasMessage($failMessage)

->if($this->calling($controller)->getCallsNumber = rand(1, PHP_INT_MAX))
->then
->object($asserter->hasReceivedMessages())->isIdenticalTo($asserter)
->object($asserter->hasReceivedMessages)->isIdenticalTo($asserter)
;
}

public function testWasNotCalled()
{
$this
Expand Down Expand Up @@ -173,6 +211,44 @@ public function testWasNotCalled()
;
}

public function testDidNotReceiveAnyMessage()
{
$this
->if($asserter = $this->newTestedInstance)
->then
->exception(function() use ($asserter) { $asserter->didNotReceiveAnyMessage(); })
->isInstanceOf('mageekguy\atoum\exceptions\logic')
->hasMessage('Mock is undefined')

->if($asserter
->setWith($mock = new \mock\foo($controller = new \mock\atoum\mock\controller()))
->setLocale($locale = new \mock\atoum\locale()),
$this->calling($locale)->_ = $hasReceivedMessage = uniqid(),
$this->calling($controller)->getCallsNumber = rand(1, PHP_INT_MAX),
$this->calling($controller)->getMockClass = $mockClass = uniqid()
)
->then
->exception(function() use ($asserter) { $asserter->didNotReceiveAnyMessage(); })
->isInstanceOf('mageekguy\atoum\asserter\exception')
->hasMessage($hasReceivedMessage)
->mock($locale)->call('_')->withArguments('%s receive some message', $mockClass)->once

->exception(function() use ($asserter) { $asserter->didNotReceiveAnyMessage; })
->isInstanceOf('mageekguy\atoum\asserter\exception')
->hasMessage($hasReceivedMessage)
->mock($locale)->call('_')->withArguments('%s receive some message', $mockClass)->twice

->exception(function() use ($asserter, & $failMessage) { $asserter->didNotReceiveAnyMessage($failMessage = uniqid()); })
->isInstanceOf('mageekguy\atoum\asserter\exception')
->hasMessage($failMessage)

->if($this->calling($controller)->getCallsNumber = 0)
->then
->object($asserter->didNotReceiveAnyMessage())->isIdenticalTo($asserter)
->object($asserter->didNotReceiveAnyMessage)->isIdenticalTo($asserter)
;
}

public function testCall()
{
$this
Expand Down

0 comments on commit b377408

Please sign in to comment.