Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for constraints or equivalent #18

@mikesimons

Description

@mikesimons

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions