3

I am new to PHP Mockery Framework. I have a mock function executePrepared($arg1, $arg2, arg3) which I am calling it twice but seems to be not working and gives below error in PHPUnit command line:

Configuration read from C:\xampp\htdocs\DatabaseTesting\phpunit.xml

..←[31;1mE←[0m

Time: 47 ms, Memory: 3.25Mb

There was 1 error:


1) Test\Database\Table\TableTest::testinsertMany
 Mockery\Exception\NoMatchingExpectationException: No matching handler found for
 Mockery_0_Database_PGC::executePrepared(array(0=>'ClayPipe',1=>2000,2=>2100,3=>1
 ,4=>'2000-01-01',5=>'{"1":"1","2":6,"3":8,"4":10}',), "insert_assets", "\Databas
 e\Model\Asset"). Either the method was unexpected or its arguments matched no ex
 pected argument list for this method

my test function is as below:

 public function testinsertMany() {
      $this->PGCMock->shouldReceive('executePrepared')->twice()->withArgs(array(
[array('Clay Pipe',2000,2100,1,'2000-01-01','{"1":"1","2":6,"3":8,"4":10}'), 'insert_assets', '\Database\Model\Asset'],
[array('Main Street',1000,1100,0,'2000-02-01','{"1":"1","2":6,"3":8,"4":10}'), 'insert_assets', '\Database\Model\Asset']))
->andReturn($expectedResult1);

$data1 = array('name'=>'Clay Pipe',
                    'hist_cost' => 2000,
                    'val_cost' => 2100,
                    'val_method' => 1,
                    'service_date' => '2000-01-01',
                    'tags' => '{"1":"1","2":6,"3":8,"4":10}'
                    );

    $data2 = array('name'=>'Main Street',
                    'hist_cost' => 1000,
                    'val_cost' => 1100,
                    'val_method' => 0,
                    'service_date' => '2000-02-01',
                    'tags' => '{"1":"1","2":6,"3":8,"4":10}'
                    ); 

    $actualResult = $this->tableMock->insertMany(array($data1,$data2));
}

I don't understand what's wrong here. Is my Syntax for calling mock function twice() with passed argument wrong? Could any body please guide me here?

Vish021
  • 399
  • 1
  • 5
  • 18
  • Possible duplicate of [Mockery specifying expected arguments for multiple calls](http://stackoverflow.com/questions/26510007/mockery-specifying-expected-arguments-for-multiple-calls) – Tim Strijdhorst Apr 19 '17 at 13:44

1 Answers1

5

twice() should be used when the same call (including arguments) is expected to be executed 2 times. Looks like you want to check 2 consecutive calls, each with distinct argument.

If that is the case, this would work:

$this->PGCMock
    ->shouldReceive('executePrepared')
    ->once()
    ->ordered()
    ->withArgs([
        array('Clay Pipe',2000,2100,1,'2000-01-01','{"1":"1","2":6,"3":8,"4":10}'),
        'insert_assets', '\Database\Model\Asset'
     ])
     ->andReturn($result1);
$this->PGCMock
    ->shouldReceive('executePrepared')
    ->once()
    ->ordered()
    ->withArgs([
         array('Main Street',1000,1100,0,'2000-02-01','{"1":"1","2":6,"3":8,"4":10}'),
         'insert_assets', '\Database\Model\Asset'            
     ])
     ->andReturn($result2);
gontrollez
  • 6,372
  • 2
  • 28
  • 36
  • Thanks @gontrollez! My intention is to write code only once and execute twice with different argument. Is there any way we can use times(n) function? I am sorry but I could not find concise examples in official documentation. – Vish021 Dec 04 '14 at 14:38
  • Not sure about what you mean. But if executePrepared is going to be executed 2 times and with different parameters, you must configure each call by separate. Isn't that the case? – gontrollez Dec 04 '14 at 17:17
  • Yes, executePrepared is going to execute exactly 2 times with different arguments. If we have to configure each call by separate then the solutions you have provided works fine. I thought we can achieve this in a single call. Anyways, Thanks for answer! – Vish021 Dec 04 '14 at 19:34
  • Old one but put the expectation in a loop. – Leigh Bicknell Jun 07 '23 at 11:17