Passed
Push — pac-827 ( 14ff5e )
by
unknown
09:22
created

DiffCommandTest::testKeyAdded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 1
1
<?php
2
/**
3
 *
4
 * @author    [email protected]
5
 * @copyright 2023 TechDivision GmbH <[email protected]>
6
 * @license   https://opensource.org/licenses/MIT
7
 * @link      https://github.com/techdivision/import-cli-simple
8
 * @link      http://www.techdivision.com
9
 */
10
11
namespace TechDivision\Import\Cli;
12
13
use PHPUnit\Framework\TestCase;
14
use TechDivision\Import\Cli\Command\ImportConfigDiffCommand;
15
16
/**
17
 *
18
 * @author    [email protected]
19
 * @copyright 2023 TechDivision GmbH <[email protected]>
20
 * @license   https://opensource.org/licenses/MIT
21
 * @link      https://github.com/techdivision/import-cli-simple
22
 * @link      http://www.techdivision.com
23
 */
24
class DiffCommandTest extends TestCase
25
{
26
27
    /** @var ImportConfigDiffCommand */
28
    private $importConfigDiffCommand;
29
30
    /**
31
     * @return void
32
     */
33
    public function setUp(): void
34
    {
35
        $this->importConfigDiffCommand = new ImportConfigDiffCommand();
36
    }
37
38
    /**
39
     * @return void
40
     */
41
    public function testPathCreation(): void
42
    {
43
        $expected = include __DIR__ . '/_files/results/diff-result.php';
44
        $operationsFile = __DIR__ . '/_files/test-operations.json';
45
        $values = file_get_contents($operationsFile);
46
        $jsonValues = json_decode($values);
47
        $result = $this->importConfigDiffCommand->getDataAsFlatArray($jsonValues);
48
        $this->assertEquals($expected, $result);
49
    }
50
51
    /**
52
     * @return void
53
     */
54
    public function testKeyAdded(): void
55
    {
56
        $operationsFile = __DIR__ . '/_files/test-operations.json';
57
        $operationsFileAdded = __DIR__ . '/_files/test-project-operations-add.json';
58
        $expected = include __DIR__ . '/_files/results/added-keys-result.php';
59
        $result = $this->getDiffResult($operationsFile, $operationsFileAdded);
60
        $this->assertEquals($expected, $result[ImportConfigDiffCommand::ADD_KEY]);
61
    }
62
63
    /**
64
     * @return void
65
     */
66
    public function testKeyDeleted(): void
67
    {
68
        $operationsFile = __DIR__ . '/_files/test-operations.json';
69
        $operationsFileDelete = __DIR__ . '/_files/test-project-operations-delete.json';
70
        $expected = include __DIR__ . '/_files/results/deleted-key-result.php';
71
        $result = $this->getDiffResult($operationsFile, $operationsFileDelete);
72
        $this->assertEquals($expected, $result[ImportConfigDiffCommand::DELETE_KEY]);
73
    }
74
75
    /**
76
     * @return void
77
     */
78
    public function testKeyChanged(): void
79
    {
80
        $operationsFile = __DIR__ . '/_files/test-operations.json';
81
        $operationsFileChanged = __DIR__ . '/_files/test-project-operations-changed.json';
82
        $expected = include __DIR__ . '/_files/results/changed-key-result.php';
83
        $result = $this->getDiffResult($operationsFile, $operationsFileChanged);
84
        $this->assertEquals($expected, $result[ImportConfigDiffCommand::CHANGED_KEY]);
85
    }
86
87
    /**
88
     * @param string $defaultFile
89
     * @param string $projectFile
90
     * @return array
91
     */
92
    private function getDiffResult(string $defaultFile, string $projectFile): array
93
    {
94
        $values = file_get_contents($defaultFile);
95
        $projectValues = file_get_contents($projectFile);
96
        $jsonValues = json_decode($values);
97
        $projectJsonValues = json_decode($projectValues);
98
        $default = $this->importConfigDiffCommand->getDataAsFlatArray($jsonValues);
99
        $project = $this->importConfigDiffCommand->getDataAsFlatArray($projectJsonValues);
100
        return $this->importConfigDiffCommand->getAllDiffs($default, $project);
101
    }
102
}
103