DocumentTest::providerHas()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 8.72
c 0
b 0
f 0
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
namespace EmberDb;
25
26
use PHPUnit\Framework\TestCase;
27
28
class DocumentTest extends TestCase
29
{
30
    public function providerHas()
31
    {
32
        $defaultPreconditions = [
33
            'document' => [
34
                'foo' => [
35
                    'bar' => 'baz'
36
                ]
37
            ]
38
        ];
39
        $defaultExpectations = [];
40
41
        $testCases = [
42
            '1-element subpath of existing 2-element-path' => [
43
                'preconditions' => [
44
                    'path' => 'foo'
45
                ],
46
                'expectations' => [
47
                    'has' => true
48
                ]
49
            ],
50
            'Existing, two-element path' => [
51
                'preconditions' => [
52
                    'path' => 'foo.bar'
53
                ],
54
                'expectations' => [
55
                    'has' => true
56
                ]
57
            ],
58
            'Non-existing, two-element path' => [
59
                'preconditions' => [
60
                    'path' => 'foo.baz'
61
                ],
62
                'expectations' => [
63
                    'has' => false
64
                ]
65
            ],
66
            'Non-existing 3-element-path in a document only containing 2-element-paths' => [
67
                'preconditions' => [
68
                    'path' => 'foo.bar.baz'
69
                ],
70
                'expectations' => [
71
                    'has' => false
72
                ]
73
            ],
74
            'Non-existing 3-element-path in a document only containing 2-element-paths with an empty array' => [
75
                'preconditions' => [
76
                    'document' => [
77
                        'foo' => [
78
                            'bar' => []
79
                        ]
80
                    ],
81
                    'path' => 'foo.bar.baz'
82
                ],
83
                'expectations' => [
84
                    'has' => false
85
                ]
86
            ],
87
        ];
88
89
        // Merge test data with default data
90
        foreach ($testCases as &$testCase) {
91
            $testCase['preconditions'] = array_merge($defaultPreconditions, $testCase['preconditions']);
92
            $testCase['expectations'] = array_merge($defaultExpectations, $testCase['expectations']);
93
        }
94
95
        return $testCases;
96
    }
97
98
99
    /**
100
     * @dataProvider providerHas
101
     */
102
    public function testHas($preconditions, $expectations)
103
    {
104
        $document = new Document($preconditions['document']);
105
        $has = $document->has($preconditions['path']);
106
107
        $this->assertEquals($expectations['has'], $has);
108
    }
109
}
110