AbstractTest::sortNumericArrayForComparison()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category    Jkphl
7
 * @package     Jkpl\RdfaLiteMicrodata
8
 * @subpackage  Jkpl\RdfaLiteMicrodata\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2017 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 © 2017 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 Jkphl\RdfaLiteMicrodata\Tests;
38
39
/**
40
 * Abstract test base class
41
 *
42
 * @package Jkpl\RdfaLiteMicrodata
43
 * @subpackage Jkpl\RdfaLiteMicrodata\Tests
44
 */
45
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
46
{
47
    /**
48
     * Tests if two arrays equal in their keys and values
49
     *
50
     * @param array $expected Expected result
51
     * @param array $actual Actual result
52
     * @param string $message Message
53
     */
54
    public function assertArrayEquals(array $expected, array $actual, $message = '')
55
    {
56
        $this->assertEquals(
57
            $this->sortArrayForComparison($expected),
58
            $this->sortArrayForComparison($actual),
59
            $message
60
        );
61
    }
62
63
    /**
64
     * Recursively sort an array for comparison with another array
65
     *
66
     * @param array $array Array
67
     * @return array                Sorted array
68
     */
69
    protected function sortArrayForComparison(array $array)
70
    {
71
        // If not all keys are numeric: Sort the array by key
72
        if (!$this->isAllNumericArray($array)) {
73
            ksort($array, SORT_STRING);
74
            return $this->sortArrayRecursive($array);
75
        }
76
77
        return $this->sortNumericArrayForComparison($array);
78
    }
79
80
    /**
81
     * Tests if all keys of an array are numeric
82
     *
83
     * @param array $array Array
84
     * @return bool All keys are numeric
85
     */
86
    protected function isAllNumericArray(array $array)
87
    {
88
        $allNumeric = true;
89
        foreach (array_keys($array) as $key) {
90
            if (!is_numeric($key)) {
91
                $allNumeric = false;
92
                break;
93
            }
94
        }
95
        return $allNumeric;
96
    }
97
98
    /**
99
     * Recursively sort an array for comparison
100
     *
101
     * @param array $array Original array
102
     * @return array Sorted array
103
     */
104
    protected function sortArrayRecursive(array $array)
105
    {
106
        // Run through all elements and sort them recursively if they are an array
107
        reset($array);
108
        while (list($key, $value) = each($array)) {
109
            if (is_array($value)) {
110
                $array[$key] = $this->sortArrayForComparison($value);
111
            }
112
        }
113
114
        return $array;
115
    }
116
117
    /**
118
     * Recursively sort a numeric array for comparison with another array
119
     *
120
     * @param array $array Array
121
     * @return array                Sorted array
122
     */
123
    protected function sortNumericArrayForComparison(array $array)
124
    {
125
        // Sort them by data type and value
126
        $array = $this->sortArrayRecursive($array);
127
        usort($array, [$this, 'compare']);
128
        return $array;
129
    }
130
131
    /**
132
     * Compare values
133
     *
134
     * @param mixed $first First value
135
     * @param mixed $second Second value
136
     * @return int Sorting
137
     */
138
    protected function compare($first, $second)
139
    {
140
        $aType = gettype($first);
141
        $bType = gettype($second);
142
        if ($aType === $bType) {
143
            switch ($aType) {
144
                case 'array':
145
                    return strcmp(implode('', array_keys($first)), implode('', array_keys($second)));
146
                case 'object':
147
                    return strcmp(spl_object_hash($first), spl_object_hash($second));
148
                default:
149
                    return strcmp(strval($first), strval($second));
150
            }
151
        }
152
153
        return strcmp($aType, $bType);
154
    }
155
156
    /**
157
     * Cast a value as a (multidimensional) array
158
     *
159
     * @param mixed $value Value
160
     * @return array Cast value
161
     */
162
    protected function castArray($value)
163
    {
164
        return json_decode(json_encode((array)$value), true);
165
    }
166
}
167