-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: TypeError in Validation is_unique/is_not_unique #7085
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d195e3
test: refactor: remove duplicate code
kenjis 7f789ac
test: fix wrong Rules instance
kenjis 5ab8487
test: add test for is_unique and int value
kenjis d7099cc
fix: TypeError in Rules::is_unique()
kenjis 5722ab0
fix: TypeError in Rules::is_not_unique()
kenjis 7b405d1
fix: add type check
kenjis 7d4e0eb
fix: revert Traditional is_unique() is_not_unique() implementation
kenjis 76c8997
style: break long lines
kenjis 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -11,23 +11,17 @@ | |
|
||
namespace CodeIgniter\Validation; | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\DatabaseTestTrait; | ||
use Config\Database; | ||
use Config\Services; | ||
use CodeIgniter\Validation\StrictRules\DatabaseRelatedRulesTest as StrictDatabaseRelatedRulesTest; | ||
use Tests\Support\Validation\TestRules; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @group DatabaseLive | ||
*/ | ||
final class DatabaseRelatedRulesTest extends CIUnitTestCase | ||
final class DatabaseRelatedRulesTest extends StrictDatabaseRelatedRulesTest | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
private Validation $validation; | ||
private array $config = [ | ||
protected array $config = [ | ||
'ruleSets' => [ | ||
Rules::class, | ||
FormatRules::class, | ||
|
@@ -45,173 +39,8 @@ final class DatabaseRelatedRulesTest extends CIUnitTestCase | |
], | ||
]; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->validation = new Validation((object) $this->config, Services::renderer()); | ||
$this->validation->reset(); | ||
} | ||
|
||
public function testIsUniqueFalse(): void | ||
{ | ||
Database::connect() | ||
->table('user') | ||
->insert([ | ||
'name' => 'Derek Travis', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$data = ['email' => '[email protected]']; | ||
$this->validation->setRules(['email' => 'is_unique[user.email]']); | ||
$this->assertFalse($this->validation->run($data)); | ||
} | ||
|
||
public function testIsUniqueTrue(): void | ||
{ | ||
$data = ['email' => '[email protected]']; | ||
$this->validation->setRules(['email' => 'is_unique[user.email]']); | ||
$this->assertTrue($this->validation->run($data)); | ||
} | ||
|
||
public function testIsUniqueIgnoresParams(): void | ||
{ | ||
$db = Database::connect(); | ||
$db | ||
->table('user') | ||
->insert([ | ||
'name' => 'Developer A', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
$row = $db->table('user') | ||
->limit(1) | ||
->get() | ||
->getRow(); | ||
|
||
$data = ['email' => '[email protected]']; | ||
$this->validation->setRules(['email' => "is_unique[user.email,id,{$row->id}]"]); | ||
$this->assertTrue($this->validation->run($data)); | ||
} | ||
|
||
public function testIsUniqueIgnoresParamsPlaceholders(): void | ||
{ | ||
$this->hasInDatabase('user', [ | ||
'name' => 'Derek', | ||
'email' => '[email protected]', | ||
'country' => 'GB', | ||
]); | ||
|
||
$row = Database::connect() | ||
->table('user') | ||
->limit(1) | ||
->get() | ||
->getRow(); | ||
|
||
$data = [ | ||
'id' => $row->id, | ||
'email' => '[email protected]', | ||
]; | ||
|
||
$this->validation->setRules(['email' => 'is_unique[user.email,id,{id}]']); | ||
$this->assertTrue($this->validation->run($data)); | ||
} | ||
|
||
public function testIsUniqueByManualRun(): void | ||
{ | ||
Database::connect() | ||
->table('user') | ||
->insert([ | ||
'name' => 'Developer A', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$this->assertFalse((new Rules())->is_unique('[email protected]', 'user.email,id,{id}', [])); | ||
} | ||
|
||
public function testIsNotUniqueFalse(): void | ||
{ | ||
Database::connect() | ||
->table('user') | ||
->insert([ | ||
'name' => 'Derek Travis', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$data = ['email' => '[email protected]']; | ||
$this->validation->setRules(['email' => 'is_not_unique[user.email]']); | ||
$this->assertFalse($this->validation->run($data)); | ||
} | ||
|
||
public function testIsNotUniqueTrue(): void | ||
{ | ||
Database::connect() | ||
->table('user') | ||
->insert([ | ||
'name' => 'Derek Travis', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$data = ['email' => '[email protected]']; | ||
$this->validation->setRules(['email' => 'is_not_unique[user.email]']); | ||
$this->assertTrue($this->validation->run($data)); | ||
} | ||
|
||
public function testIsNotUniqueIgnoresParams(): void | ||
protected function createRules() | ||
{ | ||
$db = Database::connect(); | ||
$db->table('user') | ||
->insert([ | ||
'name' => 'Developer A', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$row = $db->table('user') | ||
->limit(1) | ||
->get() | ||
->getRow(); | ||
|
||
$data = ['email' => '[email protected]']; | ||
$this->validation->setRules(['email' => "is_not_unique[user.email,id,{$row->id}]"]); | ||
$this->assertFalse($this->validation->run($data)); | ||
} | ||
|
||
public function testIsNotUniqueIgnoresParamsPlaceholders(): void | ||
{ | ||
$this->hasInDatabase('user', [ | ||
'name' => 'Derek', | ||
'email' => '[email protected]', | ||
'country' => 'GB', | ||
]); | ||
|
||
$row = Database::connect() | ||
->table('user') | ||
->limit(1) | ||
->get() | ||
->getRow(); | ||
|
||
$data = [ | ||
'id' => $row->id, | ||
'email' => '[email protected]', | ||
]; | ||
$this->validation->setRules(['email' => 'is_not_unique[user.email,id,{id}]']); | ||
$this->assertTrue($this->validation->run($data)); | ||
} | ||
|
||
public function testIsNotUniqueByManualRun(): void | ||
{ | ||
Database::connect() | ||
->table('user') | ||
->insert([ | ||
'name' => 'Developer A', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$this->assertTrue((new Rules())->is_not_unique('[email protected]', 'user.email,id,{id}', [])); | ||
return new Rules(); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -21,14 +21,16 @@ | |
/** | ||
* @internal | ||
* | ||
* @no-final | ||
* | ||
* @group DatabaseLive | ||
*/ | ||
final class DatabaseRelatedRulesTest extends CIUnitTestCase | ||
class DatabaseRelatedRulesTest extends CIUnitTestCase | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
private Validation $validation; | ||
private array $config = [ | ||
protected Validation $validation; | ||
protected array $config = [ | ||
'ruleSets' => [ | ||
Rules::class, | ||
FormatRules::class, | ||
|
@@ -53,6 +55,11 @@ protected function setUp(): void | |
$this->validation->reset(); | ||
} | ||
|
||
protected function createRules() | ||
{ | ||
return new Rules(); | ||
} | ||
|
||
public function testIsUniqueFalse(): void | ||
{ | ||
Database::connect() | ||
|
@@ -128,7 +135,22 @@ public function testIsUniqueByManualRun(): void | |
'country' => 'Elbonia', | ||
]); | ||
|
||
$this->assertFalse((new Rules())->is_unique('[email protected]', 'user.email,id,{id}', [])); | ||
$this->assertFalse($this->createRules()->is_unique('[email protected]', 'user.email,id,{id}', [])); | ||
} | ||
|
||
public function testIsUniqueIntValueByManualRun(): void | ||
{ | ||
Database::connect() | ||
->table('user') | ||
->insert([ | ||
'name' => 'Developer A', | ||
'email' => '[email protected]', | ||
'country' => 'Elbonia', | ||
]); | ||
|
||
$result = $this->createRules()->is_unique(1, 'user.id', []); | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
public function testIsNotUniqueFalse(): void | ||
|
@@ -213,6 +235,6 @@ public function testIsNotUniqueByManualRun(): void | |
'country' => 'Elbonia', | ||
]); | ||
|
||
$this->assertTrue((new Rules())->is_not_unique('[email protected]', 'user.email,id,{id}', [])); | ||
$this->assertTrue($this->createRules()->is_not_unique('[email protected]', 'user.email,id,{id}', [])); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yesterday I was trying something else, I removed "?string" and it worked.