PHP code example of doppiogancio / mocked-symfony-client

1. Go to this page and download the library: Download doppiogancio/mocked-symfony-client 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/ */

    

doppiogancio / mocked-symfony-client example snippets




namespace DoppioGancio\MockedSymfonyClient\Tests;

use DoppioGancio\MockedSymfonyClient\MockedClient;
use PHPUnit\Framework\TestCase;

class RealExampleTest extends TestCase
{
    private MockedClient $jsonPlaceHolderClient;
    private MockedClient $dummyJsonClient;

    protected function setUp(): void
    {
        parent::setUp();

        $this->injectJsonPlaceHolderClient();
        $this->injectDummyJsonClient();
    }
    
    public function testGetUserByJsonPlaceHolderApi(): void
    {
        $response = $this->jsonPlaceHolderClient->request('GET', '/user/1');
        self::assertEquals(200, $response->getStatusCode());

        $user = $response->toArray();
        self::assertEquals('Leanne Graham', $user['name']);
    }

    private function injectJsonPlaceHolderClient(): void
    {
        $client = new MockedClient([
            'base_uri' => [
                'https://jsonplaceholder.typicode.com',
            ],
        ]);

        $this->jsonPlaceHolderClient = $client;
    }

    private function injectDummyJsonClient(): void
    {
        $client = new MockedClient([
            'base_uri' => [
                'https://dummyjson.com',
            ],
        ]);

        $this->dummyJsonClient = $client;
    }
}

$response = $this->jsonPlaceHolderClient->request('GET', '/user/1');
$user = $response->toArray();

// will return
array:8 [
  "id" => 1
  "name" => "Leanne Graham"
  "username" => "Bret"
  "email" => "[email protected]"
  "address" => array:5 [
    "street" => "Kulas Light"
    "suite" => "Apt. 556"
    "city" => "Gwenborough"
    "zipcode" => "92998-3874"
    "geo" => array:2 [
      "lat" => "-37.3159"
      "lng" => "81.1496"
    ]
  ]
  "phone" => "1-770-736-8031 x56442"
  "website" => "hildegard.org"
  "company" => array:3 [
    "name" => "Romaguera-Crona"
    "catchPhrase" => "Multi-layered client-server neural-net"
    "bs" => "harness real-time e-markets"
  ]
]

self::$container->set(Client::class, $client);
self::$container->set('my_named_client', $client);