|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2015 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 | +use google\appengine\api\log\LogService; |
| 19 | + |
| 20 | +class loggingTest extends PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + public function testNoLogs() |
| 23 | + { |
| 24 | + ob_start(); |
| 25 | + include __DIR__ . '/../index.php'; |
| 26 | + $result = ob_get_contents(); |
| 27 | + ob_end_clean(); |
| 28 | + // Make sure it looks like Shakespeare. |
| 29 | + $this->assertContains('No logs!', $result); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSomeLogs() |
| 33 | + { |
| 34 | + $log1 = $this->getMock('google\appengine\api\log\RequestLog'); |
| 35 | + $applog1 = $this->getMock('google\appengine\api\log\AppLogLine'); |
| 36 | + $expectedLog = [ |
| 37 | + [ 'method' => 'getIp', 'return' => '127.0.0.1' ], |
| 38 | + [ 'method' => 'getStatus', 'return' => 'log-status-1' ], |
| 39 | + [ 'method' => 'getMethod', 'return' => 'log-method-1' ], |
| 40 | + [ 'method' => 'getResource', 'return' => 'log-resource-1' ], |
| 41 | + [ 'method' => 'getEndDateTime', 'return' => $d1 = new DateTime() ], |
| 42 | + [ 'method' => 'getAppLogs', 'return' => [ $applog1 ] ], |
| 43 | + ]; |
| 44 | + $expectedAppLog = [ |
| 45 | + [ 'method' => 'getMessage', 'return' => 'applog-message-1' ], |
| 46 | + [ 'method' => 'getDateTime', 'return' => $d2 = new DateTime('-1 hour') ], |
| 47 | + ]; |
| 48 | + foreach ($expectedLog as $expected) { |
| 49 | + $log1->expects($this->once()) |
| 50 | + ->method($expected['method']) |
| 51 | + ->will($this->returnValue($expected['return'])); |
| 52 | + } |
| 53 | + foreach ($expectedAppLog as $expected) { |
| 54 | + $applog1->expects($this->once()) |
| 55 | + ->method($expected['method']) |
| 56 | + ->will($this->returnValue($expected['return'])); |
| 57 | + } |
| 58 | + |
| 59 | + LogService::$logs = [ $log1 ]; |
| 60 | + ob_start(); |
| 61 | + include __DIR__ . '/../index.php'; |
| 62 | + $result = ob_get_contents(); |
| 63 | + ob_end_clean(); |
| 64 | + // Make sure it looks like Shakespeare. |
| 65 | + $this->assertContains('127.0.0.1', $result); |
| 66 | + $this->assertContains('log-status-1', $result); |
| 67 | + $this->assertContains('log-method-1', $result); |
| 68 | + $this->assertContains('log-resource-1', $result); |
| 69 | + $this->assertContains($d1->format('c'), $result); |
| 70 | + $this->assertContains('applog-message-1', $result); |
| 71 | + $this->assertContains($d2->format('c'), $result); |
| 72 | + } |
| 73 | +} |
0 commit comments