1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Collector; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use DoctrineORMModule\Collector\SQLLoggerCollector; |
7
|
|
|
use Doctrine\DBAL\Logging\DebugStack; |
8
|
|
|
use Laminas\Mvc\MvcEvent; |
9
|
|
|
|
10
|
|
|
class SQLLoggerCollectorTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var DebugStack |
14
|
|
|
*/ |
15
|
|
|
protected $logger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = 'test-collector-name'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var SQLLoggerCollector |
24
|
|
|
*/ |
25
|
|
|
protected $collector; |
26
|
|
|
|
27
|
|
|
public function setUp() : void |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
|
|
|
|
30
|
|
|
$this->logger = new DebugStack(); |
31
|
|
|
$this->collector = new SQLLoggerCollector($this->logger, $this->name); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testHasCorrectName() |
35
|
|
|
{ |
36
|
|
|
$this->assertSame($this->name, $this->collector->getName()); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetPriority() |
40
|
|
|
{ |
41
|
|
|
$this->assertIsInt($this->collector->getPriority()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testCollect() |
45
|
|
|
{ |
46
|
|
|
$this->collector->collect(new MvcEvent()); |
47
|
|
|
$this->addToAssertionCount(1); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testCanHide() |
51
|
|
|
{ |
52
|
|
|
$this->assertTrue($this->collector->canHide()); |
|
|
|
|
53
|
|
|
$this->logger->startQuery('some sql'); |
54
|
|
|
$this->assertFalse($this->collector->canHide()); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetQueryCount() |
58
|
|
|
{ |
59
|
|
|
$this->assertSame(0, $this->collector->getQueryCount()); |
|
|
|
|
60
|
|
|
$this->logger->startQuery('some sql'); |
61
|
|
|
$this->assertSame(1, $this->collector->getQueryCount()); |
|
|
|
|
62
|
|
|
$this->logger->startQuery('some more sql'); |
63
|
|
|
$this->assertSame(2, $this->collector->getQueryCount()); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetQueryTime() |
67
|
|
|
{ |
68
|
|
|
$start = microtime(true); |
69
|
|
|
$this->assertEquals(0, $this->collector->getQueryTime()); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->logger->startQuery('some sql'); |
72
|
|
|
sleep(1); |
73
|
|
|
$this->logger->stopQuery(); |
74
|
|
|
$time = microtime(true) - $start; |
75
|
|
|
$time1 = $this->collector->getQueryTime(); |
76
|
|
|
$this->assertGreaterThan(0, $time1); |
|
|
|
|
77
|
|
|
$this->assertLessThan($time, $time1); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->logger->startQuery('some more sql'); |
80
|
|
|
$this->logger->stopQuery(); |
81
|
|
|
$time = microtime(true) - $start; |
82
|
|
|
$time2 = $this->collector->getQueryTime(); |
83
|
|
|
$this->assertGreaterThan($time1, $time2); |
|
|
|
|
84
|
|
|
$this->assertLessThan($time, $time1); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: