Completed
Pull Request — master (#4)
by Timóteo
05:46
created

ModelWrapperTest::methodNamesData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 74
rs 9.232
c 0
b 0
f 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
declare(strict_types=1);
4
5
namespace TZachi\PhalconRepository\Tests\Unit;
6
7
use Phalcon\DiInterface;
8
use Phalcon\Mvc\Model;
9
use Phalcon\Mvc\Model\Criteria;
10
use Phalcon\Mvc\Model\Resultset\Simple as SimpleResultset;
11
use PHPUnit\Framework\TestCase;
12
use TZachi\PhalconRepository\ModelWrapper;
13
use TZachi\PhalconRepository\Tests\Mock\Model\Wrapper;
14
15
class ModelWrapperTest extends TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function constructorShouldAssignModelName(): void
21
    {
22
        $modelClass = Wrapper::class;
23
24
        $modelWrapper = new ModelWrapper($modelClass);
25
26
        self::assertSame($modelClass, $modelWrapper->getModelName());
27
    }
28
29
    /**
30
     * @test
31
     * @dataProvider methodNamesData
32
     *
33
     * @param mixed[] $args
34
     * @param mixed   $returnValue
35
     */
36
    public function allMethodsInWrapperShouldReturnSameValueAsModel(string $methodName, array $args, $returnValue): void
37
    {
38
        Wrapper::$methodName  = $methodName;
39
        Wrapper::$args        = $args;
40
        Wrapper::$returnValue = $returnValue;
41
42
        $modelWrapper = new ModelWrapper(Wrapper::class);
43
44
        static::assertSame($returnValue, $modelWrapper->{$methodName}(...$args));
45
    }
46
47
    /**
48
     * @return mixed[]
49
     */
50
    public function methodNamesData(): array
51
    {
52
        $defaultArgs = [
53
            [
54
                'conditions' => 'field1 => :value:',
55
                'binds' => ['value' => 1],
56
            ],
57
        ];
58
59
        return [
60
            [
61
                'methodName' => 'findFirst',
62
                'args' => $defaultArgs,
63
                'returnData' => $this->createMock(Model::class),
64
            ],
65
            [
66
                'methodName' => 'findFirst',
67
                'args' => $defaultArgs,
68
                'returnData' => false,
69
            ],
70
            [
71
                'methodName' => 'find',
72
                'args' => $defaultArgs,
73
                'returnData' => $this->createMock(SimpleResultset::class),
74
            ],
75
            [
76
                'methodName' => 'query',
77
                'args' => [$this->createMock(DiInterface::class)],
78
                'returnData' => $this->createMock(Criteria::class),
79
            ],
80
            [
81
                'methodName' => 'count',
82
                'args' => $defaultArgs,
83
                'returnData' => 10,
84
            ],
85
            [
86
                'methodName' => 'sum',
87
                'args' => $defaultArgs,
88
                'returnData' => null,
89
            ],
90
            [
91
                'methodName' => 'sum',
92
                'args' => $defaultArgs,
93
                'returnData' => '11.4',
94
            ],
95
            [
96
                'methodName' => 'average',
97
                'args' => $defaultArgs,
98
                'returnData' => null,
99
            ],
100
            [
101
                'methodName' => 'average',
102
                'args' => $defaultArgs,
103
                'returnData' => '20.42',
104
            ],
105
            [
106
                'methodName' => 'minimum',
107
                'args' => $defaultArgs,
108
                'returnData' => null,
109
            ],
110
            [
111
                'methodName' => 'minimum',
112
                'args' => $defaultArgs,
113
                'returnData' => '2019-01-01',
114
            ],
115
            [
116
                'methodName' => 'maximum',
117
                'args' => $defaultArgs,
118
                'returnData' => null,
119
            ],
120
            [
121
                'methodName' => 'maximum',
122
                'args' => $defaultArgs,
123
                'returnData' => '2019-01-01',
124
            ],
125
        ];
126
    }
127
}
128