I have a class
<?php
class Cards
{
public function __construct($id) {
$this->id = $id;
}
public function add($card) {
// Make API call
return true;
}
}
and another class that returns the Cards class
<?php
class Payment
{
public function cards() {
return new Cards('1');
}
}
And I cannot seem to figure out how to mock Payment so it returns a mocked Cards. Basically I want to
Mock Payment so it returns a mocked Cards that would allow a function like
function() {
$vault = new Payment;
$cards = $vault->cards();
if ($cards->add()) {
// do stuff
}
}
can be mocked and tested without making API calls to the payment processor.
The problem is that you're trying to instantiate the Card
class using the new
keyword. This makes it (almost) impossible to mock it. You can use a couple of different techniques to replace the new
keyword, the easiest would be to pass an instance into the Payment
constructor or directly into the method.