Skip to content

Commit

Permalink
Split magical and non-magical implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 22, 2024
1 parent b482833 commit b4fa5f0
Show file tree
Hide file tree
Showing 80 changed files with 4,391 additions and 599 deletions.
175 changes: 89 additions & 86 deletions tests/ActiveQueryFindTest.php

Large diffs are not rendered by default.

547 changes: 278 additions & 269 deletions tests/ActiveQueryTest.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/ActiveRecordFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public function testGetArInstanceWithConstructor(): void
$query = $this->arFactory->createQueryTo(CustomerWithConstructor::class);
$customer = $query->onePopulate();

$this->assertNotNull($customer->profile);
$this->assertNotNull($customer->getProfile());
}
}
181 changes: 89 additions & 92 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithProperties;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\NoExist;
Expand Down Expand Up @@ -86,10 +85,9 @@ public function testStoreEmpty(): void

$record = new NullValues($this->db);

/** this is to simulate empty html form submission */
$record->var1 = '';
$record->var2 = '';
$record->var3 = '';
$record->var1 = null;
$record->var2 = null;
$record->var3 = null;
$record->stringcol = '';
$record->save();

Expand Down Expand Up @@ -128,22 +126,21 @@ public function testOutdatedRelationsAreResetForNewRecords(): void

$orderItem = new OrderItem($this->db);

$orderItem->order_id = 1;
$orderItem->item_id = 3;
$this->assertEquals(1, $orderItem->order->id);
$this->assertEquals(3, $orderItem->item->id);
$orderItem->setOrderId(1);
$orderItem->setItemId(3);
$this->assertEquals(1, $orderItem->getOrder()->getId());
$this->assertEquals(3, $orderItem->getItem()->getId());

/** test `__set()`. */
$orderItem->order_id = 2;
$orderItem->item_id = 1;
$this->assertEquals(2, $orderItem->order->id);
$this->assertEquals(1, $orderItem->item->id);
$orderItem->setOrderId(2);
$orderItem->setItemId(1);
$this->assertEquals(2, $orderItem->getOrder()->getId());
$this->assertEquals(1, $orderItem->getItem()->getId());

/** test `setAttribute()`. */
$orderItem->setAttribute('order_id', 2);
$orderItem->setAttribute('item_id', 2);
$this->assertEquals(2, $orderItem->order->id);
$this->assertEquals(2, $orderItem->item->id);
$this->assertEquals(2, $orderItem->getOrder()->getId());
$this->assertEquals(2, $orderItem->getItem()->getId());
}

public function testDefaultValues(): void
Expand Down Expand Up @@ -243,21 +240,21 @@ public function testNoTablenameReplacement(): void

$customer = new Customer($this->db);

$customer->name = 'Some {{weird}} name';
$customer->email = '[email protected]';
$customer->address = 'Some {{%weird}} address';
$customer->setName('Some {{weird}} name');
$customer->setEmail('[email protected]');
$customer->setAddress('Some {{%weird}} address');
$customer->insert();
$customer->refresh();

$this->assertEquals('Some {{weird}} name', $customer->name);
$this->assertEquals('Some {{%weird}} address', $customer->address);
$this->assertEquals('Some {{weird}} name', $customer->getName());
$this->assertEquals('Some {{%weird}} address', $customer->getAddress());

$customer->name = 'Some {{updated}} name';
$customer->address = 'Some {{%updated}} address';
$customer->setName('Some {{updated}} name');
$customer->setAddress('Some {{%updated}} address');
$customer->update();

$this->assertEquals('Some {{updated}} name', $customer->name);
$this->assertEquals('Some {{%updated}} address', $customer->address);
$this->assertEquals('Some {{updated}} name', $customer->getName());
$this->assertEquals('Some {{%updated}} address', $customer->getAddress());
}

public static function legalValuesForFindByCondition(): array
Expand Down Expand Up @@ -373,23 +370,25 @@ public function testResetNotSavedRelation(): void

$order = new Order($this->db);

$order->customer_id = 1;
$order->created_at = 1_325_502_201;
$order->total = 0;
$order->setCustomerId(1);
$order->setCreatedAt(1_325_502_201);
$order->setTotal(0);

$orderItem = new OrderItem($this->db);

$order->orderItems;
$order->getOrderItems();

$order->populateRelation('orderItems', [$orderItem]);

$order->save();

