-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathBaseCommandTest.php
More file actions
144 lines (116 loc) · 3.92 KB
/
BaseCommandTest.php
File metadata and controls
144 lines (116 loc) · 3.92 KB
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
<?php
declare(strict_types=1);
namespace PHPCR\Tests\Util\Console\Command;
use PHPCR\NodeInterface;
use PHPCR\Query\QueryManagerInterface;
use PHPCR\Query\RowInterface;
use PHPCR\RepositoryInterface;
use PHPCR\SessionInterface;
use PHPCR\Tests\Stubs\MockNode;
use PHPCR\Tests\Stubs\MockRow;
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
use PHPCR\Util\Console\Helper\PhpcrHelper;
use PHPCR\WorkspaceInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;
require_once __DIR__.'/../../../Stubs/MockNode.php';
require_once __DIR__.'/../../../Stubs/MockNodeTypeManager.php';
require_once __DIR__.'/../../../Stubs/MockRow.php';
abstract class BaseCommandTest extends TestCase
{
/**
* @var SessionInterface&MockObject
* */
public $session;
/**
* @var WorkspaceInterface&MockObject
*/
public $workspace;
/**
* @var RepositoryInterface&MockObject
*/
public $repository;
/**
* @var PhpcrConsoleDumperHelper&MockObject
*/
public $dumperHelper;
/**
* @var NodeInterface&MockObject
*/
public $node1;
/**
* @var RowInterface&MockObject
*/
public $row1;
/**
* @var QueryManagerInterface&MockObject
*/
public $queryManager;
/**
* @var HelperSet
*/
public $helperSet;
/**
* @var Application
*/
public $application;
public function setUp(): void
{
$this->session = $this->createMock(SessionInterface::class);
$this->workspace = $this->createMock(WorkspaceInterface::class);
$this->repository = $this->createMock(RepositoryInterface::class);
$this->queryManager = $this->createMock(QueryManagerInterface::class);
$this->row1 = $this->createMock(MockRow::class);
$this->node1 = $this->createMock(MockNode::class);
$this->dumperHelper = $this->getMockBuilder(PhpcrConsoleDumperHelper::class)
->disableOriginalConstructor()
->getMock();
$this->helperSet = new HelperSet([
'phpcr' => new PhpcrHelper($this->session),
'phpcr_console_dumper' => $this->dumperHelper,
]);
$this->session
->method('getWorkspace')
->willReturn($this->workspace);
$this->workspace
->method('getName')
->willReturn('test');
$this->workspace
->method('getQueryManager')
->willReturn($this->queryManager);
$this->queryManager
->method('getSupportedQueryLanguages')
->willReturn(['JCR-SQL2']);
$this->application = new Application();
$this->application->setHelperSet($this->helperSet);
}
protected function addCommand(callable|Command $command): void
{
/* @phpstan-ignore function.alreadyNarrowedType */
if (method_exists($this->application, 'addCommand')) {
$this->application->addCommand($command);
return;
}
if (!$command instanceof Command) {
throw new \InvalidArgumentException('Until we remove support for symfony console < 7.4, all commands must extend the base class');
}
$this->application->add($command);
}
/**
* Build and execute the command tester.
*
* @param mixed[] $arguments
*/
public function executeCommand(string $commandName, array $arguments, int $expectedReturnStatus = 0): CommandTester
{
$command = $this->application->find($commandName);
$commandTester = new CommandTester($command);
$arguments = array_merge(['command' => $command->getName()], $arguments);
$this->assertEquals($expectedReturnStatus, $commandTester->execute($arguments));
return $commandTester;
}
}