FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TimingLoggerTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 
9 namespace Fossology\Lib\Util;
10 
11 use Mockery as M;
12 
14 {
15  public $timestamp = 3.1415926;
16  public function getTimestamp()
17  {
18  return $this->timestamp;
19  }
20 }
21 
22 class TimingLoggerTest extends \PHPUnit\Framework\TestCase
23 {
24  private $logger;
25 
26  protected function setUp() : void
27  {
28  $this->logger = M::mock('Monolog\Logger');
29  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
30  }
31 
32  protected function tearDown() : void
33  {
34  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
35  M::close();
36  }
37 
38  public function testTicToc()
39  {
40  $hackedTimingLogger = new HackedTimingLogger($this->logger);
41  $startTime = 19;
42  $endTime = 42;
43  $text = 'whatever';
44  $expected = sprintf("%s (%.3fms)", $text, ($endTime - $startTime) * 1000);
45  $this->logger->shouldReceive('debug')->with($expected);
46  $hackedTimingLogger->timestamp = $startTime;
47  $hackedTimingLogger->tic();
48  $hackedTimingLogger->timestamp = $endTime;
49  $hackedTimingLogger->toc($text);
50  }
51 
52  public function testTicTocOtherWatch()
53  {
54  $hackedTimingLogger = new HackedTimingLogger($this->logger);
55  $startTime = 19;
56  $endTime = 42;
57  $text = 'whatever';
58  $watch = 'otherWatch';
59  $expected = sprintf("%s (%.3fms)", $text, ($endTime - $startTime) * 1000);
60  $this->logger->shouldReceive('debug')->with($expected);
61  $hackedTimingLogger->timestamp = $startTime;
62  $hackedTimingLogger->tic($watch);
63  $hackedTimingLogger->timestamp = ($startTime+$endTime)/2;
64  $hackedTimingLogger->tic('default');
65  $hackedTimingLogger->timestamp = $endTime;
66  $hackedTimingLogger->toc($text,$watch);
67  }
68 }