Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twice() method doesn't work in Mockery PHP

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?

like image 214
Vish021 Avatar asked Jun 23 '26 20:06

Vish021


1 Answers

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);
like image 182
gontrollez Avatar answered Jun 25 '26 10:06

gontrollez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!