phpgasil.blogg.se

Phpunit mocks vs stubs
Phpunit mocks vs stubs







  1. #PHPUNIT MOCKS VS STUBS HOW TO#
  2. #PHPUNIT MOCKS VS STUBS UPDATE#

If the values are numbers they they are considered equal if their difference is equal to zero (this level of accuracy can be changed).If the values are of different types they are not equal.If the values are identical, they are equal.PHPUnit has a fairly complex way of comparing parameters passed to the mock method with the expected values, which can be summarised like so. $mock->expects($matcher)Ĭalling with() without any parameters will force the mock method to accept no parameters. The order in which you define the parameters is the order that it expects them to be in when called. To only allow specific parameters you can use the with() method which accepts any number of parameters. The former can be achieved by calling withAnyParameters() on the object returned from method() $mock->expects($matcher) Tell the method to accept a specific set of parameters.Tell the method to accept any parameters.$mock = $this->GetMock('ORM', array('check')) There's nothing very fancy about this function. You do this by calling method() on the returned from expects(): $mock->expects($matcher)Īs you can probably guess, method() takes one parameter, the name of the method you're mocking.

phpunit mocks vs stubs

#PHPUNIT MOCKS VS STUBS UPDATE#

In our example we want check() to be called once on our mock object, so if we update it accordingly: $mock = $this->getMock('ORM', array('check')) Īlthough we told PHPUnit what methods we want to mock, we haven't actually told it what method these rules we're specifiying apply to. You start off by telling PHPUnit how many times the method should be called by calling expects() on the mock object: $mock->expects($matcher) Įxpects() takes one argument, an invoker matcher which you can create using factory methods defined in PHPUnit_Framework_TestCase: Possible invoker matchers: $this->any() Returns a matcher that allows the method to be called any number of times $this->never() Returns a matcher that asserts that the method is never called $this->once() Returns a matcher that asserts that the method is only called once $this->atLeastOnce() Returns a matcher that asserts that the method is called at least once $this->exactly($count) Returns a matcher that asserts that the method is called at least $count times $this->at($index) Returns a matcher that matches when the method it is evaluated for is invoked at the given $index.

#PHPUNIT MOCKS VS STUBS HOW TO#

We now need to tell PHPUnit how to mock the check function when its called. Mocking methodsĪssuming we've created a mock object like so: $mock = $this->getMock('ORM', array('check')) $mock now contains a mock of ORM, but this time we're also mocking the check() method.

phpunit mocks vs stubs

$mock now contains a mock of ORM and can be handled as though it were a vanilla instance of ORM $mock = $this->getMock('ORM', array('check')) Most of the time you'll only need to use the first two parameters, i.e.: $mock = $this->getMock('ORM') $arguments An array of arguments to pass to the mock's constructor $mockClassName Allows you to specify the name that will be given to the mock $callOriginalConstructor Should the mock call its parent's constructor automatically? $callOriginalClone Should the mock call its parent's clone method? You need to tell PHPUnit in advance because PHP doesn't allow you to extend an object once it's been initialised. $originalClassName The name of the class that you want to mock $methods The methods of $originalClassName that you want to mock. You create mocks from within testcases using the getMock() function, which is defined in PHPUnit_Framework_TestCase like so: getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE) You can also tell PHPUnit to override certain functions to return set values / assert that they're called in a specific way. It creates a class that extends the one you want to mock. PHPUnit has a built in mock object creator which can generate mocks for classes (inc. You can create a mock database connection which responds in the way the model expects, but doesn't actually connect to a physical database. Say for example you're testing a model - you want to make sure that the model is running the correct query, but you don't want it to run on a real database server.

phpunit mocks vs stubs

Sometimes when writing tests you need to test something that depends on an object being in a certain state. What parameters should our mock method expect?









Phpunit mocks vs stubs