-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect string handling of callbacks #26
Fix incorrect scoping of callbacks preventing access to rule properties via $this
- Loading branch information
1 parent
06af1b5
commit bb7baa2
Showing
6 changed files
with
87 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Somnambulist\Components\Validation\Tests\Issues; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Somnambulist\Components\Validation\Factory; | ||
|
||
class GH26HandlingCallbackErrorMessagesTest extends TestCase | ||
{ | ||
/** | ||
* @link https://github.com/somnambulist-tech/validation/issues/26 | ||
*/ | ||
public function testReturningMessageStringsProducesErrorMessage() | ||
{ | ||
$testData = [ | ||
'name' => 'John Doe', | ||
'custom' => 'foo', | ||
]; | ||
|
||
$validation = (new Factory)->validate($testData, [ | ||
'name' => 'required', | ||
'custom' => [ | ||
'required', | ||
function ($value) { | ||
if ($value !== 'bar') { | ||
return ':attribute should be bar'; | ||
} | ||
|
||
return true; | ||
}, | ||
], | ||
]); | ||
|
||
$this->assertEquals('custom should be bar', $validation->errors()->first('custom')); | ||
} | ||
|
||
public function testCanSetMessageIdentityViaCallback() | ||
{ | ||
$testData = [ | ||
'name' => 'John Doe', | ||
'custom' => 'foo', | ||
]; | ||
|
||
$validation = (new Factory)->validate($testData, [ | ||
'name' => 'required', | ||
'custom' => [ | ||
'required', | ||
function ($value) { | ||
$this->message = ':attribute should be bar'; | ||
|
||
return $value === 'bar'; | ||
}, | ||
], | ||
]); | ||
|
||
$this->assertEquals('custom should be bar', $validation->errors()->first('custom')); | ||
} | ||
} |