Passed
Push — master ( bc8b6f...7a9b1f )
by Sergei
02:57
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
13
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...
14
{
15
    public function setUp(): void
16
    {
17
        parent::setUp();
18
19
        $mysqlHelper = new MysqlHelper();
20
        $this->db = $mysqlHelper->createConnection();
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
    }
22
23
    protected function tearDown(): void
24
    {
25
        parent::tearDown();
26
27
        $this->db->close();
28
29
        unset($this->db);
30
    }
31
32
    public function testCastValues(): void
33
    {
34
        $this->checkFixture($this->db, 'type');
35
36
        $arClass = new Type($this->db);
37
38
        $arClass->int_col = 123;
39
        $arClass->int_col2 = 456;
40
        $arClass->smallint_col = 42;
41
        $arClass->char_col = '1337';
42
        $arClass->char_col2 = 'test';
43
        $arClass->char_col3 = 'test123';
44
        $arClass->enum_col = 'B';
45
        $arClass->float_col = 3.742;
46
        $arClass->float_col2 = 42.1337;
47
        $arClass->bool_col = true;
48
        $arClass->bool_col2 = false;
49
50
        $arClass->save();
51
52
        /** @var $model Type */
53
        $aqClass = new ActiveQuery(Type::class, $this->db);
54
        $query = $aqClass->onePopulate();
55
56
        $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...
57
        $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...
58
        $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...
59
        $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...
60
        $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...
61
        $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...
62
        $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...
63
    }
64
65
    public function testExplicitPkOnAutoIncrement(): void
66
    {
67
        $this->checkFixture($this->db, 'customer');
68
69
        $customer = new Customer($this->db);
70
71
        $customer->setId(1337);
72
        $customer->setEmail('[email protected]');
73
        $customer->setName('user1337');
74
        $customer->setAddress('address1337');
75
76
        $this->assertTrue($customer->getIsNewRecord());
77
78
        $customer->save();
79
80
        $this->assertEquals(1337, $customer->getId());
81
        $this->assertFalse($customer->getIsNewRecord());
82
    }
83
84
    /**
85
     * {@see https://github.com/yiisoft/yii2/issues/15482}
86
     */
87
    public function testEagerLoadingUsingStringIdentifiers(): void
88
    {
89
        $this->checkFixture($this->db, 'beta');
90
91
        $betaQuery = new ActiveQuery(Beta::class, $this->db);
92
93
        $betas = $betaQuery->with('alpha')->all();
94
95
        $this->assertNotEmpty($betas);
96
97
        $alphaIdentifiers = [];
98
99
        /** @var Beta[] $betas */
100
        foreach ($betas as $beta) {
101
            $this->assertNotNull($beta->getAlpha());
102
            $this->assertEquals($beta->getAlphaStringIdentifier(), $beta->getAlpha()->getStringIdentifier());
103
            $alphaIdentifiers[] = $beta->getAlpha()->getStringIdentifier();
104
        }
105
106
        $this->assertEquals(['1', '01', '001', '001', '2', '2b', '2b', '02'], $alphaIdentifiers);
107
    }
108
}
109