Passed
Pull Request — master (#368)
by Sergei
02:51
created

ActiveRecordTest::testCastValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 31
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql;
6
7
use Yiisoft\ActiveRecord\ActiveQuery;
8
use Yiisoft\ActiveRecord\Tests\Driver\Mysql\Stubs\Type;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
11
use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper;
12
use Yiisoft\Db\Connection\ConnectionInterface;
13
14
final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest
0 ignored issues
show
Bug introduced by
The type Yiisoft\ActiveRecord\Tests\ActiveRecordTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
{
16
    protected function createConnection(): ConnectionInterface
17
    {
18
        return (new MysqlHelper())->createConnection();
19
    }
20
21
    public function testCastValues(): void
22
    {
23
        $this->checkFixture($this->db(), 'type');
24
25
        $arClass = new Type();
26
27
        $arClass->int_col = 123;
28
        $arClass->int_col2 = 456;
29
        $arClass->smallint_col = 42;
30
        $arClass->char_col = '1337';
31
        $arClass->char_col2 = 'test';
32
        $arClass->char_col3 = 'test123';
33
        $arClass->enum_col = 'B';
34
        $arClass->float_col = 3.742;
35
        $arClass->float_col2 = 42.1337;
36
        $arClass->bool_col = true;
37
        $arClass->bool_col2 = false;
38
39
        $arClass->save();
40
41
        /** @var $model Type */
42
        $aqClass = new ActiveQuery(Type::class);
43
        $query = $aqClass->onePopulate();
44
45
        $this->assertSame(123, $query->int_col);
0 ignored issues
show
Bug introduced by
Accessing int_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
        $this->assertSame(456, $query->int_col2);
0 ignored issues
show
Bug introduced by
Accessing int_col2 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
47
        $this->assertSame(42, $query->smallint_col);
0 ignored issues
show
Bug introduced by
Accessing smallint_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
48
        $this->assertSame('1337', trim($query->char_col));
0 ignored issues
show
Bug introduced by
Accessing char_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
        $this->assertSame('test', $query->char_col2);
0 ignored issues
show
Bug introduced by
Accessing char_col2 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
50
        $this->assertSame('test123', $query->char_col3);
0 ignored issues
show
Bug introduced by
Accessing char_col3 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
51
        $this->assertSame('B', $query->enum_col);
0 ignored issues
show
Bug introduced by
Accessing enum_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
    }
53
54
    public function testExplicitPkOnAutoIncrement(): void
55
    {
56
        $this->checkFixture($this->db(), 'customer');
57
58
        $customer = new Customer();
59
60
        $customer->setId(1337);
61
        $customer->setEmail('[email protected]');
62
        $customer->setName('user1337');
63
        $customer->setAddress('address1337');
64
65
        $this->assertTrue($customer->getIsNewRecord());
66
67
        $customer->save();
68
69
        $this->assertEquals(1337, $customer->getId());
70
        $this->assertFalse($customer->getIsNewRecord());
71
    }
72
73
    /**
74
     * {@see https://github.com/yiisoft/yii2/issues/15482}
75
     */
76
    public function testEagerLoadingUsingStringIdentifiers(): void
77
    {
78
        $this->checkFixture($this->db(), 'beta');
79
80
        $betaQuery = new ActiveQuery(Beta::class);
81
82
        $betas = $betaQuery->with('alpha')->all();
83
84
        $this->assertNotEmpty($betas);
85
86
        $alphaIdentifiers = [];
87
88
        /** @var Beta[] $betas */
89
        foreach ($betas as $beta) {
90
            $this->assertNotNull($beta->getAlpha());
91
            $this->assertEquals($beta->getAlphaStringIdentifier(), $beta->getAlpha()->getStringIdentifier());
92
            $alphaIdentifiers[] = $beta->getAlpha()->getStringIdentifier();
93
        }
94
95
        $this->assertEquals(['1', '01', '001', '001', '2', '2b', '2b', '02'], $alphaIdentifiers);
96
    }
97
}
98