-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor BaseActiveRecord::getDirtyAttributes()
#283
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ade14f6
Refactor `BaseActiveRecord::getDirtyAttributes()`
Tigrov 5964f2a
Merge branch 'master' into refactor-getDirtyAttributes
Tigrov 78023fe
Fix AR with properties
Tigrov bd6e59e
Apply fixes from StyleCI
StyleCIBot 4f9a24a
Fix tests
Tigrov 002c356
Improve `BaseActiveRecord::getAttributes()`
Tigrov e4cdc4f
Merge branch 'master' into refactor-getDirtyAttributes
Tigrov b894025
Merge branch 'master' into refactor-getDirtyAttributes
Tigrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
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; | ||
|
@@ -597,14 +598,9 @@ public function testAttributeAccess(): void | |
$this->assertTrue($customer->canGetProperty('orderItems')); | ||
$this->assertFalse($customer->canSetProperty('orderItems')); | ||
|
||
try { | ||
/** @var $itemClass ActiveRecordInterface */ | ||
$customer->orderItems = [new Item($this->db)]; | ||
$this->fail('setter call above MUST throw Exception'); | ||
} catch (Exception $e) { | ||
/** catch exception "Setting read-only property" */ | ||
$this->assertInstanceOf(InvalidCallException::class, $e); | ||
} | ||
$this->expectException(InvalidCallException::class); | ||
$this->expectExceptionMessage('Setting read-only property: ' . Customer::class . '::orderItems'); | ||
$customer->orderItems = [new Item($this->db)]; | ||
|
||
/** related attribute $customer->orderItems didn't change cause it's read-only */ | ||
$this->assertSame([], $customer->orderItems); | ||
|
@@ -836,4 +832,86 @@ public function testToArrayForArrayable(): void | |
]), | ||
); | ||
} | ||
|
||
public function testGetDirtyAttributesOnNewRecord(): void | ||
{ | ||
$this->checkFixture($this->db, 'customer'); | ||
|
||
$customer = new Customer($this->db); | ||
|
||
$this->assertSame([], $customer->getDirtyAttributes()); | ||
|
||
$customer->setAttribute('name', 'Adam'); | ||
$customer->setAttribute('email', '[email protected]'); | ||
$customer->setAttribute('address', null); | ||
|
||
$this->assertEquals( | ||
['name' => 'Adam', 'email' => '[email protected]', 'address' => null], | ||
$customer->getDirtyAttributes() | ||
); | ||
$this->assertEquals( | ||
['email' => '[email protected]', 'address' => null], | ||
$customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']), | ||
); | ||
|
||
$this->assertTrue($customer->save()); | ||
$this->assertSame([], $customer->getDirtyAttributes()); | ||
|
||
$customer->setAttribute('address', ''); | ||
|
||
$this->assertSame(['address' => ''], $customer->getDirtyAttributes()); | ||
} | ||
|
||
public function testGetDirtyAttributesAfterFind(): void | ||
{ | ||
$this->checkFixture($this->db, 'customer'); | ||
|
||
$customerQuery = new ActiveQuery(Customer::class, $this->db); | ||
$customer = $customerQuery->findOne(1); | ||
|
||
$this->assertSame([], $customer->getDirtyAttributes()); | ||
|
||
$customer->setAttribute('name', 'Adam'); | ||
$customer->setAttribute('email', '[email protected]'); | ||
$customer->setAttribute('address', null); | ||
|
||
$this->assertEquals( | ||
['name' => 'Adam', 'email' => '[email protected]', 'address' => null], | ||
$customer->getDirtyAttributes(), | ||
); | ||
$this->assertEquals( | ||
['email' => '[email protected]', 'address' => null], | ||
$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']), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord; | ||
|
||
use Yiisoft\ActiveRecord\ActiveQuery; | ||
use Yiisoft\ActiveRecord\ActiveRecord; | ||
|
||
/** | ||
* Class Customer with defined properties. | ||
*/ | ||
class CustomerWithProperties extends ActiveRecord | ||
{ | ||
protected int $id; | ||
protected string $email; | ||
protected string|null $name = null; | ||
public string|null $address = null; | ||
|
||
public function getTableName(): string | ||
{ | ||
return 'customer'; | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getEmail(): string | ||
{ | ||
return $this->email; | ||
} | ||
|
||
public function getName(): string|null | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getAddress(): string|null | ||
{ | ||
return $this->address; | ||
} | ||
|
||
public function getStatus(): int|null | ||
{ | ||
return $this->getAttribute('status'); | ||
} | ||
|
||
public function getProfile(): ActiveQuery | ||
{ | ||
return $this->hasOne(Profile::class, ['id' => 'profile_id']); | ||
} | ||
|
||
public function getOrders(): ActiveQuery | ||
{ | ||
return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('[[id]]'); | ||
} | ||
|
||
public function setEmail(string $email): void | ||
{ | ||
$this->email = $email; | ||
} | ||
|
||
public function setName(string|null $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function setAddress(string|null $address): void | ||
{ | ||
$this->address = $address; | ||
} | ||
|
||
public function setStatus(int|null $status): void | ||
{ | ||
$this->setAttribute('status', $status); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you bench it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
oldAttributes
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before:
benchGetAttributes......................R1 I1 - Mo20.920μs (±1.00%)
After:
benchGetAttributes......................R2 I2 - Mo17.238μs (±1.78%)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getOldAttributes()
is a different method, without changes.