Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.78% covered (success)
97.78%
44 / 45
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
diff_files_test
97.78% covered (success)
97.78%
44 / 45
50.00% covered (danger)
50.00%
1 / 2
9
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 test_diff_files
97.37% covered (success)
97.37%
37 / 38
0.00% covered (danger)
0.00%
0 / 1
8
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14include_once(__DIR__ . '/../../phpBB/includes/diff/diff.php');
15include_once(__DIR__ . '/../../phpBB/includes/diff/engine.php');
16
17class diff_files_test extends phpbb_test_case
18{
19    /**
20     * @var string
21     */
22    protected $filename;
23
24    /**
25     * @var phpbb_mock_container_builder
26     */
27    protected $old_path;
28
29    /**
30     * @var \phpbb\install\helper\config
31     */
32    protected $new_path;
33
34    /**
35     * @var string
36     */
37    protected $path;
38
39    /**
40     * @var array
41     */
42    protected $update_files = [];
43
44    protected function setUp(): void
45    {
46        $this->filename = 'test_files_diff.php';
47        $this->path = __DIR__ . '/fixtures/';
48        $this->old_path = $this->path . 'install/update/old/';
49        $this->new_path = $this->path . 'install/update/new/';
50        $this->update_files = [
51            $this->filename,
52        ];
53    }
54
55    public function test_diff_files()
56    {
57        foreach ($this->update_files as $key => $filename)
58        {
59            $merge_conflicts = $file_contents = [];
60
61            $file_to_diff = $this->old_path . $filename;
62            $file_contents[0] = file_get_contents($file_to_diff);
63            $this->assertNotFalse($file_contents[0], "File $file_to_diff is empty");
64
65            $filenames = [
66                $this->path . $filename,
67                $this->new_path . $filename
68            ];
69
70            foreach ($filenames as $file_to_diff)
71            {
72                $file_contents[] = file_get_contents($file_to_diff);
73                $this->assertNotFalse($file_contents[count($file_contents) - 1], "File $file_to_diff is empty");
74            }
75
76            $diff = new \diff3($file_contents[0], $file_contents[1], $file_contents[2]);
77
78            $file_is_merged = $diff->merged_output() === $file_contents[1];
79
80            // Handle conflicts
81            if ($diff->get_num_conflicts() !== 0)
82            {
83                // Check if current file content is merge of new or original file
84                $tmp = [
85                    'file1'        => $file_contents[1],
86                    'file2'        => implode("\n", $diff->merged_new_output()),
87                ];
88
89                $diff2 = new \diff($tmp['file1'], $tmp['file2']);
90                $empty = $diff2->is_empty();
91
92                if (!$empty)
93                {
94                    unset($tmp, $diff2);
95
96                    // We check if the user merged with his output
97                    $tmp = [
98                        'file1'        => $file_contents[1],
99                        'file2'        => implode("\n", $diff->merged_orig_output()),
100                    ];
101
102                    $diff2 = new \diff($tmp['file1'], $tmp['file2']);
103                    $empty = $diff2->is_empty();
104                }
105
106                unset($diff2);
107
108                if (!$empty && in_array($filename, $merge_conflicts))
109                {
110                    $merge_conflicts[] = $filename;
111                }
112                else
113                {
114                    $file_is_merged = true;
115                }
116            }
117
118            if ($file_is_merged)
119            {
120                unset($this->update_files[$key]);
121            }
122
123            unset($file_contents);
124            unset($diff);
125        }
126
127        $this->assertEquals([], $this->update_files);
128    }
129}