|
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()); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|