-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathNodesUpdateCommandTest.php
More file actions
171 lines (141 loc) · 4.6 KB
/
NodesUpdateCommandTest.php
File metadata and controls
171 lines (141 loc) · 4.6 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace PHPCR\Tests\Util\Console\Command;
use PHPCR\Query\QueryInterface;
use PHPCR\Util\Console\Command\NodesUpdateCommand;
use PHPUnit\Framework\MockObject\MockObject;
class NodesUpdateCommandTest extends BaseCommandTest
{
/**
* @var QueryInterface&MockObject
*/
private $query;
public function setUp(): void
{
parent::setUp();
$this->addCommand(new NodesUpdateCommand());
$this->query = $this->createMock(QueryInterface::class);
}
/**
* @return array<array<array<string, mixed>>>
*/
public function provideNodeUpdate(): array
{
return [
// No query specified
[[
'exception' => \InvalidArgumentException::class,
]],
// Specify query
[[
'query' => 'SELECT * FROM nt:unstructured WHERE foo="bar"',
]],
// Set, remote properties and mixins
[[
'setProp' => [['foo', 'bar']],
'removeProp' => ['bar'],
'addMixin' => ['mixin1'],
'removeMixin' => ['mixin1'],
'query' => 'SELECT * FROM nt:unstructured',
]],
];
}
/**
* @param array<string, string> $options
*/
protected function setupQueryManager(array $options): void
{
$options = array_merge(['query' => ''], $options);
$this->session
->method('getWorkspace')
->willReturn($this->workspace);
$this->workspace
->method('getQueryManager')
->willReturn($this->queryManager);
$this->queryManager
->method('createQuery')
->with($options['query'], 'JCR-SQL2')
->willReturn($this->query);
$this->query
->method('execute')
->willReturn([$this->row1]);
$this->row1
->method('getNode')
->willReturn($this->node1);
}
/**
* @dataProvider provideNodeUpdate
*
* @param array<string, mixed> $options
*/
public function testNodeUpdate(array $options): void
{
$options = array_merge([
'query' => null,
'setProp' => [],
'removeProp' => [],
'addMixin' => [],
'removeMixin' => [],
'exception' => null,
], $options);
if ($options['exception']) {
$this->expectException($options['exception']);
}
$this->setupQueryManager($options);
$args = [
'--query' => $options['query'],
'--no-interaction' => true,
'--set-prop' => [],
'--remove-prop' => [],
'--add-mixin' => [],
'--remove-mixin' => [],
];
$setPropertyArguments = [];
foreach ($options['setProp'] as $setProp) {
[$prop, $value] = $setProp;
$setPropertyArguments[] = [$prop, $value, null];
$args['--set-prop'][] = $prop.'='.$value;
}
foreach ($options['removeProp'] as $prop) {
$setPropertyArguments[] = [$prop, null, null];
$args['--remove-prop'][] = $prop;
}
$this->node1
->method('setProperty')
->withConsecutive(...$setPropertyArguments);
foreach ($options['addMixin'] as $mixin) {
$this->node1->expects($this->once())
->method('addMixin')
->with($mixin);
$args['--add-mixin'][] = $mixin;
}
foreach ($options['removeMixin'] as $mixin) {
$this->node1->expects($this->once())
->method('removeMixin')
->with($mixin);
$args['--remove-mixin'][] = $mixin;
}
$this->executeCommand('phpcr:nodes:update', $args);
}
public function testApplyClosure(): void
{
$args = [
'--query' => 'SELECT foo FROM bar',
'--no-interaction' => true,
'--apply-closure' => [
'$session->getNodeByIdentifier("/foo"); $node->setProperty("foo", "bar");',
function ($session, $node): void {
$node->setProperty('foo', 'bar');
},
],
];
$this->setupQueryManager(['query' => 'SELECT foo FROM bar']);
$this->node1->expects($this->exactly(2))
->method('setProperty')
->with('foo', 'bar');
$this->session->expects($this->once())
->method('getNodeByIdentifier')
->with('/foo');
$this->executeCommand('phpcr:nodes:update', $args);
}
}