Completed
Push — dev ( ce9639...c420bb )
by Josef
04:04
created

testCheckInvalidStructuresMissingKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace HelePartnerSyncApi;
4
5
use DateTime;
6
use PHPUnit_Framework_TestCase;
7
use stdClass;
8
9
class ValidatorTest extends PHPUnit_Framework_TestCase
10
{
11
12
	public function testValidExamples()
13
	{
14
		$now = new DateTime();
15
		Validator::checkNull(null);
16
		Validator::checkString('true');
17
		Validator::checkString('0');
18
		Validator::checkString('1.9');
19
		Validator::checkString('array');
20
		Validator::checkInt(121);
21
		Validator::checkInt(-78);
22
		Validator::checkInt(0);
23
		Validator::checkDateTime($now);
24
		Validator::checkDateTimeString($now->format(DATE_W3C));
25
		$this->assertTrue(true);
26
	}
27
28
	/**
29
	 * @return mixed[][]
30
	 */
31 View Code Duplication
	public function provideInvalidNulls()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
	{
33
		return array(
34
			array(true),
35
			array(false),
36
			array(0),
37
			array(0.0),
38
			array(array()),
39
		);
40
	}
41
42
	/**
43
	 * @param mixed $value
44
	 *
45
	 * @dataProvider provideInvalidNulls
46
	 * @expectedException \HelePartnerSyncApi\ValidatorException
47
	 * @expectedExceptionMessage Null expected
48
	 */
49
	public function testCheckInvalidNulls($value)
50
	{
51
		Validator::checkNull($value);
52
	}
53
54
	/**
55
	 * @return mixed[][]
56
	 */
57 View Code Duplication
	public function provideInvalidStrings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
	{
59
		return array(
60
			array(true),
61
			array(0.5),
62
			array(1212),
63
			array(null),
64
			array(array()),
65
		);
66
	}
67
68
	/**
69
	 * @param mixed $value
70
	 *
71
	 * @dataProvider provideInvalidStrings
72
	 * @expectedException \HelePartnerSyncApi\ValidatorException
73
	 * @expectedExceptionMessage String expected
74
	 */
75
	public function testCheckInvalidStrings($value)
76
	{
77
		Validator::checkString($value);
78
	}
79
80
	/**
81
	 * @return mixed[][]
82
	 */
83
	public function provideInvalidInts()
84
	{
85
		return array(
86
			array('1212'),
87
			array(true),
88
			array(0.5),
89
			array(null),
90
			array(array()),
91
			array(new stdClass()),
92
		);
93
	}
94
95
	/**
96
	 * @param mixed $value
97
	 *
98
	 * @dataProvider provideInvalidInts
99
	 * @expectedException \HelePartnerSyncApi\ValidatorException
100
	 * @expectedExceptionMessage Int expected
101
	 */
102
	public function testCheckInvalidIntegers($value)
103
	{
104
		Validator::checkInt($value);
105
	}
106
107
	public function testCheckValidStructures()
108
	{
109
		Validator::checkStructure(array(-1, 0, 1), array(Validator::TYPE_INT));
110
111
		Validator::checkStructure(array(
112
			'key' => 123,
113
			'data' => array(
114
				'text' => 'abc',
115
			),
116
		), array(
117
			'key' => Validator::TYPE_INT,
118
			'data' => array(
119
				'text' => Validator::TYPE_STRING,
120
			),
121
		));
122
123
		Validator::checkStructure(array(
124
			array(
125
				'from' => 12,
126
				'to' => 16,
127
			),
128
			array(
129
				'from' => 17,
130
				'to' => 22,
131
			),
132
		), array(
133
			array(
134
				'from' => Validator::TYPE_INT,
135
				'to' => Validator::TYPE_INT,
136
			),
137
		));
138
139
		$this->assertTrue(true);
140
	}
141
142
	/**
143
	 * @expectedException \HelePartnerSyncApi\ValidatorException
144
	 * @expectedExceptionMessage Invalid type in key 'foo': Int expected, array given.
145
	 */
146
	public function testCheckInvalidStructuresBasic()
147
	{
148
		Validator::checkStructure(array(
149
			'moo' => 0,
150
			'foo' => array(),
151
		), array(
152
			'moo' => Validator::TYPE_INT,
153
			'foo' => Validator::TYPE_INT,
154
		));
155
	}
156
157
	/**
158
	 * @expectedException \HelePartnerSyncApi\ValidatorException
159
	 * @expectedExceptionMessage Expected list structure in root
160
	 */
161
	public function testCheckInvalidStructureNotAList()
162
	{
163
		Validator::checkStructure(array(1, 2, 'key' => 'not-a-list'), array(Validator::TYPE_INT));
164
	}
165
166
	/**
167
	 * @expectedException \HelePartnerSyncApi\ValidatorException
168
	 * @expectedExceptionMessage Invalid type in key '2': Int expected, string (3) given.
169
	 */
170
	public function testCheckInvalidStructureListElement()
171
	{
172
		Validator::checkStructure(array(1, 2, '3'), array(Validator::TYPE_INT));
173
	}
174
175
	/**
176
	 * @expectedException \HelePartnerSyncApi\ValidatorException
177
	 * @expectedExceptionMessage Invalid type in path 'foo.bar': Int expected, string (moo) given.
178
	 */
179
	public function testCheckInvalidStructuresDeep()
180
	{
181
		Validator::checkStructure(array(
182
			'boo' => null,
183
			'foo' => array(
184
				'bar' => 'moo',
185
			),
186
		), array(
187
			'boo' => Validator::TYPE_NULL,
188
			'foo' => array(
189
				'bar' => Validator::TYPE_INT,
190
			),
191
		));
192
	}
193
194
	/**
195
	 * @expectedException \HelePartnerSyncApi\ValidatorException
196
	 * @expectedExceptionMessage Missing keys (bar, foo) in root
197
	 */
198
	public function testCheckInvalidStructuresMissingKeys()
199
	{
200
		Validator::checkStructure(array(), array(
201
			'bar' => Validator::TYPE_INT,
202
			'foo' => Validator::TYPE_INT,
203
		));
204
	}
205
206
	/**
207
	 * @expectedException \HelePartnerSyncApi\ValidatorException
208
	 * @expectedExceptionMessage Unknown keys (foo, bar) in root
209
	 */
210
	public function testCheckInvalidStructuresUnknownKeys()
211
	{
212
		Validator::checkStructure(array(
213
			'foo' => 'foo',
214
			'bar' => 'bar',
215
		), array(
216
			'moo' => Validator::TYPE_INT,
217
		));
218
	}
219
220
}
221