$this->assertCount(1, $order->orderItems);
$this->assertCount(1, $order->getOrderItems());
}

public function testIssetException(): void
{
self::markTestSkipped('There are no magic properties in the Cat class');

$this->checkFixture($this->db, 'cat');

$cat = new Cat($this->db);
Expand All @@ -400,6 +399,8 @@ public function testIssetException(): void

public function testIssetThrowable(): void
{
self::markTestSkipped('There are no magic properties in the Cat class');

$this->checkFixture($this->db, 'cat');

$cat = new Cat($this->db);
Expand All @@ -410,6 +411,8 @@ public function testIssetThrowable(): void

public function testIssetNonExisting(): void
{
self::markTestSkipped('There are no magic properties in the Cat class');

$this->checkFixture($this->db, 'cat');

$cat = new Cat($this->db);
Expand Down Expand Up @@ -443,13 +446,15 @@ public function testSetAttributes(): void

public function testSetAttributeNoExist(): void
{
self::markTestSkipped('There are no magic properties in the Cat class');

$this->checkFixture($this->db, 'cat');

$cat = new Cat($this->db);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat has no attribute named "noExist"'
'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Cat has no attribute named "noExist"'
);

$cat->setAttribute('noExist', 1);
Expand Down Expand Up @@ -489,9 +494,9 @@ public function testIsAttributeChangedNotChanged(): void

$customer = new Customer($this->db);

$this->assertEmpty($customer->getAttribute('name'));
$this->assertEmpty($customer->getOldAttribute('name'));
$this->assertFalse($customer->isAttributeChanged('name', false));
$this->assertEmpty($customer->getAttribute('email'));
$this->assertEmpty($customer->getOldAttribute('email'));
$this->assertFalse($customer->isAttributeChanged('email', false));
}

public function testTableSchemaException(): void
Expand All @@ -509,17 +514,17 @@ public function testInsert(): void

$customer = new Customer($this->db);

$customer->email = '[email protected]';
$customer->name = 'user4';
$customer->address = 'address4';
$customer->setEmail('[email protected]');
$customer->setName('user4');
$customer->setAddress('address4');

$this->assertNull($customer->id);
$this->assertTrue($customer->isNewRecord);
$this->assertNull($customer->getAttribute('id'));
$this->assertTrue($customer->getIsNewRecord());

$customer->save();

$this->assertNotNull($customer->id);
$this->assertFalse($customer->isNewRecord);
$this->assertNotNull($customer->getId());
$this->assertFalse($customer->getIsNewRecord());
}

/**
Expand All @@ -533,31 +538,33 @@ public function testBooleanAttribute(): void

$customer = new Customer($this->db);

$customer->name = 'boolean customer';
$customer->email = '[email protected]';
$customer->status = true;
$customer->setName('boolean customer');
$customer->setEmail('[email protected]');
$customer->setStatus(1);

$customer->save();
$customer->refresh();
$this->assertEquals(1, $customer->status);
$this->assertEquals(1, $customer->getStatus());

$customer->status = false;
$customer->setStatus(0);
$customer->save();

$customer->refresh();
$this->assertEquals(0, $customer->status);
$this->assertEquals(0, $customer->getStatus());

$customerQuery = new ActiveQuery(Customer::class, $this->db);
$customers = $customerQuery->where(['status' => true])->all();
$customers = $customerQuery->where(['status' => 1])->all();
$this->assertCount(2, $customers);

$customerQuery = new ActiveQuery(Customer::class, $this->db);
$customers = $customerQuery->where(['status' => false])->all();
$customers = $customerQuery->where(['status' => 0])->all();
$this->assertCount(1, $customers);
}

public function testAttributeAccess(): void
{
self::markTestSkipped('There are no magic properties in the Cat class');

$this->checkFixture($this->db, 'customer');

$arClass = new Customer($this->db);
Expand Down Expand Up @@ -639,10 +646,10 @@ public function testRefresh(): void

$customerQuery = new ActiveQuery(Customer::class, $this->db);
$customer = $customerQuery->findOne(1);
$customer->name = 'to be refreshed';
$customer->setName('to be refreshed');

$this->assertTrue($customer->refresh());
$this->assertEquals('user1', $customer->name);
$this->assertEquals('user1', $customer->getName());
}

public function testEquals(): void
Expand Down Expand Up @@ -679,12 +686,12 @@ public function testUnlinkWithViaOnCondition($delete, $count)
$orderQuery = new ActiveQuery(Order::class, $this->db);
$order = $orderQuery->findOne(2);

$this->assertCount(1, $order->itemsFor8);
$order->unlink('itemsFor8', $order->itemsFor8[0], $delete);
$this->assertCount(1, $order->getItemsFor8());
$order->unlink('itemsFor8', $order->getItemsFor8()[0], $delete);

$order = $orderQuery->findOne(2);
$this->assertCount(0, $order->itemsFor8);
$this->assertCount(2, $order->orderItemsWithNullFK);
$this->assertCount(0, $order->getItemsFor8());
$this->assertCount(2, $order->getOrderItemsWithNullFK());

$orderItemQuery = new ActiveQuery(OrderItemWithNullFK::class, $this->db);
$this->assertCount(1, $orderItemQuery->findAll([
Expand All @@ -705,7 +712,7 @@ public function testVirtualRelation()
/** @var Order $order */
$order = $orderQuery->findOne(2);

$order->setVirtualCustomerId($order->customer_id);
$order->setVirtualCustomerId($order->getCustomerId());
$this->assertNotNull($order->getVirtualCustomerQuery());
}

