|
17 | 17 |
|
18 | 18 | namespace Google\Cloud\BigQuery\Tests\Unit; |
19 | 19 |
|
| 20 | +use Google\ApiCore\ValidationException; |
| 21 | +use Google\Auth\FetchAuthTokenInterface; |
| 22 | +use Google\Auth\HttpHandler\Guzzle6HttpHandler; |
20 | 23 | use Google\Cloud\BigQuery\BigNumeric; |
21 | 24 | use Google\Cloud\BigQuery\BigQueryClient; |
22 | 25 | use Google\Cloud\BigQuery\Bytes; |
|
36 | 39 | use Google\Cloud\BigQuery\Time; |
37 | 40 | use Google\Cloud\BigQuery\Timestamp; |
38 | 41 | use Google\Cloud\Core\Int64; |
| 42 | +use Google\Cloud\Core\Testing\Snippet\Fixtures; |
39 | 43 | use Google\Cloud\Core\Testing\TestHelpers; |
40 | 44 | use Google\Cloud\Core\Upload\AbstractUploader; |
| 45 | +use GuzzleHttp\Client; |
| 46 | +use GuzzleHttp\Handler\MockHandler; |
| 47 | +use GuzzleHttp\HandlerStack; |
| 48 | +use GuzzleHttp\Psr7\Request; |
| 49 | +use GuzzleHttp\Psr7\Response; |
| 50 | +use GuzzleHttp\Exception\RequestException; |
41 | 51 | use PHPUnit\Framework\TestCase; |
42 | 52 | use Prophecy\Argument; |
43 | 53 | use Prophecy\PhpUnit\ProphecyTrait; |
| 54 | +use Psr\Log\LoggerInterface; |
| 55 | +use stdClass; |
44 | 56 |
|
45 | 57 | /** |
46 | 58 | * @group bigquery |
@@ -698,4 +710,105 @@ public function testGetsJson() |
698 | 710 |
|
699 | 711 | $this->assertInstanceOf(Json::class, $json); |
700 | 712 | } |
| 713 | + |
| 714 | + /** |
| 715 | + * @runInSeparateProcess |
| 716 | + */ |
| 717 | + public function testEnablingLoggerWithFlagLogsToStdOut() |
| 718 | + { |
| 719 | + putenv('GOOGLE_SDK_PHP_LOGGING=true'); |
| 720 | + $client = new BigQueryClient([ |
| 721 | + 'projectId' => self::PROJECT_ID, |
| 722 | + 'keyFilePath' => Fixtures::KEYFILE_STUB_FIXTURE() |
| 723 | + ]); |
| 724 | + |
| 725 | + $output = $this->getActualOutput(); |
| 726 | + $parsed = json_decode($output, true); |
| 727 | + $this->assertNotFalse($parsed); |
| 728 | + $this->assertArrayHasKey('severity', $parsed); |
| 729 | + } |
| 730 | + |
| 731 | + /** |
| 732 | + * @runInSeparateProcess |
| 733 | + */ |
| 734 | + public function testDisableLoggerWithOptionsDoesNotLogToStdOut() |
| 735 | + { |
| 736 | + putenv('GOOGLE_SDK_PHP_LOGGING=true'); |
| 737 | + $client = new BigQueryClient([ |
| 738 | + 'projectId' => self::PROJECT_ID, |
| 739 | + 'keyFilePath' => Fixtures::KEYFILE_STUB_FIXTURE(), |
| 740 | + 'logger' => false |
| 741 | + ]); |
| 742 | + |
| 743 | + $output = $this->getActualOutput(); |
| 744 | + $this->assertEmpty($output); |
| 745 | + } |
| 746 | + |
| 747 | + public function testPassingTheWrongLoggerRaisesAnException() |
| 748 | + { |
| 749 | + $this->expectException(ValidationException::class); |
| 750 | + |
| 751 | + $client = new BigQueryClient([ |
| 752 | + 'projectId' => self::PROJECT_ID, |
| 753 | + 'keyFilePath' => Fixtures::KEYFILE_STUB_FIXTURE(), |
| 754 | + 'logger' => new stdClass() |
| 755 | + ]); |
| 756 | + } |
| 757 | + |
| 758 | + public function testRetryLogging() |
| 759 | + { |
| 760 | + $doneJobResponse = $this->jobResponse; |
| 761 | + $doneJobResponse['status']['state'] = 'DONE'; |
| 762 | + |
| 763 | + $apiMockHandler = new MockHandler([ |
| 764 | + new RequestException( |
| 765 | + 'Transient error', |
| 766 | + new Request('POST', ''), |
| 767 | + new Response(502) |
| 768 | + ), |
| 769 | + new Response(200, [], json_encode($this->jobResponse)), |
| 770 | + new Response(200, [], json_encode($doneJobResponse)) |
| 771 | + ]); |
| 772 | + |
| 773 | + $authMockHandler = new MockHandler([ |
| 774 | + new Response(200, [], json_encode(['access_token' => 'token'])) |
| 775 | + ]); |
| 776 | + |
| 777 | + $retryCallOptionAppeared = false; |
| 778 | + $logger = $this->prophesize(LoggerInterface::class); |
| 779 | + $logger->debug( |
| 780 | + Argument::that(function (string $jsonString) use (&$retryCallOptionAppeared) { |
| 781 | + $jsonParsed = json_decode($jsonString, true); |
| 782 | + if (isset($jsonParsed['jsonPayload']['retryAttempt'])) { |
| 783 | + $retryCallOptionAppeared = true; |
| 784 | + } |
| 785 | + return true; |
| 786 | + }) |
| 787 | + )->shouldBeCalled(); |
| 788 | + |
| 789 | + $credentials = $this->prophesize(FetchAuthTokenInterface::class); |
| 790 | + $credentials->fetchAuthToken(Argument::any()) |
| 791 | + ->willReturn(['access_token' => 'foo']); |
| 792 | + |
| 793 | + $apiHandlerStack = HandlerStack::create($apiMockHandler); |
| 794 | + $apiHttpClient = new Client(['handler' => $apiHandlerStack]); |
| 795 | + |
| 796 | + $authHandlerStack = HandlerStack::create($authMockHandler); |
| 797 | + $authHttpClient = new Client(['handler' => $authHandlerStack]); |
| 798 | + |
| 799 | + $client = new BigQueryClient([ |
| 800 | + 'credentials' => $credentials->reveal(), |
| 801 | + 'projectId' => self::PROJECT_ID, |
| 802 | + 'logger' => $logger->reveal(), |
| 803 | + 'httpHandler' => new Guzzle6HttpHandler($apiHttpClient, $logger->reveal()), |
| 804 | + 'authHttpHandler' => new Guzzle6HttpHandler($authHttpClient) |
| 805 | + ]); |
| 806 | + |
| 807 | + $queryConfig = $client->query( |
| 808 | + 'SELECT * FROM `random_project.random_dataset.random_table`' |
| 809 | + ); |
| 810 | + |
| 811 | + $client->startQuery($queryConfig); |
| 812 | + $this->assertTrue($retryCallOptionAppeared); |
| 813 | + } |
701 | 814 | } |
0 commit comments