-
Notifications
You must be signed in to change notification settings - Fork 283
Closed
Description
At present phpspec does not appear to support anything equivalent to the 'constraint' functionality provided by phpunit but sometimes fuzzy matches are required when testing arguments passed to mocked methods.
As an example use case consider the regular expression example below or in a situation where multiple arguments are passed to a mocked method but you only need to verify one (in which case you would use the 'anything' constraint).
http://www.phpunit.de/manual/3.4/en/api.html#api.assert.assertThat.tables.constraints documents all constraints that PHPUnit supports.
Here is an example of the constraints functionality.
<?php
class SomeObject
{
public function someMethod( $arg1, $arg2 )
{
}
}
class TestCase extends PHPUnit_Framework_TestCase
{
public function setup()
{
$this->mock = $this->getMock( 'SomeObject' );
$this->mock->expects( $this->any() )
->method( 'someMethod' )
->with(
$this->matchesRegularExpression( '/^abc.*/' ),
$this->greaterThan( 4 )
);
}
public function testConstraintPassExample()
{
$this->mock->someMethod( 'abc123', 5 );
}
public function testFirstArgConstraintFailExample()
{
$this->mock->someMethod( 'def456', 6 );
}
public function testSecondArgConstraintFailExample()
{
$this->mock->someMethod( 'abc456', 2 );
}
}
Metadata
Metadata
Assignees
Labels
No labels