|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Samples\Logging\Tests; |
| 19 | + |
| 20 | +use Google\Cloud\Samples\Logging\CreateSinkCommand; |
| 21 | +use Google\Cloud\Samples\Logging\DeleteSinkCommand; |
| 22 | +use Symfony\Component\Console\Application; |
| 23 | +use Symfony\Component\Console\Tester\CommandTester; |
| 24 | + |
| 25 | +/** |
| 26 | + * Functional tests for ListSinkCommand. |
| 27 | + */ |
| 28 | +class CreateSinkCommandTest extends \PHPUnit_Framework_TestCase |
| 29 | +{ |
| 30 | + /* @var $hasCredentials boolean */ |
| 31 | + protected static $hasCredentials; |
| 32 | + /* @var $sinkName string */ |
| 33 | + protected static $sinkName; |
| 34 | + /* @var $projectId mixed|string */ |
| 35 | + private $projectId; |
| 36 | + |
| 37 | + public static function setUpBeforeClass() |
| 38 | + { |
| 39 | + $path = getenv('GOOGLE_APPLICATION_CREDENTIALS'); |
| 40 | + self::$hasCredentials = $path && file_exists($path) && |
| 41 | + filesize($path) > 0; |
| 42 | + self::$sinkName = sprintf("sink-%s", uniqid()); |
| 43 | + } |
| 44 | + |
| 45 | + public function setUp() |
| 46 | + { |
| 47 | + if (!self::$hasCredentials) { |
| 48 | + $this->markTestSkipped('No application credentials were found.'); |
| 49 | + } |
| 50 | + |
| 51 | + if (!$this->projectId = getenv('GOOGLE_PROJECT_ID')) { |
| 52 | + $this->markTestSkipped('No project ID'); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function tearDown() |
| 57 | + { |
| 58 | + // Clean up the sink after the test |
| 59 | + $application = new Application(); |
| 60 | + $application->add(new DeleteSinkCommand()); |
| 61 | + $commandTester = new CommandTester($application->get('delete-sink')); |
| 62 | + $commandTester->execute( |
| 63 | + [ |
| 64 | + '--project' => $this->projectId, |
| 65 | + '--sink' => self::$sinkName, |
| 66 | + ], |
| 67 | + ['interactive' => false] |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testCreateSink() |
| 72 | + { |
| 73 | + if (!$bucket = getenv('GOOGLE_BUCKET_NAME')) { |
| 74 | + $this->markTestSkipped('No SINK_BUCKET envvar'); |
| 75 | + } |
| 76 | + $application = new Application(); |
| 77 | + $application->add(new CreateSinkCommand()); |
| 78 | + $commandTester = new CommandTester($application->get('create-sink')); |
| 79 | + $loggerName = 'my_test_logger'; |
| 80 | + $commandTester->execute( |
| 81 | + [ |
| 82 | + '--project' => $this->projectId, |
| 83 | + '--logger' => $loggerName, |
| 84 | + '--bucket' => $bucket, |
| 85 | + '--sink' => self::$sinkName, |
| 86 | + ], |
| 87 | + ['interactive' => false] |
| 88 | + ); |
| 89 | + $this->expectOutputRegex( |
| 90 | + sprintf("/Created a sink '%s'./", self::$sinkName) |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments