Passed
Pull Request — master (#203)
by Wilmer
02:48
created

testCreateARWithTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests;
6
7
use Yiisoft\ActiveRecord\ActiveQuery;
8
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerQuery;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithConstructor;
11
use Yiisoft\Db\Mysql\ConnectionPDO as ConnectionPDOMysql;
12
13
/**
14
 * @group main
15
 */
16
final class ActiveRecordFactoryTest extends TestCase
17
{
18
    protected string $driverName = 'sqlite';
19
20
    public function testCreateAR(): void
21
    {
22
        $customerAR = $this->arFactory->createAR(Customer::class);
23
24
        $this->assertInstanceOf(Customer::class, $customerAR);
25
    }
26
27
    public function testCreateARWithConnection(): void
28
    {
29
        $customerAR = $this->arFactory->createAR(arClass: Customer::class, db: $this->mysqlConnection);
30
        $db = $this->getInaccessibleProperty($customerAR, 'db', true);
31
32
        $this->assertInstanceOf(ConnectionPDOMysql::class, $db);
33
        $this->assertInstanceOf(Customer::class, $customerAR);
34
    }
35
36
    public function testCreateARWithTableName(): void
37
    {
38
        $customerAR = $this->arFactory->createAR(Customer::class, 'customer');
39
        $tableName = $customerAR->getTableName();
40
41
        $this->assertSame('customer', $tableName);
42
        $this->assertInstanceOf(Customer::class, $customerAR);
43
    }
44
45
    public function testCreateQueryTo(): void
46
    {
47
        /** example create active query */
48
        $customerQuery = $this->arFactory->createQueryTo(Customer::class);
49
50
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
51
52
        /** example create active query custom */
53
        $customerQuery = $this->arFactory->createQueryTo(arClass: Customer::class, queryClass: CustomerQuery::class);
54
55
        $this->assertInstanceOf(CustomerQuery::class, $customerQuery);
56
    }
57
58
    public function testCreateQueryToWithConnection(): void
59
    {
60
        /** example create active query */
61
        $customerQuery = $this->arFactory->createQueryTo(
62
            arClass: Customer::class,
63
            queryClass: CustomerQuery::class,
64
            db: $this->mysqlConnection
65
        );
66
        $db = $this->getInaccessibleProperty($customerQuery, 'db', true);
67
68
        $this->assertInstanceOf(ConnectionPDOMysql::class, $db);
69
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
70
    }
71
72
    public function testCreateQueryToWithTableName(): void
73
    {
74
        /** example create active query */
75
        $customerQuery = $this->arFactory->createQueryTo(arClass: Customer::class, tableName: 'customer');
76
        $tableName = $customerQuery->getARInstance()->getTableName();
0 ignored issues
show
Bug introduced by
The method getARInstance() does not exist on Yiisoft\ActiveRecord\ActiveQueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Yiisoft\ActiveRecord\ActiveQueryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $tableName = $customerQuery->/** @scrutinizer ignore-call */ getARInstance()->getTableName();
Loading history...
77
78
        $this->assertSame('customer', $tableName);
79
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
80
    }
81
82
    public function testGetArInstanceWithConstructor(): void
83
    {
84
        $this->checkFixture($this->sqliteConnection, 'customer', true);
85
86
        $query = $this->arFactory->createQueryTo(CustomerWithConstructor::class);
87
        $customer = $query->one();
88
89
        $this->assertNotNull($customer->profile);
90
    }
91
}
92