-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
TestCase.php
129 lines (106 loc) · 3.51 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
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
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Console\Tests;
use Psr\Container\ContainerInterface;
use ReflectionObject;
use ReflectionException;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
use Yiisoft\Yii\Console\Application;
use Yiisoft\Yii\Console\Command\Serve;
use Yiisoft\Yii\Console\CommandLoader;
use Yiisoft\Yii\Console\SymfonyEventDispatcher;
use Yiisoft\Yii\Console\Tests\Stub\StubCommand;
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
private ?ContainerInterface $container = null;
protected function setUp(): void
{
parent::setUp();
$this->container = $this->createContainer();
}
protected function tearDown(): void
{
$this->container = null;
parent::tearDown();
}
protected function container(): ContainerInterface
{
if ($this->container === null) {
$this->container = $this->createContainer();
}
return $this->container;
}
protected function application(): Application
{
return $this
->container()
->get(Application::class);
}
/**
* Gets an inaccessible object property.
*
* @return mixed
*/
protected function getInaccessibleProperty(object $object, string $propertyName)
{
$reflection = new ReflectionObject($object);
while (!$reflection->hasProperty($propertyName)) {
$reflection = $reflection->getParentClass();
}
$property = $reflection->getProperty($propertyName);
$property->setAccessible(true);
$result = $property->getValue($object);
$property->setAccessible(false);
return $result;
}
/**
* Invokes an inaccessible method.
*
* @throws ReflectionException
*
* @return mixed
*/
protected function invokeMethod(object $object, string $method, array $args = [])
{
$reflection = new ReflectionObject($object);
$method = $reflection->getMethod($method);
$method->setAccessible(true);
$result = $method->invokeArgs($object, $args);
$method->setAccessible(false);
return $result;
}
private function createContainer(): ContainerInterface
{
$dispatcher = new SymfonyEventDispatcher(new SimpleEventDispatcher());
$application = new Application();
$application->setAutoExit(false);
$application->setDispatcher($dispatcher);
$application->addOptions(new InputOption(
'config',
'c',
InputOption::VALUE_REQUIRED,
'Set alternative configuration name'
));
$commandLoader = new CommandLoader(
new SimpleContainer([
Serve::class => new Serve(),
StubCommand::class => new StubCommand($application),
]),
[
'serve' => Serve::class,
'stub|st' => StubCommand::class,
'stub/rename' => StubCommand::class,
],
);
$application->setCommandLoader($commandLoader);
return new SimpleContainer([
Application::class => $application,
EventDispatcherInterface::class => $dispatcher,
CommandLoaderInterface::class => $commandLoader,
]);
}
}