forked from rialto-php/puphpeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
70 lines (55 loc) · 2.07 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Nesk\Puphpeteer\Tests;
use Monolog\Logger;
use ReflectionClass;
use Psr\Log\LogLevel;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\TestCase as BaseTestCase;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
class TestCase extends BaseTestCase
{
private $dontPopulateProperties = [];
public function setUp(): void
{
parent::setUp();
$testMethod = new \ReflectionMethod($this, $this->getName());
$docComment = $testMethod->getDocComment();
if (preg_match('/@dontPopulateProperties (.*)/', $docComment, $matches)) {
$this->dontPopulateProperties = array_values(array_filter(explode(' ', $matches[1])));
}
}
public function canPopulateProperty(string $propertyName): bool
{
return !in_array($propertyName, $this->dontPopulateProperties);
}
public function loggerMock($expectations) {
$loggerMock = $this->getMockBuilder(Logger::class)
->setConstructorArgs(['rialto'])
->setMethods(['log'])
->getMock();
if ($expectations instanceof Invocation) {
$expectations = [func_get_args()];
}
foreach ($expectations as $expectation) {
[$matcher] = $expectation;
$with = array_slice($expectation, 1);
$loggerMock->expects($matcher)
->method('log')
->with(...$with);
}
return $loggerMock;
}
public function isLogLevel(): Callback {
$psrLogLevels = (new ReflectionClass(LogLevel::class))->getConstants();
$monologLevels = (new ReflectionClass(Logger::class))->getConstants();
$monologLevels = array_intersect_key($monologLevels, $psrLogLevels);
return $this->callback(function ($level) use ($psrLogLevels, $monologLevels) {
if (is_string($level)) {
return in_array($level, $psrLogLevels, true);
} else if (is_int($level)) {
return in_array($level, $monologLevels, true);
}
return false;
});
}
}