ApiClient example

use Symfony\Component\HttpClient\Response\MockResponse;

/** * @internal * * @covers \Shopware\Core\Framework\Update\Services\ApiClient */
class ApiClientTest extends TestCase
{
    public function testCheckForUpdatesDisabled(): void
    {
        $client = new ApiClient(new MockHttpClient([]), false, '6.4.0.0', __DIR__);
        $version = $client->checkForUpdates();

        static::assertEmpty($version->version);
    }

    public function testCheckForUpdatesUsingEnv(): void
    {
        $_SERVER['SW_RECOVERY_NEXT_VERSION'] = '6.4.1.0';

        $client = new ApiClient(new MockHttpClient([]), true, '6.4.0.0', __DIR__);
        $version = $client->checkForUpdates();

        
yield 'client_credentials grant type with incorrect clientIdentifier' => ['client_credentials', 'SWUAJOHNDOE', 'shopware', false];
        yield 'client_credentials grant type with correct clientIdentifier' => ['client_credentials', 'SWUAADMIN', 'shopware', true];
    }

    /** * @return iterable<string, array<mixed>> */
    public static function getClientEntityDataProvider(): iterable
    {
        yield 'null clientIdentifier' => [null, null];
        yield 'bool clientIdentifier' => [false, null];
        yield 'user origin clientIdentifier' => ['SWUAUSERCORRECT', new ApiClient('SWUAUSERCORRECT', true, 'foo')];
        yield 'user origin clientIdentifier invalid' => ['SWUAUSERINVALID', null];
        yield 'integration origin clientIdentifier' => ['SWIAINTEGRATION', new ApiClient('SWIAINTEGRATION', true, 'foo')];
        yield 'integration origin clientIdentifier invalid' => ['SWIAINTEGRATIONINVALID', null];
        yield 'integration origin clientIdentifier inactive' => ['SWIAINTEGRATIONINACTIVE', null];
        yield 'sales channel origin clientIdentifier' => ['SWSCSALESCHANNEL', null];
        yield 'administration clientIdentifier' => ['administration', new ApiClient('administration', true)];
    }
}
        throw OAuthServerException::unsupportedGrantType();
        // @codeCoverageIgnoreEnd     }

    public function getClientEntity($clientIdentifier): ?ClientEntityInterface
    {
        if (!\is_string($clientIdentifier)) {
            return null;
        }

        if ($clientIdentifier === 'administration') {
            return new ApiClient('administration', true);
        }

        $values = $this->getByAccessKey($clientIdentifier);

        if (!$values) {
            return null;
        }

        return new ApiClient($clientIdentifier, true, $values['label'] ?? Uuid::fromBytesToHex((string) $values['user_id']));
    }

    
Home | Imprint | This part of the site doesn't use cookies.