jeudi 21 juillet 2016

PHPUnit mocked method returns null

I am trying to test the below class using PHPUnit

class stripe extends paymentValidator {
    public $apiKey;

    public function __construct ($apiKey){
        $this->apiKey = $apiKey;
    }

    public function charge($token) {
        try {
            return $this->requestStripe($token);
        } catch(\Stripe\Error\Card $e) {
            echo $e->getMessage();
            return false;
        }
    }

    public function requestStripe($token) {
        // do something        
    }
}

My test scripts is like the below:

class paymentvalidatorTest extends PHPUnit_Framework_TestCase
{
   /**
    * @test
    */
    public function test_stripe() {
        // Create a stub for the SomeClass class.
        $stripe = $this->getMockBuilder(stripe::class)
            ->disableOriginalConstructor()
            ->setMethods(['requestStripe', 'charge'])
            ->getMock();

        $stripe->expects($this->any())
            ->method('requestStripe')
            ->will($this->returnValue('Miaw'));

        $sound = $stripe->charge('token');
        $this->assertEquals('Miaw', $sound);
    }
}

With my test script I was expecting the test double of stripe::charge() method will do exactly as the defined in the original class and the stripe::requestStripe() will return 'Miaw'. Therefore, $stripe->charge('token') should also return 'Miaw'. However, when I run the test I get:

Failed asserting that null matches expected 'Miaw'.

How should I fix this ?

Aucun commentaire:

Enregistrer un commentaire