mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') ); } protected function tearDown(): void { $reflectionClass = new ReflectionClass(Manager::class); $reflectionProperty = $reflectionClass->getProperty('client'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null); } public function testMissingRequest(): void { $this->expectException(ClientException::class); $this->expectExceptionMessage('Request not configured.'); (new ImagesVariations())->getRequest(); } public function testUnsupportedContentTypeResponse(): void { $this->expectException(ClientException::class); $this->expectExceptionMessage('Unsupported content type: text/plain'); $handler = new ImagesVariations(); $method = (new ReflectionObject($handler))->getMethod('parseResponse'); $method->setAccessible(true); $method->invoke($handler, new Response( 200, ['Content-Type' => 'text/plain'], null )); } public function testInvalidJsonResponse(): void { $this->expectException(ClientException::class); $this->expectExceptionMessage('Failed to parse JSON response body: Syntax error'); $handler = new ImagesVariations(); $method = (new ReflectionObject($handler))->getMethod('parseResponse'); $method->setAccessible(true); $method->invoke($handler, new Response( 200, ['Content-Type' => 'application/json'], 'invalidJson' )); } public function testUnsuccessfulResponseCode(): void { $this->expectException(ClientException::class); $this->expectExceptionMessage("Unsuccessful response. HTTP status code: 418 (I'm a teapot)."); $handler = new ImagesVariations(); $property = (new ReflectionObject($handler))->getProperty('response'); $property->setAccessible(true); $property->setValue($handler, new Response(418)); $handler->toModel(); } public static function toArrayDataProvider(): array { return [ ['{}', []], ['[]', []], ['{"a": "b"}', ['a' => 'b']], ]; } /** * @dataProvider toArrayDataProvider */ public function testToArray(string $rawJsonResponse, array $expected): void { $handler = new ImagesVariations(); $property = (new ReflectionObject($handler))->getProperty('response'); $property->setAccessible(true); $property->setValue($handler, new Response( 200, ['Content-Type' => 'application/json'], $rawJsonResponse )); $this->assertSame($expected, $handler->toArray()); } public function testCreateImageMethod(): void { $filesystem = vfsStream::setup(); // Create the file(s) to be uploaded. $files = ['image']; foreach ($files as $file) { vfsStream::newFile($file) ->withContent(LargeFileContent::withKilobytes(1)) ->at($filesystem); } $request = (new ImagesVariations()) ->createImage(new CreateImageRequest(['image' => 'vfs://root/image'])) ->getRequest(); $this->assertValidate($request); } }