clientId = getenv('GOOGLE_CLIENT_ID'); $this->clientSecret = getenv('GOOGLE_CLIENT_SECRET'); // load access token if applicable if ($this->accessToken !== false) { $this->accessToken = $this->checkToken(); } // spoof web host $_SERVER['HTTP_HOST'] = 'testhost'; } /** * @dataProvider provideFileNamesWithIndex */ public function testSyntax($file) { $file = __DIR__ . '/../' . $file; exec(sprintf('php -l %s', escapeshellarg($file)), $output, $return_var); $this->assertEquals(0, $return_var); } /** * @dataProvider provideFileNames * @runInSeparateProcess * @preserveGlobalState disabled * @expectedException Exception * @expectedExceptionMessage please run "composer require google/apiclient:~2.0" */ public function testComposerException($file) { if (!$this->clientId || !$this->clientSecret) { $this->markTestSkipped( 'You must set the GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables' ); } $file = __DIR__ . '/../' . $file; $tmpFile = $this->copyFile($file, false); require $tmpFile; } /** * @dataProvider provideFileNames * @runInSeparateProcess * @preserveGlobalState disabled */ public function testNoClientAuthentication($file) { if (!$this->clientId || !$this->clientSecret) { $this->markTestSkipped( 'You must set the GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables' ); } $file = __DIR__ . '/../' . $file; $contents = file_get_contents($file); if (false === strpos($contents, '$OAUTH2_CLIENT_ID')) { // no client authentication required in this file return; } $output = $this->runFile($file); $this->assertContains( 'You need to set $OAUTH2_CLIENT_ID and', $output ); } /** * @dataProvider provideFileNames * @runInSeparateProcess * @preserveGlobalState disabled */ public function testAuthorizeAccess($file) { if (!$this->clientId || !$this->clientSecret) { $this->markTestSkipped( 'You must set the GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables' ); } $file = __DIR__ . '/../' . $file; $contents = file_get_contents($file); if (false === strpos($contents, '$OAUTH2_CLIENT_ID')) { // no client authentication required in this file return; } $tmpFile = $this->copyFile($file); $this->addClientCredentialsToFile($tmpFile); $output = $this->runFile($tmpFile); $this->assertContains( 'You need to', $output ); $this->assertContains( 'authorize access before proceeding.

', $output ); } /** * @dataProvider provideFileNames * @runInSeparateProcess * @preserveGlobalState disabled */ public function testWithSessionToken($file) { if (!$this->clientId || !$this->clientSecret) { $this->markTestSkipped( 'You must set the GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables' ); } if (!$this->accessToken) { $this->markTestSkipped( 'Could not fetch access token' ); } $file = __DIR__ . '/../' . $file; $contents = file_get_contents($file); if (false === strpos($contents, '$tokenSessionKey')) { // no token authentication required in this file return; } $tmpFile = $this->copyFile($file); $this->addClientCredentialsToFile($tmpFile); $this->addTokenToFile($tmpFile, $this->accessToken); $output = $this->runFile($tmpFile); var_dump($output); $this->assertNotContains( 'You need to clientId && $this->clientSecret) { $path = sys_get_temp_dir().'/youtube-api-php-tests'; if (file_exists($path)) { if ($token = json_decode(file_get_contents($path), true)) { if ($token['created'] + $token['expires_in'] > time()) { return $token; } } } if ($token = $this->tryToGetAnAccessToken()) { file_put_contents($path, json_encode($token)); return $token; } } return false; } private function tryToGetAnAccessToken() { $client = new Google_Client(); $client->setApplicationName('youtube-php-tests'); $client->addScope('https://www.googleapis.com/auth/youtube'); $client->setClientId($this->clientId); $client->setClientSecret($this->clientSecret); $client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob"); $client->setConfig('access_type', 'offline'); $authUrl = $client->createAuthUrl(); echo "\nPlease enter the auth code:\n"; ob_flush(); `open '$authUrl'`; $authCode = trim(fgets(STDIN)); if ($accessToken = $client->fetchAccessTokenWithAuthCode($authCode)) { if (isset($accessToken['access_token'])) { return $accessToken; } } } public function tearDown() { foreach ($this->tmpFiles as $file) { unlink($file); } } }