Passed
Pull Request — master (#203)
by Alexander
05:12 queued 02:19
created

testCreateQueryToWithTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
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(Customer::class, null, $this->mysqlConnection);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $tableName of Yiisoft\ActiveRecord\Act...cordFactory::createAR(). ( Ignorable by Annotation )

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

29
        $customerAR = $this->arFactory->createAR(Customer::class, /** @scrutinizer ignore-type */ null, $this->mysqlConnection);
Loading history...
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(Customer::class, 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(Customer::class, CustomerQuery::class, $this->mysqlConnection);
0 ignored issues
show
Bug introduced by
$this->mysqlConnection of type Yiisoft\Db\Mysql\ConnectionPDO is incompatible with the type string expected by parameter $queryClass of Yiisoft\ActiveRecord\Act...actory::createQueryTo(). ( Ignorable by Annotation )

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

61
        $customerQuery = $this->arFactory->createQueryTo(Customer::class, CustomerQuery::class, /** @scrutinizer ignore-type */ $this->mysqlConnection);
Loading history...
62
        $db = $this->getInaccessibleProperty($customerQuery, 'db', true);
63
64
        $this->assertInstanceOf(ConnectionPDOMysql::class, $db);
65
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
66
    }
67
68
    public function testCreateQueryToWithTableName(): void
69
    {
70
        /** example create active query */
71
        $customerQuery = $this->arFactory->createQueryTo(arClass: Customer::class, tableName: 'customer');
72
        $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

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