<commit_before><?php
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


namespace Google\Cloud\Samples\Auth;

use Symfony\Component\Console\Tester\CommandTester;

/**
 * Unit Tests for transcribe commands.
 */
class CommandTest extends \PHPUnit_Framework_TestCase
{

    public function setUp()
    {
        if (!$creds = getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
            $this->markTestSkipped('Set the GOOGLE_APPLICATION_CREDENTIALS ' .
                'environment variable');
        }
        $this->bucketName = getenv('GCS_BUCKET_NAME');
        $this->projectId = getenv('GCLOUD_PROJECT');
    }

    public function testAuthCloudImplicitCommand()
    {
        $output = $this->runCommand('auth-cloud-implicit');
        $this->assertContains($this->bucketName, $output);
    }

    public function testAuthCloudExplicitCommand()
    {
        $serviceAccountPath = getenv('GOOGLE_APPLICATION_CREDENTIALS');
        $output = $this->runCommand('auth-cloud-explicit', $serviceAccountPath);
        $this->assertContains($this->bucketName, $output);
    }

    public function testAuthCloudExplicitComputeEngineCommand()
    {
        $this->setExpectedException('Google\Cloud\Core\Exception\ServiceException');
        $output = $this->runCommand(
            'auth-cloud-explicit-compute-engine', null, $this->projectId);
    }

    public function testAuthCloudExplicitAppEngineCommand()
    {
        $this->setExpectedException('Google\Cloud\Core\Exception\ServiceException');
        $output = $this->runCommand(
            'auth-cloud-explicit-app-engine', null, $this->projectId);
    }

    private function runCommand($commandName, $serviceAccountPath=null, $projectId=null)
    {
        $application = require __DIR__ . '/../auth.php';
        $command = $application->get($commandName);
        $commandTester = new CommandTester($command);

        ob_start();
        if ($serviceAccountPath) {
            $commandTester->execute(
                [
                    'serviceAccountPath'=> $serviceAccountPath
                ],
                ['interactive' => false]
            );
        } else if ($projectId) {
            $commandTester->execute(
                [
                    'projectId'=> $projectId
                ],
                ['interactive' => false]
            );
        } else {
            $commandTester->execute([], ['interactive' => false]);
        }
        return ob_get_clean();
    }
}<commit_msg>Fixed risky tests for compute/appengine functions<commit_after><?php
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


namespace Google\Cloud\Samples\Auth;

use Symfony\Component\Console\Tester\CommandTester;

/**
 * Unit Tests for transcribe commands.
 */
class CommandTest extends \PHPUnit_Framework_TestCase
{

    public function setUp()
    {
        if (!$creds = getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
            $this->markTestSkipped('Set the GOOGLE_APPLICATION_CREDENTIALS ' .
                'environment variable');
        }
        $this->bucketName = getenv('GCS_BUCKET_NAME');
        $this->projectId = getenv('GCLOUD_PROJECT');
    }

    public function testAuthCloudImplicitCommand()
    {
        $output = $this->runCommand('auth-cloud-implicit');
        $this->assertContains($this->bucketName, $output);
    }

    public function testAuthCloudExplicitCommand()
    {
        $serviceAccountPath = getenv('GOOGLE_APPLICATION_CREDENTIALS');
        $output = $this->runCommand('auth-cloud-explicit', $serviceAccountPath);
        $this->assertContains($this->bucketName, $output);
    }

    public function testAuthCloudExplicitComputeEngineCommand()
    {
        #$this->setExpectedException('Google\Cloud\Core\Exception\ServiceException');
        $output = $this->runCommand(
            'auth-cloud-explicit-compute-engine', null, $this->projectId);
        $this->assertContains('Undefined index: access_token', $output);
    }

    public function testAuthCloudExplicitAppEngineCommand()
    {
        #$this->setExpectedException('Google\Cloud\Core\Exception\ServiceException');
        $output = $this->runCommand(
            'auth-cloud-explicit-app-engine', null, $this->projectId);
        $this->assertContains('Undefined index: access_token', $output);
    }

    private function runCommand($commandName, $serviceAccountPath=null, $projectId=null)
    {
        $application = require __DIR__ . '/../auth.php';
        $command = $application->get($commandName);
        $commandTester = new CommandTester($command);

        ob_start();
        if ($serviceAccountPath) {
            $commandTester->execute(
                [
                    'serviceAccountPath'=> $serviceAccountPath
                ],
                ['interactive' => false]
            );
        } else if ($projectId) {
            try {
                $commandTester->execute(
                    [
                        'projectId'=> $projectId
                    ],
                    ['interactive' => false]
                );
            } catch (\Google\Cloud\Core\Exception\ServiceException $e) {
                ob_get_clean();
                $application->renderException($e, $commandTester->getOutput());
                return $commandTester->getDisplay();
            }
        } else {
            $commandTester->execute([], ['interactive' => false]);
        }
        return ob_get_clean();
    }
}
{'fraction_non_alphanumeric': 0.11778255528255528, 'fraction_numerical': 0.002457002457002457, 'mean_word_length': 2.5243506493506493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2727244', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1702, 'n_words': 386}