PHPUnit: Mocking a Method That Takes a Parameter

I am creating tests for a class that accepts the Search class, which searches the supermarket using the search bar and has a getItem ($ itemNo) method that returns the corresponding item.

So a bit like this:

class MyClass { public function __construct(Search $search) { $item0 = $search->getItem(0); $item1 = $search->getItem(1); // etc... you get the picture } } 

I want to mock this search class, since I donโ€™t want to look for a supermarket every time I do a test.

So I wrote:

 class MyClassTest extends PHPUnit_Framework_TestCase { public function setUp() { $searchResults=$this->getMockBuilder('Search') //Because the constructor takes in a search string: ->disableOriginalConstructor() ->getMock(); $pseudoSupermarketItem=array( "SearchResult1", "SearchResult2", etc...); $this->searchResult ->expects($this->any()) ->method('getItem') ->with(/*WHAT DO I PUT HERE SO THAT THE METHOD WILL TAKE IN A NUMBER*/) ->will($this->returnValue($pseudoSupermarketItem[/* THE NUMBER THAT WAS PUT IN */])); } } 

As you can see in the code, I would like the mock method to accept an integer, as shown in MyClass, which will then return the corresponding string pseudoSupermarketItem. As long as I'm not sure how to do this, any help is appreciated!

+4
source share
2 answers

This should work for you:

 $this->searchResult ->expects($this->any()) ->method('getItem') ->with($this->isType('integer')) ->will($this->returnCallback(function($argument) use ($pseudoSupermarketItem) { return $pseudoSupermarketItem[$argument]; }); 

In addition, you might find it useful (using onConsecutiveCalls ):

http://phpunit.de/manual/3.7/en/test-doubles.html#test-doubles.stubs.examples.StubTest7.php

The sixth way is something like this:

 $this->searchResult ->expects($this->at(0)) ->method('getItem') ->with($this->equalTo(0)) ->will($this->returnValue($pseudoSupermarketItem[0]); $this->searchResult ->expects($this->at(1)) ->method('getItem') ->with($this->equalTo(1)) ->will($this->returnValue($pseudoSupermarketItem[1]); // (...) 
+4
source

PHPUnit offers returnValueMap() for cases where the input parameters must trigger a specific return value.

 // Create a map of arguments to return values. $map = array( array(0, 'SearchResult0'), array(1, 'SearchResult1') ); // Configure the stub. $searchResults->expects($this->any()) ->method('getItem') ->will($this->returnValueMap($map)); $this->assertEquals('SearchResult0', $searchResults->getItem(0)); $this->assertEquals('SearchResult1', $searchResults->getItem(1)); $this->assertEquals('SearchResult0', $searchResults->getItem(0)); 

At first, the map looks a little strange, because there is no direct assignment of the key-> value, but this is because this map also works with several input parameters for the scoffing method.

 $mockedAddition = array( array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 2), ); $calculator->expects($this->any()) ->method('add') ->will($this->returnValueMap($mockedAddition); $this->assertSame(0, $calculator->add(0, 0)); // Returns the value x of (0, 0, x) $this->assertSame(1, $calculator->add(1, 0)); // Returns the value x of (1, 0, x) $this->assertSame(1, $calculator->add(0, 1)); // Returns the value x of (0, 1, x) 
+2
source

Source: https://habr.com/ru/post/1502074/


All Articles