Skip to content

Commit 516c5d2

Browse files
ISSUE-3: Implemented unit-tests
1 parent 0710c26 commit 516c5d2

File tree

3 files changed

+296
-0
lines changed

3 files changed

+296
-0
lines changed

tests/CollectionTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
namespace DarkWebDesign\SymfonyAddon\Constraint\Tests;
22+
23+
use DarkWebDesign\SymfonyAddon\Constraint\Collection;
24+
use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\ArrayAccessObject;
25+
use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\ToStringObject;
26+
use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\TraversableObject;
27+
use PHPUnit_Framework_TestCase;
28+
use stdClass;
29+
use Symfony\Component\Validator\Constraints\NotBlank;
30+
use Symfony\Component\Validator\Constraints\Valid;
31+
32+
class CollectionTest extends PHPUnit_Framework_TestCase
33+
{
34+
public function testConstruct()
35+
{
36+
new Collection(array(
37+
'constraints' => array(
38+
new NotBlank(),
39+
),
40+
));
41+
}
42+
43+
public function testConstructDefaultOption()
44+
{
45+
new Collection(array(
46+
new NotBlank(),
47+
));
48+
}
49+
50+
/**
51+
* @expectedException \Symfony\Component\Validator\Exception\MissingOptionsException
52+
*/
53+
public function testConstructMissingRequiredConstraintsOption()
54+
{
55+
new Collection();
56+
}
57+
58+
/**
59+
* @param mixed $constraints
60+
*
61+
* @dataProvider providerNoArray
62+
*
63+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
64+
*/
65+
public function testConstructConstraintsOptionNoArray($constraints)
66+
{
67+
new Collection(array(
68+
'constraints' => $constraints,
69+
));
70+
}
71+
72+
/**
73+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
74+
*/
75+
public function testConstructNoConstraint()
76+
{
77+
new Collection(array(
78+
'constraints' => array(
79+
'foo',
80+
),
81+
));
82+
}
83+
84+
/**
85+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
86+
*/
87+
public function testConstructValidConstraint()
88+
{
89+
new Collection(array(
90+
'constraints' => array(
91+
new Valid(),
92+
),
93+
));
94+
}
95+
96+
/**
97+
* @return array[]
98+
*/
99+
public function providerNoArray()
100+
{
101+
return array(
102+
'bool' => array(true),
103+
'int' => array(1),
104+
'float' => array(1.2),
105+
'string' => array('foo'),
106+
'object' => array(new stdClass()),
107+
'resource' => array(tmpfile()),
108+
'callable' => array(function () {})
109+
);
110+
}
111+
}

tests/CollectionValidatorTest.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
namespace DarkWebDesign\SymfonyAddon\Constraint\Tests;
22+
23+
use DarkWebDesign\SymfonyAddon\Constraint\Collection;
24+
use DarkWebDesign\SymfonyAddon\Constraint\CollectionValidator;
25+
use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\ToStringObject;
26+
use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\TraversableObject;
27+
use PHPUnit_Framework_TestCase;
28+
use stdClass;
29+
use Symfony\Component\Validator\Constraints as Assert;
30+
31+
class CollectionValidatorTest extends PHPUnit_Framework_TestCase
32+
{
33+
/** @var \Symfony\Component\Validator\ExecutionContext */
34+
private $context;
35+
36+
/** @var \DarkWebDesign\SymfonyAddon\Constraint\CollectionValidator */
37+
private $validator;
38+
39+
protected function setUp()
40+
{
41+
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
42+
$this->validator = new CollectionValidator();
43+
$this->validator->initialize($this->context);
44+
}
45+
46+
/**
47+
* @param array $value
48+
*
49+
* @dataProvider providerValidCollection
50+
*/
51+
public function testValidate($value)
52+
{
53+
$constraints = array(
54+
new Assert\Email(),
55+
new Assert\NotBlank(),
56+
);
57+
58+
$i = 0;
59+
60+
foreach ($value as $field => $fieldValue) {
61+
foreach ($constraints as $constraint) {
62+
$this->context->expects($this->at($i++))
63+
->method('validateValue')
64+
->with($fieldValue, $constraint, '[' . $field . ']');
65+
}
66+
}
67+
68+
$this->validator->validate($value, new Collection($constraints));
69+
}
70+
71+
/**
72+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
73+
*/
74+
public function testValidateNoCollectionConstraint()
75+
{
76+
$this->context
77+
->expects($this->never())
78+
->method('validateValue');
79+
80+
$this->validator->validate(array(), new Assert\NotNull());
81+
}
82+
83+
public function testValidateNull()
84+
{
85+
$this->context
86+
->expects($this->never())
87+
->method('validateValue');
88+
89+
$this->validator->validate(null, new Collection(array(
90+
new Assert\NotBlank(),
91+
)));
92+
}
93+
94+
/**
95+
* @param mixed $value
96+
*
97+
* @dataProvider providerNoArray
98+
*
99+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
100+
*/
101+
public function testValidateNoArray($value)
102+
{
103+
$this->context
104+
->expects($this->never())
105+
->method('validateValue');
106+
107+
$this->validator->validate($value, new Collection(array(
108+
new Assert\NotBlank(),
109+
)));
110+
}
111+
112+
/**
113+
* @return array[]
114+
*/
115+
public function providerValidCollection()
116+
{
117+
return array(
118+
'empty' => array(array()),
119+
'array' => array(array('[email protected]')),
120+
'traversableObject' => array(new TraversableObject(array('[email protected]'))),
121+
);
122+
}
123+
124+
/**
125+
* @return array[]
126+
*/
127+
public function providerNoArray()
128+
{
129+
return array(
130+
'bool' => array(true),
131+
'int' => array(1),
132+
'float' => array(1.2),
133+
'string' => array('foo'),
134+
'object' => array(new stdClass()),
135+
'resource' => array(tmpfile()),
136+
'callable' => array(function () {})
137+
);
138+
}
139+
}

tests/Models/TraversableObject.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
namespace DarkWebDesign\SymfonyAddon\Constraint\Tests\Models;
22+
23+
use ArrayIterator;
24+
use IteratorAggregate;
25+
26+
class TraversableObject implements IteratorAggregate
27+
{
28+
/** @var array */
29+
private $container;
30+
31+
/**
32+
* @param array $data
33+
*/
34+
public function __construct($data = array())
35+
{
36+
$this->container = $data;
37+
}
38+
39+
/**
40+
* @return \ArrayIterator
41+
*/
42+
public function getIterator()
43+
{
44+
return new ArrayIterator($this->container);
45+
}
46+
}

0 commit comments

Comments
 (0)