|
| 1 | +<?php |
| 2 | + |
| 3 | +use CleanPhp\Invoicer\Service\InputFilter\OrderInputFilter; |
| 4 | + |
| 5 | +describe('InputFilter\Order', function () { |
| 6 | + beforeEach(function () { |
| 7 | + $this->inputFilter = new OrderInputFilter(); |
| 8 | + }); |
| 9 | + |
| 10 | + describe('->isValid()', function () { |
| 11 | + it('should require a customer.id', function () { |
| 12 | + $isValid = $this->inputFilter->isValid(); |
| 13 | + |
| 14 | + $error = [ |
| 15 | + 'id' => [ |
| 16 | + 'isEmpty' => 'Value is required and can\'t be empty' |
| 17 | + ] |
| 18 | + ]; |
| 19 | + |
| 20 | + $customer = $this->inputFilter |
| 21 | + ->getMessages()['customer']; |
| 22 | + |
| 23 | + expect($isValid)->to->equal(false); |
| 24 | + expect($customer)->to->equal($error); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should require an order number', function () { |
| 28 | + $isValid = $this->inputFilter->isValid(); |
| 29 | + |
| 30 | + $error = [ |
| 31 | + 'isEmpty' => 'Value is required and can\'t be empty' |
| 32 | + ]; |
| 33 | + |
| 34 | + $orderNo = $this->inputFilter |
| 35 | + ->getMessages()['orderNumber']; |
| 36 | + |
| 37 | + expect($isValid)->to->equal(false); |
| 38 | + expect($orderNo)->to->equal($error); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should require order numbers be 13 chars long', function () { |
| 42 | + $scenarios = [ |
| 43 | + [ |
| 44 | + 'value' => '124', |
| 45 | + 'errors' => [ |
| 46 | + 'stringLengthTooShort' => |
| 47 | + 'The input is less than 13 characters long' |
| 48 | + ] |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'value' => '20001020-0123XR', |
| 52 | + 'errors' => [ |
| 53 | + 'stringLengthTooLong' => |
| 54 | + 'The input is more than 13 characters long' |
| 55 | + ] |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'value' => '20040717-1841', |
| 59 | + 'errors' => null |
| 60 | + ] |
| 61 | + ]; |
| 62 | + |
| 63 | + foreach ($scenarios as $scenario) { |
| 64 | + $this->inputFilter = new OrderInputFilter(); |
| 65 | + $this->inputFilter->setData([ |
| 66 | + 'orderNumber' => $scenario['value'] |
| 67 | + ])->isValid(); |
| 68 | + |
| 69 | + $messages = $this->inputFilter |
| 70 | + ->getMessages()['orderNumber']; |
| 71 | + |
| 72 | + expect($messages)->to->equal($scenario['errors']); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('should require a description', function () { |
| 77 | + $isValid = $this->inputFilter->isValid(); |
| 78 | + |
| 79 | + $error = [ |
| 80 | + 'isEmpty' => 'Value is required and can\'t be empty' |
| 81 | + ]; |
| 82 | + |
| 83 | + $messages = $this->inputFilter |
| 84 | + ->getMessages()['description']; |
| 85 | + |
| 86 | + expect($isValid)->to->equal(false); |
| 87 | + expect($messages)->to->equal($error); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should require a total', function () { |
| 91 | + $isValid = $this->inputFilter->isValid(); |
| 92 | + |
| 93 | + $error = [ |
| 94 | + 'isEmpty' => 'Value is required and can\'t be empty' |
| 95 | + ]; |
| 96 | + |
| 97 | + $messages = $this->inputFilter |
| 98 | + ->getMessages()['total']; |
| 99 | + |
| 100 | + expect($isValid)->to->equal(false); |
| 101 | + expect($messages)->to->equal($error); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should require total to be a float value', function () { |
| 105 | + $scenarios = [ |
| 106 | + [ |
| 107 | + 'value' => 124, |
| 108 | + 'errors' => null |
| 109 | + ], |
| 110 | + [ |
| 111 | + 'value' => 'asdf', |
| 112 | + 'errors' => [ |
| 113 | + 'notFloat' |
| 114 | + => 'The input does not appear to be a float' |
| 115 | + ] |
| 116 | + ], |
| 117 | + [ |
| 118 | + 'value' => 99.99, |
| 119 | + 'errors' => null |
| 120 | + ] |
| 121 | + ]; |
| 122 | + |
| 123 | + foreach ($scenarios as $scenario) { |
| 124 | + $this->inputFilter = new OrderInputFilter(); |
| 125 | + $this->inputFilter->setData([ |
| 126 | + 'total' => $scenario['value'] |
| 127 | + ])->isValid(); |
| 128 | + |
| 129 | + $messages = $this->inputFilter |
| 130 | + ->getMessages()['total']; |
| 131 | + |
| 132 | + expect($messages)->to->equal($scenario['errors']); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments