-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathBaseTestCaseTest.php
158 lines (135 loc) · 4.86 KB
/
BaseTestCaseTest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Testing\Tests\Functional;
use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerManager;
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
use Doctrine\Bundle\PHPCRBundle\ManagerRegistryInterface;
use Doctrine\Bundle\PHPCRBundle\Test\RepositoryManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Symfony\Cmf\Component\Testing\Functional\DbManager\PHPCR;
use Symfony\Cmf\Component\Testing\Tests\Fixtures\TestTestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
class BaseTestCaseTest extends TestCase
{
/**
* @var ContainerInterface&MockObject
*/
private $container;
/**
* @var KernelInterface&MockObject
*/
private $kernel;
/**
* @var TestTestCase
*/
private $testCase;
/**
* @var KernelBrowser|Client|MockObject
*/
private $client;
protected function setUp(): void
{
$managerRegistry = interface_exists(ManagerRegistryInterface::class)
? $this->createMock(ManagerRegistryInterface::class)
: $this->createMock(ManagerRegistry::class)
;
$this->container = Kernel::MAJOR_VERSION >= 7 ? $this->createMock(Container::class) : $this->createMock(ContainerInterface::class);
$this->container
->method('get')
->willReturnCallback(function ($name) use ($managerRegistry) {
$dic = [
'test.client' => $this->client,
'test.service_container' => $this->container,
'doctrine_phpcr' => $managerRegistry,
'doctrine_phpcr.initializer_manager' => $this->createMock(InitializerManager::class),
];
return $dic[$name];
});
$this->kernel = $this->createMock(KernelInterface::class);
$this->kernel
->method('getContainer')
->willReturn($this->container)
;
$this->kernel
->method('getEnvironment')
->willReturn('phpcr')
;
$this->testCase = new TestTestCase();
$this->testCase->setKernel($this->kernel);
if (class_exists(KernelBrowser::class)) {
$this->client = $this->createMock(KernelBrowser::class);
} else {
$this->client = $this->createMock(Client::class);
}
$this->client
->method('getContainer')
->willReturn($this->container);
}
public function testGetKernel()
{
$class = new \ReflectionClass(BaseTestCase::class);
$method = $class->getMethod('getKernel');
$method->setAccessible(true);
$this->assertInstanceOf(KernelInterface::class, $method->invoke(null));
}
public function testItCanProvideAFrameworkBundleClient()
{
$class = new \ReflectionClass(BaseTestCase::class);
$method = $class->getMethod('getFrameworkBundleClient');
$method->setAccessible(true);
if (class_exists(KernelBrowser::class)) {
$this->assertInstanceOf(KernelBrowser::class, $method->invoke($this->testCase));
} else {
$this->assertInstanceOf(Client::class, $method->invoke($this->testCase));
}
}
public function provideTestDb()
{
return [
['PHPCR', 'PHPCR'],
['Phpcr', 'PHPCR'],
['ORM', 'ORM'],
['foobar', null],
];
}
/**
* @dataProvider provideTestDb
*/
public function testDb($dbName, $expected)
{
$class = new \ReflectionClass(BaseTestCase::class);
$method = $class->getMethod('getDbManager');
$method->setAccessible(true);
if (null === $expected) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($dbName.'" does not exist');
}
$res = $method->invoke($this->testCase, $dbName);
if (null === $expected) {
// do not do assertions if the expected exception has not been thrown.
return;
}
$className = sprintf(
'Symfony\Cmf\Component\Testing\Functional\DbManager\%s',
$expected
);
if (PHPCR::class === $className && class_exists(RepositoryManager::class)) {
$className = RepositoryManager::class;
}
$this->assertInstanceOf($className, $res);
}
}