@vincent15000 In both vanilla PHP and Laravel, mocking works by substituting real objects with test mocking.
But in Laravel, the Service Container and Service Providers play an important role in resolving dependencies.
In vanilla PHP, you manually handle dependency injection, so mocking relies on your ability to control how dependencies are passed into the class.
Here is an example for vanilla PHP:
class Foo {
protected $dependency;
public function __construct(Dependency $dependency) {
$this->dependency = $dependency;
}
public function doSomething() {
return $this->dependency->performAction();
}
}
// PHPUnit Test
class FooTest extends TestCase {
public function testDoSomething() {
$mockDependency = $this->createMock(Dependency::class);
$mockDependency->method('performAction')->willReturn('mocked result');
$foo = new Foo($mockDependency);
$this->assertEquals('mocked result', $foo->doSomething());
}
}