From d4755c1e80e657e60b96b94612018d0f70ec2e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Baconnier?= Date: Thu, 4 Nov 2021 10:47:38 +0100 Subject: [PATCH] Add tests --- tests/Clients/ZohoHttpTest.php | 172 ++++++++++++++++++ .../ZohoUrlTest.php} | 4 +- tests/Factories/ZohoHttpTest.php | 100 ---------- 3 files changed, 174 insertions(+), 102 deletions(-) create mode 100644 tests/Clients/ZohoHttpTest.php rename tests/{Factories/UrlFactoryTest.php => Clients/ZohoUrlTest.php} (97%) delete mode 100644 tests/Factories/ZohoHttpTest.php diff --git a/tests/Clients/ZohoHttpTest.php b/tests/Clients/ZohoHttpTest.php new file mode 100644 index 0000000..d682b5f --- /dev/null +++ b/tests/Clients/ZohoHttpTest.php @@ -0,0 +1,172 @@ + 'mock_access_token', 'expires' => now()->addHour()->timestamp]); + + $this->mock(AccessTokenRepository::class, static function (MockInterface $repository) use ($accessToken) + { + $repository->shouldReceive('exists')->andReturn(true); + $repository->shouldReceive('get')->andReturn($accessToken); + $repository->shouldReceive('store')->with($accessToken); + }); + + $this->mock(ZohoAuthProvider::class, static function (MockInterface $repository) use ($accessToken) + { + $repository->shouldReceive('getAccessToken')->andReturn($accessToken); + }); + + $this->zohoClientHttp = new ZohoHttp; + } + + protected function tearDown(): void + { + \Mockery::close(); + } + + /** @test */ + public function it_can_make_a_request(): void + { + $fakeResponse = [ + "users" => [ + [ + "country" => "US", + "street" => null, + "id" => "4150868000000225013", + "first_name" => "Patricia", + "last_name" => "Boyle", + ], + ], + "info" => [ + "per_page" => 200, + "count" => 3, + "page" => 1, + "more_records" => false, + ], + ]; + + $this->zohoClientHttp->fake([ + 'users' => Http::response($fakeResponse), + '*' => Http::response(['error' => 'error']), + ]); + + $this->assertEquals( + $fakeResponse, + $this->zohoClientHttp + ->get('users')->json() + + ); + } + + + /** @test */ + public function it_returns_a_zoho_response_class(): void + { + $this->zohoClientHttp->fake(); + + $this->zohoClientHttp->get('/'); + + /** @var Request $request */ + $request = $this->zohoClientHttp->recorded()[0][0]; + $this->assertTrue($request->hasHeader('Authorization')); + $this->assertEquals('Zoho-oauthtoken mock_access_token', $request->headers()['Authorization'][0]); + } + + /** @test */ + public function it_returns_a_zoho_pending_request(): void + { + $this->zohoClientHttp->fake(); + + $pendingRequest = $this->zohoClientHttp->asForm(); + + $reflection = new \ReflectionClass($pendingRequest); + $reflectionProperty = $reflection->getProperty('options'); + $reflectionProperty->setAccessible(true); + + $options = $reflectionProperty->getValue($pendingRequest); + + $this->assertInstanceOf(ZohoPendingRequest::class, $pendingRequest); + $this->assertEquals('Zoho-oauthtoken mock_access_token', $options['headers']['Authorization']); + } + + /** @test */ + public function it_uses_the_zoho_bearer_header_when_performing_a_request(): void + { + $this->zohoClientHttp->fake(); + + $this->assertInstanceOf(ZohoResponse::class, $this->zohoClientHttp->get('url')); + } + + /** @test */ + public function it_call_refresh_token_when_performing_a_request(): void + { + $accessToken = $this->mock(ZohoAccessToken::class, static function (MockInterface $mock): void + { + $mock->shouldReceive('hasExpired')->once()->andReturn(true); + $mock->shouldReceive('getRefreshToken')->once()->andReturnSelf(); + $mock->shouldReceive('getToken')->once(); + }); + + $this->mock(AccessTokenRepository::class, static function (MockInterface $repository) use ($accessToken) + { + $repository->shouldReceive('exists')->andReturn(true); + $repository->shouldReceive('get')->andReturn($accessToken); + $repository->shouldReceive('store'); + }); + + $this->zohoClientHttp->fake(); + + $this->zohoClientHttp->get('/'); + + $this->assertInstanceOf(MockInterface::class, $accessToken); + } + + /** @test */ + public function it_call_only_once_refresh_token_when_performing_multiple_operations(): void + { + $accessToken = $this->mock(ZohoAccessToken::class, static function (MockInterface $mock): void + { + $mock->shouldReceive('hasExpired')->once()->andReturn(true); + $mock->shouldReceive('getRefreshToken')->once()->andReturnSelf(); + $mock->shouldReceive('getToken')->once(); + }); + + $this->mock(AccessTokenRepository::class, static function (MockInterface $repository) use ($accessToken) + { + $repository->shouldReceive('exists')->andReturn(true); + $repository->shouldReceive('get')->andReturn($accessToken); + $repository->shouldReceive('store'); + }); + + $this->zohoClientHttp->fake(); + + $this->zohoClientHttp + ->asForm() + ->attach('file') + ->get('/'); + + $this->assertInstanceOf(MockInterface::class, $accessToken); + } + +} \ No newline at end of file diff --git a/tests/Factories/UrlFactoryTest.php b/tests/Clients/ZohoUrlTest.php similarity index 97% rename from tests/Factories/UrlFactoryTest.php rename to tests/Clients/ZohoUrlTest.php index 88e15d8..f6da4f4 100644 --- a/tests/Factories/UrlFactoryTest.php +++ b/tests/Clients/ZohoUrlTest.php @@ -1,6 +1,6 @@ 'mock_access_token', 'expires' => now()->addHour()->timestamp]); - - $this->mock(AccessTokenRepository::class, static function (MockInterface $repository) use ($accessToken) - { - $repository->shouldReceive('exists')->andReturn(true); - $repository->shouldReceive('get')->andReturn($accessToken); - $repository->shouldReceive('store')->with($accessToken); - }); - - $this->mock(ZohoAuthProvider::class, static function (MockInterface $repository) use ($accessToken) - { - $repository->shouldReceive('getAccessToken')->andReturn($accessToken); - }); - - $this->zohoClientFactory = new ZohoHttp; - } - - protected function tearDown(): void - { - \Mockery::close(); - } - - /** @test */ - public function it_can_make_a_request(): void - { - $fakeResponse = [ - "users" => [ - [ - "country" => "US", - "street" => null, - "id" => "4150868000000225013", - "first_name" => "Patricia", - "last_name" => "Boyle", - ], - ], - "info" => [ - "per_page" => 200, - "count" => 3, - "page" => 1, - "more_records" => false, - ], - ]; - - $this->zohoClientFactory->fake([ - 'users' => Http::response($fakeResponse), - '*' => Http::response(['error' => 'error']), - ]); - - $this->assertEquals( - $fakeResponse, - $this->zohoClientFactory - ->get('users')->json() - - ); - } - - - /** @test */ - public function it_returns_a_zoho_response_class(): void - { - $this->zohoClientFactory->fake(); - - $this->zohoClientFactory->get('/'); - - /** @var Request $request */ - $request = $this->zohoClientFactory->recorded()[0][0]; - $this->assertTrue($request->hasHeader('Authorization')); - $this->assertEquals('Zoho-oauthtoken mock_access_token', $request->headers()['Authorization'][0]); - } - - /** @test */ - public function it_uses_the_zoho_bearer_header_when_performing_a_request(): void - { - $this->zohoClientFactory->fake(); - - $this->assertInstanceOf(ZohoResponse::class, $this->zohoClientFactory->get('url')); - } -} \ No newline at end of file