Completed
Pull Request — master (#602)
by Tom
07:01
created

SQLLoggerCollectorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModuleTest\Collector;
6
7
use Doctrine\DBAL\Logging\DebugStack;
8
use DoctrineORMModule\Collector\SQLLoggerCollector;
9
use Laminas\Mvc\MvcEvent;
10
use PHPUnit\Framework\TestCase;
11
use function microtime;
12
use function sleep;
13
14
class SQLLoggerCollectorTest extends TestCase
15
{
16
    protected DebugStack $logger;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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