1. Go to this page and download the library: Download happyr/service-mocking library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// config/packages/test/happyr_service_mocking.php
use Symfony\Config\HappyrServiceMockingConfig;
return static function (HappyrServiceMockingConfig $config) {
$config->services([
\App\AcmeApiClient::class,
\App\Some\OtherService::class,
]);
};
use App\AcmeApiClient;
use App\Some\OtherService;
use Happyr\ServiceMocking\ServiceMock;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MyTest extends WebTestCase
{
public function testFoo()
{
// ...
$apiClient = self::getContainer()->get(AcmeApiClient::class);
// For all calls to $apiClient->show()
ServiceMock::all($apiClient, 'show', function ($id) {
// $id here is the same that is passed to $apiClient->show('123')
return ['id'=>$id, 'name'=>'Foobar'];
});
// For only the next call to $apiClient->delete()
ServiceMock::next($apiClient, 'delete', function () {
return true;
});
// This will queue a new callable for $apiClient->delete()
ServiceMock::next($apiClient, 'delete', function () {
throw new \InvalidArgument('Item cannot be deleted again');
});
$mock = // create a PHPUnit mock or any other mock you want.
ServiceMock::swap(self::getContainer()->get(OtherService::class), $mock);
// ...
self::$client->request(...);
}
protected function tearDown(): void
{
// To make sure we don't affect other tests
ServiceMock::resetAll();
// You can
class MyService {
public function foo()
{
return $this->bar();
}
public function bar()
{
return 'original';
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.