|
| 1 | +<?php |
| 2 | +namespace Particle\Validator\Tests; |
| 3 | + |
| 4 | +use Particle\Validator\Validator; |
| 5 | + |
| 6 | +class ReusableValidatorTest extends \PHPUnit_Framework_TestCase |
| 7 | +{ |
| 8 | + /** |
| 9 | + * @var Validator |
| 10 | + */ |
| 11 | + protected $validator; |
| 12 | + |
| 13 | + protected function configureValidator() |
| 14 | + { |
| 15 | + $this->validator = new Validator(); |
| 16 | + |
| 17 | + $this->validator->required('customerNr')->alnum(); |
| 18 | + $this->validator->required('paymentType')->inArray([ |
| 19 | + 'downPaymentChangeBankAccount', |
| 20 | + 'downPaymentHighSpend', |
| 21 | + ]); |
| 22 | + $this->validator->required('reference')->alnum(); |
| 23 | + $this->validator->required('description')->lengthBetween(1, 250); |
| 24 | + $this->validator->required('amountPaid')->float()->between(0.01, 10000); |
| 25 | + $this->validator->required('notificationDate')->datetime('Ymd'); |
| 26 | + } |
| 27 | + |
| 28 | + public function testCanUseNewValidatorEveryTime() |
| 29 | + { |
| 30 | + foreach ($this->getTestData() as $rowKey => $testRow) { |
| 31 | + $this->configureValidator(); |
| 32 | + |
| 33 | + $result = $this->validator->validate($testRow['data']); |
| 34 | + $this->assertEquals($testRow['valid'], $result->isValid()); |
| 35 | + $this->assertEquals($testRow['messages'], $result->getMessages()); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function testCanReuseValidatorMultipleTimes() |
| 40 | + { |
| 41 | + $this->configureValidator(); |
| 42 | + |
| 43 | + foreach ($this->getTestData() as $rowKey => $testRow) { |
| 44 | + $result = $this->validator->validate($testRow['data']); |
| 45 | + $this->assertEquals($testRow['valid'], $result->isValid()); |
| 46 | + $this->assertEquals($testRow['messages'], $result->getMessages()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private function getTestData() |
| 51 | + { |
| 52 | + return [ |
| 53 | + [ |
| 54 | + 'data' => [ |
| 55 | + 'customerNr' => 'c000012340', |
| 56 | + 'paymentType' => '', |
| 57 | + 'reference' => '', |
| 58 | + 'description' => 'Test payment 1', |
| 59 | + 'amountPaid' => 1000.00, |
| 60 | + 'notificationDate' => '20150401', |
| 61 | + ], |
| 62 | + 'valid' => false, |
| 63 | + 'messages' => [ |
| 64 | + 'paymentType' => [ |
| 65 | + 'NotEmpty::EMPTY_VALUE' => 'paymentType must not be empty', |
| 66 | + ], |
| 67 | + 'reference' => [ |
| 68 | + 'NotEmpty::EMPTY_VALUE' => 'reference must not be empty', |
| 69 | + ], |
| 70 | + ], |
| 71 | + ], |
| 72 | + |
| 73 | + [ |
| 74 | + 'data' => [ |
| 75 | + 'customerNr' => 'c000001234', |
| 76 | + 'paymentType' => 'gfh', |
| 77 | + 'description' => 'Test payment 2', |
| 78 | + 'amountPaid' => 25.00, |
| 79 | + 'notificationDate' => '20150401', |
| 80 | + ], |
| 81 | + 'valid' => false, |
| 82 | + 'messages' => [ |
| 83 | + 'paymentType' => [ |
| 84 | + 'InArray::NOT_IN_ARRAY' => 'paymentType must be in the defined set of values', |
| 85 | + ], |
| 86 | + 'reference' => [ |
| 87 | + 'Required::NON_EXISTENT_KEY' => 'reference must be provided, but does not exist', |
| 88 | + ], |
| 89 | + ], |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'data' => [ |
| 93 | + 'customerNr' => 'c000005678', |
| 94 | + 'paymentType' => 'sdf ', |
| 95 | + 'reference' => '', |
| 96 | + 'description' => 'Test payment 3', |
| 97 | + 'amountPaid' => 25.00, |
| 98 | + 'notificationDate' => '20150401', |
| 99 | + ], |
| 100 | + 'valid' => false, |
| 101 | + 'messages' => [ |
| 102 | + 'paymentType' => [ |
| 103 | + 'InArray::NOT_IN_ARRAY' => 'paymentType must be in the defined set of values', |
| 104 | + ], |
| 105 | + 'reference' => [ |
| 106 | + 'NotEmpty::EMPTY_VALUE' => 'reference must not be empty', |
| 107 | + ], |
| 108 | + ], |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
0 commit comments