Skip to content

Commit e89d9b7

Browse files
committed
OrderInputFilter implementation
1 parent a6bef2b commit e89d9b7

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

specs/input-filter/order.spec.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace CleanPhp\Invoicer\Service\InputFilter;
4+
5+
use Zend\I18n\Validator\IsFloat;
6+
use Zend\InputFilter\Input;
7+
use Zend\InputFilter\InputFilter;
8+
use Zend\Validator\StringLength;
9+
10+
/**
11+
* Class OrderInputFilter
12+
* @package CleanPhp\Invoicer\Service\InputFilter
13+
*/
14+
class OrderInputFilter extends InputFilter
15+
{
16+
/**
17+
* Set us up the class!
18+
*/
19+
public function __construct()
20+
{
21+
$customer = (new InputFilter());
22+
$id = (new Input('id'))
23+
->setRequired(true);
24+
$customer->add($id);
25+
26+
$orderNumber = (new Input('orderNumber'))
27+
->setRequired(true);
28+
$orderNumber->getValidatorChain()->attach(
29+
new StringLength(['min' => 13, 'max' => 13])
30+
);
31+
32+
$description = (new Input('description'))
33+
->setRequired(true);
34+
35+
$total = (new Input('total'))
36+
->setRequired(true);
37+
$total->getValidatorChain()->attach(new IsFloat());
38+
39+
$this->add($customer, 'customer');
40+
$this->add($orderNumber);
41+
$this->add($description);
42+
$this->add($total);
43+
}
44+
}

0 commit comments

Comments
 (0)