Core\Test::partial
Core\Test::partial<T>(T $real, object $answers): TCore\Test::partial<T>( T $real, object $answers, ): T
Core\Test::partial() builds a test double that replaces only some methods of a real object. You write the interface in angle brackets, then give the real object and one callable for each method you want to replace. The result has the interface type, so you can pass it to any code that takes one.
A call to a replaced method runs your callable. A call to any other method runs the method of the real object, with the same arguments. Inside the real object, $this is still the real object. So when a real method calls another method on $this, it calls the real one and not your callable. Like Core\Test::double, a partial keeps a list of its calls for Core\Test::assertCalled.
The examples below show how to replace one method, how the real object keeps its own state, and a test of a disk that is full.
Parameters
$realTThe object that gets the calls to the methods$answersdoes not replace. Each call reaches$realwith the same arguments.$answersobjectOne field for each method you replace, with the same name as the method. A field for a method thatTdoes not declare does not compile. A method you leave out is allowed, because$realhandles it. This is not allowed indouble.
Return value
Returns T. The partial double, typed as T. Every call is recorded, both the calls a function in $answers handles and the calls $real handles.
Errors
LogicErrorTdeclares more methods than one double supports. The limit and the error message are the same as fordouble.
Examples
Replace one method
<?nvs// Shows how to replace one method of a real object and keep the others.
interface Shop{ public function name(): string; public function openAt(): int;}
final class RealShop implements Shop{ public function name(): string { return 'Corner Shop'; }
public function openAt(): int { return 8; }}
// Only `openAt` is replaced. `name` still runs the method of `RealShop`.Shop $late = Core\Test::partial<Shop>(new RealShop(), { openAt: fn(): int => 10,});
// This prints `Corner Shop opens at 10`.echo $late->name(), ' opens at ', $late->openAt(), "\n";Corner Shop opens at 10The real object keeps its state
<?nvs// Shows that a partial calls the real object, so the real object keeps its own state.
interface Cart{ public function add(int $cents): void; public function total(): int; public function shipping(): int;}
final class RealCart implements Cart{ private int $sum = 0;
public function add(int $cents): void { $this->sum = $this->sum + $cents; }
public function total(): int { return $this->sum + $this->shipping(); }
public function shipping(): int { return 500; }}
RealCart $real = new RealCart();Cart $free = Core\Test::partial<Cart>($real, { shipping: fn(): int => 0,});
// `add` is not replaced, so it adds to the sum inside `$real`.$free->add(1200);$free->add(300);
// `shipping` is replaced, so this prints `0`.echo $free->shipping(), "\n";
// `total` runs in the real object, and it calls the real `shipping`.// So this prints `2000`, and `$real->total()` prints the same.echo $free->total(), "\n";echo $real->total(), "\n";020002000A disk that is full
<?nvs// Shows how to test what your code does when one operation fails, with the rest real.
interface Storage{ public function exists(string $name): bool; public function write(string $name, string $content): bool;}
final class MemoryStorage implements Storage{ public function exists(string $name): bool { return $name == 'invoice-1.txt'; }
public function write(string $name, string $content): bool { return true; }}
class Invoices{ public static function save(Storage $storage, string $name, string $content): string { if ($storage->exists($name)) { return $name . ' already exists'; } return $storage->write($name, $content) ? $name . ' saved' : $name . ' not saved: the disk is full'; }}
// `write` is replaced to act like a full disk. `exists` is still the real method.Storage $full = Core\Test::partial<Storage>(new MemoryStorage(), { write: fn(string $name, string $content): bool => false,});
// This prints `invoice-1.txt already exists` and `invoice-2.txt not saved: the disk is full`.echo Invoices::save($full, 'invoice-1.txt', 'total: 40'), "\n";echo Invoices::save($full, 'invoice-2.txt', 'total: 55'), "\n";
// The code tried to write only once, for the new invoice.Core\Test::assertCalled($full, Storage::write, { times: 1, with: ['invoice-2.txt', 'total: 55'] });echo "one write was tried\n";invoice-1.txt already existsinvoice-2.txt not saved: the disk is fullone write was tried