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\Sqlite;
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 sqlite
15
 */
16
class ActiveRecordTest extends AbstractActiveRecordTest
17
{
18
    protected string $driverName = 'sqlite';
19
    protected ConnectionInterface $db;
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
25
        $this->db = $this->sqliteConnection;
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        parent::tearDown();
31
32
        $this->sqliteConnection->close();
33
34
        unset($this->sqliteConnection);
35
    }
36
37
    public function testExplicitPkOnAutoIncrement(): void
38
    {
39
        $this->loadFixture($this->db);
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
        $customer->save();
50
51
        $this->assertEquals(1337, $customer->id);
52
        $this->assertFalse($customer->isNewRecord);
53
    }
54
55
    /**
56
     * {@see https://github.com/yiisoft/yii2/issues/15482}
57
     */
58
    public function testEagerLoadingUsingStringIdentifiers(): void
59
    {
60
        $this->checkFixture($this->db, 'beta');
61
62
        $betaQuery = new ActiveQuery(Beta::class, $this->db);
63
64
        $betas = $betaQuery->with('alpha')->all();
65
66
        $this->assertNotEmpty($betas);
67
68
        $alphaIdentifiers = [];
69
70
        /** @var Beta[] $betas */
71
        foreach ($betas as $beta) {
72
            $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...
73
            $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...
74
            $alphaIdentifiers[] = $beta->alpha->string_identifier;
75
        }
76
77
        $this->assertEquals(['1', '01', '001', '001', '2', '2b', '2b', '02'], $alphaIdentifiers);
78
    }
79
}
80