Passed
Pull Request — master (#178)
by Wilmer
11:42
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 testWithSort(): void
25
    {
26
        /** with sql statments select */
27
        $dataProvider = new SqlDataProvider(
28
            $this->getConnection(),
29
            'SELECT * FROM {{customer}} ORDER BY id DESC'
30
        );
31
32
        $models = $dataProvider->getModels();
33
34
        foreach ($models as $model) {
35
            $ids[] = $model['id'];
36
        }
37
38
        $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 34. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
39
40
        /** with {@see Sort::class} options {@see withSort()} */
41
        $ids = [];
42
        $dataProvider = new SqlDataProvider($this->getConnection(), 'SELECT * FROM {{customer}}');
43
        $dataProvider->withSort((new Sort())->attributes(['id'])->defaultOrder(['id' => ['default' => 'desc']]));
44
45
        $models = $dataProvider->getModels();
46
47
        foreach ($models as $model) {
48
            $ids[] = $model['id'];
49
        }
50
51
        $this->assertEquals([3, 2, 1], $ids);
52
    }
53
54
    public function testTotalCount(): void
55
    {
56
        $dataProvider = new SqlDataProvider($this->getConnection(), 'SELECT * FROM {{customer}}');
57
        $this->assertEquals(3, $dataProvider->getTotalCount());
58
    }
59
60
    public function testTotalCountWithParams(): void
61
    {
62
        $dataProvider = new SqlDataProvider(
63
            $this->getConnection(),
64
            'SELECT * FROM {{customer}} WHERE [[id]] > :minimum',
65
            [':minimum' => -1]
66
        );
67
        $this->assertEquals(3, $dataProvider->getTotalCount());
68
    }
69
}
70