Completed
Push — master ( 300f7c...ac8da7 )
by Joschi
06:40
created

AbstractTest::sortArrayForComparison()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
rs 6.7272
cc 7
eloc 27
nc 6
nop 1
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Dev
8
 * @subpackage  Apparat\Dev\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Dev\Tests;
38
39
/**
40
 * Basic tests for generic files
41
 *
42
 * @package     Apparat\Dev
43
 * @subpackage  Apparat\Dev\Tests
44
 */
45
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
46
{
47
    /**
48
     * Temporary FILES
49
     *
50
     * @var array
51
     */
52
    protected $tmpFiles = array();
53
54
    /**
55
     * Tests if two arrays equal in their keys and values
56
     *
57
     * @param array $expected Expected result
58
     * @param array $actual Actual result
59
     * @param string $message Message
60
     */
61
    public function assertArrayEquals(array $expected, array $actual, $message = '')
62
    {
63
        $this->assertEquals(
64
            $this->sortArrayForComparison($expected),
65
            $this->sortArrayForComparison($actual),
66
            $message
67
        );
68
    }
69
70
    /**
71
     * Recursively sort an array for comparison with another array
72
     *
73
     * @param array $array Array
74
     * @return array                Sorted array
75
     */
76
    protected function sortArrayForComparison(array $array)
77
    {
78
        // Tests if all array keys are numeric
79
        $allNumeric = true;
80
        foreach (array_keys($array) as $key) {
81
            if (!is_numeric($key)) {
82
                $allNumeric = false;
83
                break;
84
            }
85
        }
86
87
        // If not all keys are numeric: Sort the array by key
88
        if (!$allNumeric) {
89
            ksort($array, SORT_STRING);
90
            return $this->sortArrayRecursive($array);
91
        }
92
93
        // Sort them by data type and value
94
        $array = $this->sortArrayRecursive($array);
95
        usort(
96
            $array,
97
            function (
98
                $first,
99
                $second
100
            ) {
101
                $aType = gettype($first);
102
                $bType = gettype($second);
103
                if ($aType === $bType) {
104
                    switch ($aType) {
105
                        case 'array':
106
                            return strcmp(implode('', array_keys($first)), implode('', array_keys($second)));
107
                        case 'object':
108
                            return strcmp(spl_object_hash($first), spl_object_hash($second));
109
                        default:
110
                            return strcmp(strval($first), strval($second));
111
                    }
112
                }
113
114
                return strcmp($aType, $bType);
115
            }
116
        );
117
118
        return $array;
119
    }
120
121
    /**
122
     * Recursively sort an array for comparison
123
     *
124
     * @param array $array Original array
125
     * @return array Sorted array
126
     */
127
    protected function sortArrayRecursive(array $array)
128
    {
129
130
        // Run through all elements and sort them recursively if they are an array
131
        reset($array);
132
        while (list($key, $value) = each($array)) {
133
            if (is_array($value)) {
134
                $array[$key] = $this->sortArrayForComparison($value);
135
            }
136
        }
137
138
        return $array;
139
    }
140
141
    /**
142
     * Tears down the fixture
143
     */
144
    protected function tearDown()
145
    {
146
        foreach (array_reverse($this->tmpFiles) as $tmpFile) {
147
            @is_file($tmpFile) ? @unlink($tmpFile) : @rmdir($tmpFile);
148
        }
149
    }
150
151
    /*******************************************************************************
152
     * PRIVATE METHODS
153
     *******************************************************************************/
154
155
    /**
156
     * Prepare and register a temporary file name
157
     *
158
     * @return string Temporary file name
159
     */
160
    protected function createTemporaryFileName()
161
    {
162
        $tempFileName = $this->createTemporaryFile();
163
        unlink($tempFileName);
164
        return $tempFileName;
165
    }
166
167
    /**
168
     * Prepare and register a temporary file
169
     *
170
     * @return string Temporary file name
171
     */
172
    protected function createTemporaryFile()
173
    {
174
        return $this->tmpFiles[] = tempnam(sys_get_temp_dir(), 'apparat_test_');
175
    }
176
177
    /**
178
     * Normalize HTML contents
179
     *
180
     * @param string $html Original HTML
181
     * @return string Normalized HTML
182
     */
183
    protected function normalizeHtml($html)
184
    {
185
        $htmlDom = new \DOMDocument();
186
        $htmlDom->preserveWhiteSpace = false;
187
        $htmlDom->formatOutput = false;
188
        $htmlDom->loadXML("<html><head><title>apparat</title></head><body>$html</body></html>");
189
        return $htmlDom->saveXML();
190
    }
191
}
192