Passed
Pull Request — master (#234)
by Wilmer
04:01 queued 01:22
created

ActiveRecordTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
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\ActiveRecordTest as AbstractActiveRecordTest;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
11
use Yiisoft\Db\Connection\ConnectionInterface;
12
13
/**
14
 * @group mysql
15
 */
16
final class ActiveRecordTest extends AbstractActiveRecordTest
17
{
18
    protected string $driverName = 'mysql';
19
    protected ConnectionInterface $db;
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
25
        $this->db = $this->mysqlConnection;
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        parent::tearDown();
31
32
        $this->mysqlConnection->close();
33
34
        unset($this->mysqlConnection);
35
    }
36
37
    public function testExplicitPkOnAutoIncrement(): void
38
    {
39
        $this->checkFixture($this->db, 'customer');
40
41
        $customer = new Customer($this->db);
42
43
        $customer->id = 1337;
44
        $customer->email = '[email protected]';
45
        $customer->name = 'user1337';
46
        $customer->address = 'address1337';
47
48
        $this->assertTrue($customer->isNewRecord);
49
50
        $customer->save();
51
52
        $this->assertEquals(1337, $customer->id);
53
        $this->assertFalse($customer->isNewRecord);
54
    }
55
56
    /**
57
     * {@see https://github.com/yiisoft/yii2/issues/15482}
58
     */
59
    public function testEagerLoadingUsingStringIdentifiers(): void
60
    {
61
        $this->checkFixture($this->db, 'beta');
62
63
        $betaQuery = new ActiveQuery(Beta::class, $this->db);
64
65
        $betas = $betaQuery->with('alpha')->all();
66
67
        $this->assertNotEmpty($betas);
68
69
        $alphaIdentifiers = [];
70
71
        /** @var Beta[] $betas */
72
        foreach ($betas as $beta) {
73
            $this->assertNotNull($beta->alpha);
0 ignored issues
show
Bug introduced by
Accessing alpha on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
74
            $this->assertEquals($beta->alpha_string_identifier, $beta->alpha->string_identifier);
0 ignored issues
show
Bug introduced by
Accessing alpha_string_identifier on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
75
            $alphaIdentifiers[] = $beta->alpha->string_identifier;
76
        }
77
78
        $this->assertEquals(['1', '01', '001', '001', '2', '2b', '2b', '02'], $alphaIdentifiers);
79
    }
80
}
81