2021-10-23 03:07:54 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-10-23 03:07:54 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
namespace Tests\Integration\Validation;
|
|
|
|
|
|
|
|
use App\Http\ValidationRules\ValidAmount;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
2024-09-16 13:13:55 +02:00
|
|
|
*
|
2021-10-23 03:07:54 +02:00
|
|
|
*/
|
|
|
|
class AmountValidationRuleTest extends TestCase
|
|
|
|
{
|
2024-08-22 08:57:52 +02:00
|
|
|
protected function setUp(): void
|
2021-10-23 03:07:54 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleAmountValid()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
|
|
|
'amount' => 1,
|
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertTrue($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidAmountValid()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => 'aa',
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertFalse($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIllegalChars()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => '5+5',
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertFalse($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIllegalCharsNaked()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => 5 + 5, //resolves as 10 - but in practice, i believe this amount is wrapped in quotes so interpreted as a string
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertTrue($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testinValidScenario1()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => '-10x',
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertFalse($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidScenario2()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
|
|
|
'amount' => -10,
|
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertTrue($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidScenario3()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => '-10',
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertTrue($v->passes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInValidScenario4()
|
|
|
|
{
|
|
|
|
$rules = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => [new ValidAmount()],
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => '-0 1',
|
2021-10-23 03:07:54 +02:00
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 03:07:54 +02:00
|
|
|
$v = $this->app['validator']->make($data, $rules);
|
|
|
|
$this->assertFalse($v->passes());
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-23 03:07:54 +02:00
|
|
|
}
|