2017-08-23 1 views
0

Actuellement, je suis en utilisant des chaînes de spécifie où mon test échoue, comme ceci:Comment obtenir le numéro d'un déterminé appelé à la méthode dans PHPUnit

Dans le premier appel à la méthode « XY », le premier paramètre :

Ainsi, vous voulez obtenir le numéro d'appel avec phpunit.

En bref, je veux un nombre cardinal au lieu de first, second, third ..., mais étant donné pour PHPUnit (mieux)

public function testExample() 
{ 
    $test = $this; 

    $this->myClass 
     ->expects($this->exactly(2)) 
     ->method('methodOne') 
     ->withConsecutive(
      [ 
       $this->callback(function ($arg) use ($test) { 
        $part = 'In the first call to methodOne method, the first parameter: '; 

        $test->assertThat(
         $arg, 
         $this->logicalAnd($this->equalTo('example1')), 
         $part . 'is not equal to "example1" ' 
        ); 

        return true; 
       }), 
      ], 
      [ 
       $this->callback(function ($arg) use ($test) { 
        $part = 'In the first call to methodOne method, the first parameter: '; 

        $test->assertThat(
         $arg, 
         $this->logicalAnd($this->equalTo('example2')), 
         $part . 'is not equal to "example2"' 
        ); 

        return true; 
       }), 
      ] 
     ) 
     ->will($this->returnSelf()); 
} 
+0

Pouvez-vous partager le code de production? Peut-être qu'il y a une meilleure façon d'affirmer son comportement. – localheinz

Répondre

1

En utilisant la prophétie:

class A { 
    function abc($a, $b) { 
     return ...; 
    } 
} 

    $a = $this->prophesize (A::class); 
    $a->abc (1,2)->willReturn ("something"); 
    $A = $a->reveal(); 

    $A->abc (1, 2); 
    $A->abc (1, 2); 
    $A->abc (1, 2); 

Cela vous donne le nombre d'appels:

$calls = $a->findProphecyMethodCalls ("abc", new ArgumentsWildcard([new AnyValuesToken])); 
    var_dump (count($calls)); 

Vous pouvez parcourir tous les appels pour voir leurs arguments:

foreach ($calls as $call) 
    { 
     var_dump ($call->getArguments()); 
    }