|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
|
|
|