Expand All @@ -722,14 +729,14 @@ public function testJoinWithEager()
$eagerCustomers = $customerQuery->joinWith(['items2'])->all();
$eagerItemsCount = 0;
foreach ($eagerCustomers as $customer) {
$eagerItemsCount += is_countable($customer->items2) ? count($customer->items2) : 0;
$eagerItemsCount += is_countable($customer->getItems2()) ? count($customer->getItems2()) : 0;
}

$customerQuery = new ActiveQuery(Customer::class, $this->db);
$lazyCustomers = $customerQuery->all();
$lazyItemsCount = 0;
foreach ($lazyCustomers as $customer) {
$lazyItemsCount += is_countable($customer->items2) ? count($customer->items2) : 0;
$lazyItemsCount += is_countable($customer->getItems2()) ? count($customer->getItems2()) : 0;
}

$this->assertEquals($eagerItemsCount, $lazyItemsCount);
Expand Down Expand Up @@ -859,7 +866,7 @@ public function testGetOldPrimaryKey(): void
$customerQuery = new ActiveQuery(Customer::class, $this->db);

$customer = $customerQuery->findOne(1);
$customer->id = 2;
$customer->setId(2);

$this->assertSame(1, $customer->getOldPrimaryKey());
$this->assertSame(['id' => 1], $customer->getOldPrimaryKey(true));
Expand All @@ -871,18 +878,38 @@ public function testGetDirtyAttributesOnNewRecord(): void

$customer = new Customer($this->db);

$this->assertSame([], $customer->getDirtyAttributes());
$this->assertSame(
[
'name' => null,
'address' => null,
'status' => 0,
'profile_id' => null,
],
$customer->getDirtyAttributes()
);

$customer->setAttribute('name', 'Adam');
$customer->setAttribute('email', '[email protected]');
$customer->setAttribute('address', null);

$this->assertEquals([], $customer->getDirtyAttributes([]));

$this->assertEquals(
['name' => 'Adam', 'email' => '[email protected]', 'address' => null],
[
'name' => 'Adam',
'email' => '[email protected]',
'address' => null,
'status' => 0,
'profile_id' => null,
],
$customer->getDirtyAttributes()
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
[
'email' => '[email protected]',
'address' => null,
'status' => 0,
],
$customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']),
);

Expand Down Expand Up @@ -916,34 +943,4 @@ public function testGetDirtyAttributesAfterFind(): void
$customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']),
);
}

public function testGetDirtyAttributesWithProperties(): void
{
$this->checkFixture($this->db, 'customer');

$customer = new CustomerWithProperties($this->db);
$this->assertSame([
'name' => null,
'address' => null,
], $customer->getDirtyAttributes());

$customerQuery = new ActiveQuery(CustomerWithProperties::class, $this->db);
$customer = $customerQuery->findOne(1);

$this->assertSame([], $customer->getDirtyAttributes());

$customer->setEmail('[email protected]');
$customer->setName('Adam');
$customer->setAddress(null);
$customer->setStatus(null);

$this->assertEquals(
['email' => '[email protected]', 'name' => 'Adam', 'address' => null, 'status' => null],
$customer->getDirtyAttributes(),
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'unknown']),
);
}
}
Loading

0 comments on commit b4fa5f0

Please sign in to comment.