Bullying a call with chain methods and arguments

I am learning how to use mockery to run some unit test, and I'm not sure what to do to mock my database class. It consists of separate methods that can be connected in a circuit, like these two examples:

$db->select('someTblName',['fieldName'])
   ->where('fieldName', 'someValue')
   ->runQuery()
   ->fetch(); //returns array or null

Other uses might be:

$db->select('someTblName')
   ->where('fieldName', 'someValue')
   ->where('fieldName', array('>=' , 'someValue')
   ->runQuery()
   ->fetch(); //returns array or null

From reading some documents, I see that I can do something like: (for the first case)

$db = \Mockery::mock('Database');
$db->shouldReceive('select', 'where', 'runQuery', 'fetcth')
    ->with(??????)
    ->andReturn(null);

Now I'm wondering how to pass the “relevant parameters” to the methods? And, how I would make fun of the second scenario.

+4
source share
2 answers

shouldReceive('select->where->runQuery->fetch'), . , , :

$db->shouldReceive('select')->with('someTblName', ['fieldName'])
    ->once()->andReturn(m::self())->getMock()
    ->shouldReceive('where')...

shouldReceive('fetch')->andReturn(null).

+8

,

$db = m::stub('Database', array(
   'select(someTblName)->where(fieldName,someValue)->runQuery->fetch' => 'return stuff',
   'select(someOtherTblName)->where(...)->runQuery->fetch' => 'return other stuff'

));

/ Mockery,

https://github.com/elvisciotti/mockery-stub-chain-args

alpha , , , .

0

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


All Articles