testGetArInstanceWithConstructor()   A
last analyzed

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\ActiveRecordFactory;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\ArrayAndJsonTypes;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
11
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerQuery;
12
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithConstructor;
13
use Yiisoft\ActiveRecord\Tests\Support\Assert;
14
15
abstract class ActiveRecordFactoryTest extends TestCase
16
{
17
    protected ActiveRecordFactory $arFactory;
18
19
    public function testCreateAR(): void
20
    {
21
        $this->assertInstanceOf(Customer::class, $this->arFactory->createAR(Customer::class));
22
    }
23
24
    public function testCreateARWithConnection(): void
25
    {
26
        $customerAR = $this->arFactory->createAR(arClass: Customer::class, db: $this->db);
27
        $db = Assert::invokeMethod($customerAR, 'db');
28
29
        $this->assertSame($this->db, $db);
30
        $this->assertSame('customer', $customerAR->getTableName());
31
        $this->assertInstanceOf(Customer::class, $customerAR);
32
    }
33
34
    public function testCreateARWithTableName(): void
35
    {
36
        $customerAR = $this->arFactory->createAR(ArrayAndJsonTypes::class, 'array_and_json_types');
37
        $tableName = $customerAR->getTableName();
38
39
        $this->assertSame('array_and_json_types', $tableName);
40
        $this->assertInstanceOf(ArrayAndJsonTypes::class, $customerAR);
41
    }
42
43
    public function testCreateQueryTo(): void
44
    {
45
        /** example create active query */
46
        $customerQuery = $this->arFactory->createQueryTo(Customer::class);
47
48
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
49
50
        /** example create active query custom */
51
        $customerQuery = $this->arFactory->createQueryTo(arClass: Customer::class, queryClass: CustomerQuery::class);
52
53
        $this->assertInstanceOf(CustomerQuery::class, $customerQuery);
54
    }
55
56
    public function testCreateQueryToWithConnection(): void
57
    {
58
        /** example create active query */
59
        $customerQuery = $this->arFactory->createQueryTo(
60
            arClass: Customer::class,
61
            queryClass: CustomerQuery::class,
62
            db: $this->db,
63
        );
64
        $db = Assert::inaccessibleProperty($customerQuery, 'db');
65
66
        $this->assertSame($this->db, $db);
67
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
68
    }
69
70
    public function testCreateQueryToWithTableName(): void
71
    {
72
        /** example create active query */
73
        $customerQuery = $this->arFactory->createQueryTo(
74
            arClass: ArrayAndJsonTypes::class,
75
            tableName: 'array_and_json_types',
76
        );
77
        $tableName = $customerQuery->getARInstance()->getTableName();
78
79
        $this->assertSame('array_and_json_types', $tableName);
80
        $this->assertInstanceOf(ActiveQuery::class, $customerQuery);
81
    }
82
83
    public function testGetArInstanceWithConstructor(): void
84
    {
85
        $this->checkFixture($this->db, 'customer', true);
86
87
        $query = $this->arFactory->createQueryTo(CustomerWithConstructor::class);
88
        $customer = $query->onePopulate();
89
90
        $this->assertNotNull($customer->getProfile());
0 ignored issues
show
Bug introduced by
The method getProfile() does not exist on Yiisoft\ActiveRecord\ActiveRecordInterface. It seems like you code against a sub-type of Yiisoft\ActiveRecord\ActiveRecordInterface such as Yiisoft\ActiveRecord\Tes...s\ActiveRecord\Customer or Yiisoft\ActiveRecord\Tes...CustomerWithConstructor. ( Ignorable by Annotation )

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

90
        $this->assertNotNull($customer->/** @scrutinizer ignore-call */ getProfile());
Loading history...
91
    }
92
}
93