1 | <?php |
||
10 | use PHPUnit\Framework\TestCase; |
||
11 | use function microtime; |
||
12 | use function sleep; |
||
13 | |||
14 | class SQLLoggerCollectorTest extends TestCase |
||
15 | { |
||
16 | protected DebugStack $logger; |
||
|
|||
17 | |||
18 | protected string $name = 'test-collector-name'; |
||
19 | |||
20 | protected SQLLoggerCollector $collector; |
||
21 | |||
22 | protected function setUp() : void |
||
23 | { |
||
24 | parent::setUp(); |
||
25 | $this->logger = new DebugStack(); |
||
26 | $this->collector = new SQLLoggerCollector($this->logger, $this->name); |
||
27 | } |
||
28 | |||
29 | public function testHasCorrectName() : void |
||
30 | { |
||
31 | $this->assertSame($this->name, $this->collector->getName()); |
||
32 | } |
||
33 | |||
34 | public function testGetPriority() : void |
||
35 | { |
||
36 | $this->assertIsInt($this->collector->getPriority()); |
||
37 | } |
||
38 | |||
39 | public function testCollect() : void |
||
40 | { |
||
41 | $this->collector->collect(new MvcEvent()); |
||
42 | $this->addToAssertionCount(1); |
||
43 | } |
||
44 | |||
45 | public function testCanHide() : void |
||
46 | { |
||
47 | $this->assertTrue($this->collector->canHide()); |
||
48 | $this->logger->startQuery('some sql'); |
||
49 | $this->assertFalse($this->collector->canHide()); |
||
50 | } |
||
51 | |||
52 | public function testGetQueryCount() : void |
||
53 | { |
||
54 | $this->assertSame(0, $this->collector->getQueryCount()); |
||
55 | $this->logger->startQuery('some sql'); |
||
56 | $this->assertSame(1, $this->collector->getQueryCount()); |
||
57 | $this->logger->startQuery('some more sql'); |
||
58 | $this->assertSame(2, $this->collector->getQueryCount()); |
||
59 | } |
||
60 | |||
61 | public function testGetQueryTime() : void |
||
62 | { |
||
63 | $start = microtime(true); |
||
64 | $this->assertEquals(0, $this->collector->getQueryTime()); |
||
65 | |||
66 | $this->logger->startQuery('some sql'); |
||
67 | sleep(1); |
||
68 | $this->logger->stopQuery(); |
||
69 | $time = microtime(true) - $start; |
||
70 | $time1 = $this->collector->getQueryTime(); |
||
71 | $this->assertGreaterThan(0, $time1); |
||
72 | $this->assertLessThan($time, $time1); |
||
73 | |||
74 | $this->logger->startQuery('some more sql'); |
||
75 | $this->logger->stopQuery(); |
||
76 | $time = microtime(true) - $start; |
||
77 | $time2 = $this->collector->getQueryTime(); |
||
78 | $this->assertGreaterThan($time1, $time2); |
||
79 | $this->assertLessThan($time, $time1); |
||
80 | } |
||
81 | } |
||
82 |