Completed
Push — master ( 10ea42...4c1b75 )
by Joschi
03:53
created

AbstractTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertArrayEquals() 0 8 1
C sortArrayForComparison() 0 44 7
A sortArrayRecursive() 0 13 3
A castArray() 0 4 1
1
<?php
2
3
/**
4
 * rdfa-lite
5
 *
6
 * @category    Jkphl
7
 * @package     Jkpl\Rdfalite
8
 * @subpackage  Jkpl\Rdfalite\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\Rdfalite\Tests;
38
39
/**
40
 * Abstract test base class
41
 *
42
 * @package Jkpl\Rdfalite
43
 * @subpackage Jkpl\Rdfalite\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
        // Tests if all array keys are numeric
72
        $allNumeric = true;
73
        foreach (array_keys($array) as $key) {
74
            if (!is_numeric($key)) {
75
                $allNumeric = false;
76
                break;
77
            }
78
        }
79
80
        // If not all keys are numeric: Sort the array by key
81
        if (!$allNumeric) {
82
            ksort($array, SORT_STRING);
83
            return $this->sortArrayRecursive($array);
84
        }
85
86
        // Sort them by data type and value
87
        $array = $this->sortArrayRecursive($array);
88
        usort(
89
            $array,
90
            function (
91
                $first,
92
                $second
93
            ) {
94
                $aType = gettype($first);
95
                $bType = gettype($second);
96
                if ($aType === $bType) {
97
                    switch ($aType) {
98
                        case 'array':
99
                            return strcmp(implode('', array_keys($first)), implode('', array_keys($second)));
100
                        case 'object':
101
                            return strcmp(spl_object_hash($first), spl_object_hash($second));
102
                        default:
103
                            return strcmp(strval($first), strval($second));
104
                    }
105
                }
106
107
                return strcmp($aType, $bType);
108
            }
109
        );
110
111
        return $array;
112
    }
113
114
    /**
115
     * Recursively sort an array for comparison
116
     *
117
     * @param array $array Original array
118
     * @return array Sorted array
119
     */
120
    protected function sortArrayRecursive(array $array)
121
    {
122
123
        // Run through all elements and sort them recursively if they are an array
124
        reset($array);
125
        while (list($key, $value) = each($array)) {
126
            if (is_array($value)) {
127
                $array[$key] = $this->sortArrayForComparison($value);
128
            }
129
        }
130
131
        return $array;
132
    }
133
134
    /**
135
     * Cast a value as a (multidimensional) array
136
     *
137
     * @param mixed $value Value
138
     * @return array Cast value
139
     */
140
    protected function castArray($value)
141
    {
142
        return json_decode(json_encode((array)$value), true);
143
    }
144
}
145