FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TimingLoggerTest.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Util;
20 
21 use Mockery as M;
22 
24 {
25  public $timestamp = 3.1415926;
26  public function getTimestamp()
27  {
28  return $this->timestamp;
29  }
30 }
31 
32 class TimingLoggerTest extends \PHPUnit\Framework\TestCase
33 {
34  private $logger;
35 
36  protected function setUp()
37  {
38  $this->logger = M::mock('Monolog\Logger');
39  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
40  }
41 
42  protected function tearDown()
43  {
44  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
45  M::close();
46  }
47 
48  public function testTicToc()
49  {
50  $hackedTimingLogger = new HackedTimingLogger($this->logger);
51  $startTime = 19;
52  $endTime = 42;
53  $text = 'whatever';
54  $expected = sprintf("%s (%.3fms)", $text, ($endTime - $startTime) * 1000);
55  $this->logger->shouldReceive('debug')->with($expected);
56  $hackedTimingLogger->timestamp = $startTime;
57  $hackedTimingLogger->tic();
58  $hackedTimingLogger->timestamp = $endTime;
59  $hackedTimingLogger->toc($text);
60  }
61 
62  public function testTicTocOtherWatch()
63  {
64  $hackedTimingLogger = new HackedTimingLogger($this->logger);
65  $startTime = 19;
66  $endTime = 42;
67  $text = 'whatever';
68  $watch = 'otherWatch';
69  $expected = sprintf("%s (%.3fms)", $text, ($endTime - $startTime) * 1000);
70  $this->logger->shouldReceive('debug')->with($expected);
71  $hackedTimingLogger->timestamp = $startTime;
72  $hackedTimingLogger->tic($watch);
73  $hackedTimingLogger->timestamp = ($startTime+$endTime)/2;
74  $hackedTimingLogger->tic('default');
75  $hackedTimingLogger->timestamp = $endTime;
76  $hackedTimingLogger->toc($text,$watch);
77  }
78 }