Skip to content

Commit 8091bf1

Browse files
committed
Tests added for operations
1 parent 6522e6e commit 8091bf1

File tree

4 files changed

+345
-8
lines changed

4 files changed

+345
-8
lines changed

src/Operation/TestOperation.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public function __construct(
3535
public function apply(NodeValueInterface $input, PointerProcessorInterface $pointerProcessor): NodeValueInterface
3636
{
3737
$selectResult = $pointerProcessor->select($this->pathPointer, $input);
38-
3938
if ($this->matches($selectResult)) {
4039
return $input;
4140
}
@@ -45,12 +44,10 @@ public function apply(NodeValueInterface $input, PointerProcessorInterface $poin
4544

4645
private function matches(PointerResultInterface $selectResult): bool
4746
{
48-
if (!$selectResult->exists()) {
49-
return false;
50-
}
51-
52-
return $this
53-
->equalComparator
54-
->compare($selectResult->get(), $this->value);
47+
return $selectResult->exists()
48+
? $this
49+
->equalComparator
50+
->compare($selectResult->get(), $this->value)
51+
: false;
5552
}
5653
}

tests/Operation/CopyOperationTest.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Remorhaz\JSON\Patch\Test\Operation;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Remorhaz\JSON\Data\Value\NodeValueInterface;
8+
use Remorhaz\JSON\Patch\Operation\CopyOperation;
9+
use Remorhaz\JSON\Pointer\Processor\ProcessorInterface as PointerProcessorInterface;
10+
use Remorhaz\JSON\Pointer\Processor\Result\ResultInterface as PointerResultInterface;
11+
use Remorhaz\JSON\Pointer\Query\QueryInterface as PointerQueryInterface;
12+
13+
/**
14+
* @covers \Remorhaz\JSON\Patch\Operation\CopyOperation
15+
*/
16+
class CopyOperationTest extends TestCase
17+
{
18+
19+
public function testApply_Constructed_PassesInputToPointerProcessorSelect(): void
20+
{
21+
$fromPointer = $this->createMock(PointerQueryInterface::class);
22+
$operation = new CopyOperation(
23+
0,
24+
$this->createMock(PointerQueryInterface::class),
25+
$fromPointer
26+
);
27+
28+
$input = $this->createMock(NodeValueInterface::class);
29+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
30+
31+
$pointerProcessor
32+
->expects(self::once())
33+
->method('select')
34+
->with(self::identicalTo($fromPointer), self::identicalTo($input));
35+
$operation->apply($input, $pointerProcessor);
36+
}
37+
38+
public function testApply_PointerProcessorSelectsValue_PassesSelectedValueToPointerProcessorAdd(): void
39+
{
40+
$pathPointer = $this->createMock(PointerQueryInterface::class);
41+
$operation = new CopyOperation(
42+
0,
43+
$pathPointer,
44+
$this->createMock(PointerQueryInterface::class)
45+
);
46+
47+
$input = $this->createMock(NodeValueInterface::class);
48+
$selectedValue = $this->createMock(NodeValueInterface::class);
49+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
50+
51+
$result = $this->createMock(PointerResultInterface::class);
52+
$result
53+
->method('get')
54+
->willReturn($selectedValue);
55+
$pointerProcessor
56+
->method('select')
57+
->willReturn($result);
58+
$pointerProcessor
59+
->expects(self::once())
60+
->method('add')
61+
->with(self::identicalTo($pathPointer), self::identicalTo($input), self::identicalTo($selectedValue));
62+
$operation->apply($input, $pointerProcessor);
63+
}
64+
65+
public function testApply_PointerProcessorAddsValue_ReturnsValueFromAddResult(): void
66+
{
67+
$operation = new CopyOperation(
68+
0,
69+
$this->createMock(PointerQueryInterface::class),
70+
$this->createMock(PointerQueryInterface::class)
71+
);
72+
73+
$input = $this->createMock(NodeValueInterface::class);
74+
$addedValue = $this->createMock(NodeValueInterface::class);
75+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
76+
77+
$result = $this->createMock(PointerResultInterface::class);
78+
$result
79+
->method('get')
80+
->willReturn($addedValue);
81+
$pointerProcessor
82+
->method('add')
83+
->willReturn($result);
84+
$actualResult = $operation->apply($input, $pointerProcessor);
85+
self::assertSame($addedValue, $actualResult);
86+
}
87+
}

tests/Operation/MoveOperationTest.php

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Remorhaz\JSON\Patch\Test\Operation;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Remorhaz\JSON\Data\Value\NodeValueInterface;
8+
use Remorhaz\JSON\Patch\Operation\MoveOperation;
9+
use Remorhaz\JSON\Pointer\Processor\ProcessorInterface as PointerProcessorInterface;
10+
use Remorhaz\JSON\Pointer\Processor\Result\ResultInterface as PointerResultInterface;
11+
use Remorhaz\JSON\Pointer\Query\QueryInterface as PointerQueryInterface;
12+
13+
/**
14+
* @covers \Remorhaz\JSON\Patch\Operation\MoveOperation
15+
*/
16+
class MoveOperationTest extends TestCase
17+
{
18+
19+
public function testApply_Constructed_PassesInputToPointerProcessorSelect(): void
20+
{
21+
$fromPointer = $this->createMock(PointerQueryInterface::class);
22+
$operation = new MoveOperation(
23+
0,
24+
$this->createMock(PointerQueryInterface::class),
25+
$fromPointer
26+
);
27+
28+
$input = $this->createMock(NodeValueInterface::class);
29+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
30+
31+
$pointerProcessor
32+
->expects(self::once())
33+
->method('select')
34+
->with(self::identicalTo($fromPointer), self::identicalTo($input));
35+
$operation->apply($input, $pointerProcessor);
36+
}
37+
38+
public function testApply_Constructed_PassesInputToPointerProcessorDelete(): void
39+
{
40+
$fromPointer = $this->createMock(PointerQueryInterface::class);
41+
$operation = new MoveOperation(
42+
0,
43+
$this->createMock(PointerQueryInterface::class),
44+
$fromPointer
45+
);
46+
47+
$input = $this->createMock(NodeValueInterface::class);
48+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
49+
50+
$pointerProcessor
51+
->expects(self::once())
52+
->method('delete')
53+
->with(self::identicalTo($fromPointer), self::identicalTo($input));
54+
$operation->apply($input, $pointerProcessor);
55+
}
56+
57+
public function testApply_PointerProcessorSelectsAndDeletesValues_PassesProcessedValuesToPointerProcessor(): void
58+
{
59+
$pathPointer = $this->createMock(PointerQueryInterface::class);
60+
$operation = new MoveOperation(
61+
0,
62+
$pathPointer,
63+
$this->createMock(PointerQueryInterface::class)
64+
);
65+
66+
$input = $this->createMock(NodeValueInterface::class);
67+
$deletedValue = $this->createMock(NodeValueInterface::class);
68+
$selectedValue = $this->createMock(NodeValueInterface::class);
69+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
70+
71+
$selectResult = $this->createMock(PointerResultInterface::class);
72+
$selectResult
73+
->method('get')
74+
->willReturn($selectedValue);
75+
$deleteResult = $this->createMock(PointerResultInterface::class);
76+
$deleteResult
77+
->method('get')
78+
->willReturn($deletedValue);
79+
$pointerProcessor
80+
->method('select')
81+
->willReturn($selectResult);
82+
$pointerProcessor
83+
->method('delete')
84+
->willReturn($deleteResult);
85+
$pointerProcessor
86+
->expects(self::once())
87+
->method('add')
88+
->with(
89+
self::identicalTo($pathPointer),
90+
self::identicalTo($deletedValue),
91+
self::identicalTo($selectedValue)
92+
);
93+
$operation->apply($input, $pointerProcessor);
94+
}
95+
96+
public function testApply_PointerProcessorAddsValues_ReturnsValueFromAddedResult(): void
97+
{
98+
$pathPointer = $this->createMock(PointerQueryInterface::class);
99+
$operation = new MoveOperation(
100+
0,
101+
$pathPointer,
102+
$this->createMock(PointerQueryInterface::class)
103+
);
104+
105+
$input = $this->createMock(NodeValueInterface::class);
106+
$addedValue = $this->createMock(NodeValueInterface::class);
107+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
108+
109+
$addResult = $this->createMock(PointerResultInterface::class);
110+
$addResult
111+
->method('get')
112+
->willReturn($addedValue);
113+
$pointerProcessor
114+
->method('add')
115+
->willReturn($addResult);
116+
$actualValue = $operation->apply($input, $pointerProcessor);
117+
self::assertSame($addedValue, $actualValue);
118+
}
119+
}

tests/Operation/TestOperationTest.php

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Remorhaz\JSON\Patch\Test\Operation;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Remorhaz\JSON\Data\Comparator\ComparatorInterface;
8+
use Remorhaz\JSON\Data\Value\NodeValueInterface;
9+
use Remorhaz\JSON\Patch\Operation\Exception\TestFailedException;
10+
use Remorhaz\JSON\Patch\Operation\TestOperation;
11+
use Remorhaz\JSON\Pointer\Processor\ProcessorInterface as PointerProcessorInterface;
12+
use Remorhaz\JSON\Pointer\Processor\Result\ResultInterface as PointerResultInterface;
13+
use Remorhaz\JSON\Pointer\Query\QueryInterface as PointerQueryInterface;
14+
15+
/**
16+
* @covers \Remorhaz\JSON\Patch\Operation\TestOperation
17+
*/
18+
class TestOperationTest extends TestCase
19+
{
20+
21+
public function testApply_Constructed_PassesInputToPointerProcessor(): void
22+
{
23+
$pathPointer = $this->createMock(PointerQueryInterface::class);
24+
$value = $this->createMock(NodeValueInterface::class);
25+
$comparator = $this->createMock(ComparatorInterface::class);
26+
$operation = new TestOperation(0, $pathPointer, $value, $comparator);
27+
$input = $this->createMock(NodeValueInterface::class);
28+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
29+
30+
$result = $this->createMock(PointerResultInterface::class);
31+
$result
32+
->method('exists')
33+
->willReturn(true);
34+
$comparator
35+
->method('compare')
36+
->willReturn(true);
37+
38+
$pointerProcessor
39+
->expects(self::once())
40+
->method('select')
41+
->with(self::identicalTo($pathPointer), self::identicalTo($input))
42+
->willReturn($result);
43+
$operation->apply($input, $pointerProcessor);
44+
}
45+
46+
public function testApply_PointerProcessorReturnsExistingResult_PassesValueFromResultToComparator(): void
47+
{
48+
$value = $this->createMock(NodeValueInterface::class);
49+
$comparator = $this->createMock(ComparatorInterface::class);
50+
$operation = new TestOperation(
51+
0,
52+
$this->createMock(PointerQueryInterface::class),
53+
$value,
54+
$comparator
55+
);
56+
$input = $this->createMock(NodeValueInterface::class);
57+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
58+
59+
$resultValue = $this->createMock(NodeValueInterface::class);
60+
$result = $this->createMock(PointerResultInterface::class);
61+
$result
62+
->method('exists')
63+
->willReturn(true);
64+
$result
65+
->method('get')
66+
->willReturn($resultValue);
67+
$comparator
68+
->method('compare')
69+
->willReturn(true);
70+
$pointerProcessor
71+
->method('select')
72+
->willReturn($result);
73+
74+
$comparator
75+
->expects(self::once())
76+
->method('compare')
77+
->with(self::identicalTo($resultValue), self::identicalTo($value));
78+
$operation->apply($input, $pointerProcessor);
79+
}
80+
81+
public function testApply_PointerProcessorReturnsNonExistingResult_ThrowsException(): void
82+
{
83+
$comparator = $this->createMock(ComparatorInterface::class);
84+
$operation = new TestOperation(
85+
0,
86+
$this->createMock(PointerQueryInterface::class),
87+
$this->createMock(NodeValueInterface::class),
88+
$comparator
89+
);
90+
$input = $this->createMock(NodeValueInterface::class);
91+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
92+
93+
$result = $this->createMock(PointerResultInterface::class);
94+
$result
95+
->method('exists')
96+
->willReturn(false);
97+
$comparator
98+
->method('compare')
99+
->willReturn(true);
100+
$pointerProcessor
101+
->method('select')
102+
->willReturn($result);
103+
104+
$this->expectException(TestFailedException::class);
105+
$operation->apply($input, $pointerProcessor);
106+
}
107+
108+
public function testApply_ComparatorFailsForExistingResult_ThrowsException(): void
109+
{
110+
$comparator = $this->createMock(ComparatorInterface::class);
111+
$operation = new TestOperation(
112+
0,
113+
$this->createMock(PointerQueryInterface::class),
114+
$this->createMock(NodeValueInterface::class),
115+
$comparator
116+
);
117+
$input = $this->createMock(NodeValueInterface::class);
118+
$pointerProcessor = $this->createMock(PointerProcessorInterface::class);
119+
120+
$result = $this->createMock(PointerResultInterface::class);
121+
$result
122+
->method('exists')
123+
->willReturn(true);
124+
$comparator
125+
->method('compare')
126+
->willReturn(false);
127+
$pointerProcessor
128+
->method('select')
129+
->willReturn($result);
130+
131+
$this->expectException(TestFailedException::class);
132+
$operation->apply($input, $pointerProcessor);
133+
}
134+
}

0 commit comments

Comments
 (0)