-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
BaseActiveRecord::getDirtyAttributes()
- Loading branch information
Showing
2 changed files
with
66 additions
and
28 deletions.
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 |
---|---|---|
|
@@ -836,4 +836,56 @@ 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']), | ||
); | ||
|
||
$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']), | ||
); | ||
} | ||
} |