-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to include soft deleted models
- Loading branch information
Showing
4 changed files
with
213 additions
and
0 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
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 |
---|---|---|
|
@@ -255,6 +255,79 @@ public function testValidationPassesIfRuleChecksThatFactExistsAndBelongsToUserUs | |
$this->assertCount(1, Fact::all()); | ||
} | ||
|
||
/* | ||
* Tests for includeSoftDeleted | ||
*/ | ||
|
||
public function testValidationSucceedsIfSoftDeletedEntryExistInDatabaseAndIncludeSoftDeletedFlagIsActive(): void | ||
{ | ||
// Arrange | ||
User::create([ | ||
'id' => 6, | ||
'other_id' => null, | ||
'name' => 'Testname', | ||
'email' => '[email protected]', | ||
'password' => bcrypt('secret'), | ||
'remember_token' => Str::random(10), | ||
]); | ||
$fact = Fact::create([ | ||
'id' => 1, | ||
'user_id' => 6, | ||
'type' => 'type1', | ||
'description' => 'Long desc', | ||
]); | ||
$fact->delete(); | ||
|
||
$validator = Validator::make([ | ||
'id' => 1, | ||
], [ | ||
'id' => [(new ExistsEloquent(Fact::class))->includeSoftDeleted()] | ||
]); | ||
|
||
// Act | ||
$isValid = $validator->passes(); | ||
$messages = $validator->messages()->toArray(); | ||
|
||
// Assert | ||
$this->assertTrue($isValid); | ||
$this->assertArrayNotHasKey('id', $messages); | ||
$this->assertCount(1, User::withTrashed()->get()); | ||
$this->assertCount(1, User::all()); | ||
$this->assertCount(1, Fact::withTrashed()->get()); | ||
$this->assertCount(0, Fact::all()); | ||
} | ||
|
||
public function testValidationFailsIfSoftDeletedEntryDoesNotExistInDatabaseAndIncludeSoftDeletedFlagIsActive(): void | ||
{ | ||
// Arrange | ||
User::create([ | ||
'id' => 6, | ||
'other_id' => null, | ||
'name' => 'Testname', | ||
'email' => '[email protected]', | ||
'password' => bcrypt('secret'), | ||
'remember_token' => Str::random(10), | ||
]); | ||
|
||
$validator = Validator::make([ | ||
'id' => 1, | ||
], [ | ||
'id' => [(new ExistsEloquent(Fact::class))->includeSoftDeleted()] | ||
]); | ||
|
||
// Act | ||
$isValid = $validator->passes(); | ||
$messages = $validator->messages()->toArray(); | ||
|
||
// Assert | ||
$this->assertFalse($isValid); | ||
$this->assertEquals('The resource does not exist.', $messages['id'][0]); | ||
$this->assertCount(1, User::withTrashed()->get()); | ||
$this->assertCount(1, User::all()); | ||
$this->assertCount(0, Fact::withTrashed()->get()); | ||
$this->assertCount(0, Fact::all()); | ||
} | ||
|
||
/* | ||
* Test language support | ||
*/ | ||
|
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 |
---|---|---|
|
@@ -257,6 +257,79 @@ public function testValidationFailsIfRuleChecksThatFactExistsAndBelongsToUserUsi | |
$this->assertCount(1, Fact::all()); | ||
} | ||
|
||
/* | ||
* Tests for includeSoftDeleted | ||
*/ | ||
|
||
public function testValidationSucceedsIfSoftDeletedEntryDoesNotExistInDatabaseAndIncludeSoftDeletedFlagIsActive(): void | ||
{ | ||
// Arrange | ||
User::create([ | ||
'id' => 6, | ||
'other_id' => null, | ||
'name' => 'Testname', | ||
'email' => '[email protected]', | ||
'password' => bcrypt('secret'), | ||
'remember_token' => Str::random(10), | ||
]); | ||
|
||
$validator = Validator::make([ | ||
'id' => 1, | ||
], [ | ||
'id' => [(new UniqueEloquent(Fact::class))->includeSoftDeleted()] | ||
]); | ||
|
||
// Act | ||
$isValid = $validator->passes(); | ||
$messages = $validator->messages()->toArray(); | ||
|
||
// Assert | ||
$this->assertTrue($isValid); | ||
$this->assertArrayNotHasKey('id', $messages); | ||
$this->assertCount(1, User::withTrashed()->get()); | ||
$this->assertCount(1, User::all()); | ||
$this->assertCount(0, Fact::withTrashed()->get()); | ||
$this->assertCount(0, Fact::all()); | ||
} | ||
|
||
public function testValidationFailsIfSoftDeletedEntryDoesExistInDatabaseAndIncludeSoftDeletedFlagIsActive(): void | ||
{ | ||
// Arrange | ||
User::create([ | ||
'id' => 6, | ||
'other_id' => null, | ||
'name' => 'Testname', | ||
'email' => '[email protected]', | ||
'password' => bcrypt('secret'), | ||
'remember_token' => Str::random(10), | ||
]); | ||
$fact = Fact::create([ | ||
'id' => 1, | ||
'user_id' => 6, | ||
'type' => 'type1', | ||
'description' => 'Long desc', | ||
]); | ||
$fact->delete(); | ||
|
||
$validator = Validator::make([ | ||
'id' => 1, | ||
], [ | ||
'id' => [(new UniqueEloquent(Fact::class))->includeSoftDeleted()] | ||
]); | ||
|
||
// Act | ||
$isValid = $validator->passes(); | ||
$messages = $validator->messages()->toArray(); | ||
|
||
// Assert | ||
$this->assertFalse($isValid); | ||
$this->assertEquals('The resource already exists.', $messages['id'][0]); | ||
$this->assertCount(1, User::withTrashed()->get()); | ||
$this->assertCount(1, User::all()); | ||
$this->assertCount(1, Fact::withTrashed()->get()); | ||
$this->assertCount(0, Fact::all()); | ||
} | ||
|
||
/* | ||
* Test language support | ||
*/ | ||
|