Passed
Pull Request — master (#178)
by Wilmer
12:09
created

testTotalCountWithParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestUtility;
6
7
use Yiisoft\Db\Data\SqlDataProvider;
8
use Yiisoft\Db\Data\Sort;
9
10
trait TestSqlDataProviderTrait
11
{
12
    public function testGetModels(): void
13
    {
14
        $dataProvider = new SqlDataProvider($this->getConnection(), 'SELECT * FROM {{customer}}');
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        $dataProvider = new SqlDataProvider($this->/** @scrutinizer ignore-call */ getConnection(), 'SELECT * FROM {{customer}}');
Loading history...
15
        $this->assertCount(3, $dataProvider->getModels());
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        $this->/** @scrutinizer ignore-call */ 
16
               assertCount(3, $dataProvider->getModels());
Loading history...
16
    }
17
18
    public function testGetKeys(): void
19
    {
20
        $dataProvider = new SqlDataProvider($this->getConnection(), 'SELECT * FROM {{customer}}');
21
        $this->assertEquals([0, 1, 2], $dataProvider->getKeys());
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $this->/** @scrutinizer ignore-call */ 
22
               assertEquals([0, 1, 2], $dataProvider->getKeys());
Loading history...
22
    }
23
24
    public function testSort(): void
25
    {
26
        $dataProvider = new SqlDataProvider(
27
            $this->getConnection(),
28
            'SELECT * FROM {{customer}} ORDER BY id DESC'
29
        );
30
31
        $models = $dataProvider->getModels();
32
33
        foreach ($models as $model) {
34
            $ids[] = $model['id'];
35
        }
36
37
        $this->assertEquals([3, 2, 1], $ids);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ids seems to be defined by a foreach iteration on line 33. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
38
39
        $ids = [];
40
        $dataProvider = new SqlDataProvider($this->getConnection(), 'SELECT * FROM {{customer}}');
41
        $dataProvider->withSort((new Sort(['id']))->defaultOrder(['id' => ['default' => 'desc']]));
0 ignored issues
show
Bug introduced by
new Yiisoft\Db\Data\Sort...('default' => 'desc'))) of type Yiisoft\Db\Data\Sort is incompatible with the type array expected by parameter $value of Yiisoft\Db\Data\DataProvider::withSort(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $dataProvider->withSort(/** @scrutinizer ignore-type */ (new Sort(['id']))->defaultOrder(['id' => ['default' => 'desc']]));
Loading history...
Unused Code introduced by
The call to Yiisoft\Db\Data\Sort::__construct() has too many arguments starting with array('id'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $dataProvider->withSort((/** @scrutinizer ignore-call */ new Sort(['id']))->defaultOrder(['id' => ['default' => 'desc']]));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
43
        $models = $dataProvider->getModels();
44
45
        foreach ($models as $model) {
46
            $ids[] = $model['id'];
47
        }
48
49
        $this->assertEquals([3, 2, 1], $ids);
50
    }
51
52
    public function testTotalCount(): void
53
    {
54
        $dataProvider = new SqlDataProvider($this->getConnection(), 'SELECT * FROM {{customer}}');
55
        $this->assertEquals(3, $dataProvider->getTotalCount());
56
    }
57
58
    public function testTotalCountWithParams(): void
59
    {
60
        $dataProvider = new SqlDataProvider(
61
            $this->getConnection(),
62
            'SELECT * FROM {{customer}} WHERE [[id]] > :minimum',
63
            [':minimum' => -1]
64
        );
65
        $this->assertEquals(3, $dataProvider->getTotalCount());
66
    }
67
}
68