Skip to content

Commit 759c91f

Browse files
ISSUE-15: Implemented unit-tests
1 parent f615650 commit 759c91f

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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\Transformer\Tests;
22+
23+
use DarkWebDesign\SymfonyAddon\Transformer\BooleanToStringTransformer;
24+
use PHPUnit_Framework_TestCase;
25+
use stdClass;
26+
27+
class BooleanToStringTransformerTest extends PHPUnit_Framework_TestCase
28+
{
29+
/**
30+
* @param string $trueValue
31+
* @param string $falseValue
32+
*
33+
* @dataProvider providerTrueFalseValue
34+
*/
35+
public function testTransform($trueValue, $falseValue)
36+
{
37+
$transformer = new BooleanToStringTransformer($trueValue, $falseValue);
38+
39+
$returnValue = $transformer->transform(true);
40+
41+
$this->assertSame($trueValue, $returnValue);
42+
43+
$returnValue = $transformer->transform(false);
44+
45+
$this->assertSame($falseValue, $returnValue);
46+
}
47+
48+
public function testTransformNull()
49+
{
50+
$transformer = new BooleanToStringTransformer();
51+
52+
$returnValue = $transformer->transform(null);
53+
54+
$this->assertNull($returnValue);
55+
}
56+
57+
/**
58+
* @param mixed $value
59+
*
60+
* @dataProvider providerNoBool
61+
*
62+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
63+
* @expectedExceptionMessage Expected a boolean.
64+
*/
65+
public function testTransformNoBool($value)
66+
{
67+
$transformer = new BooleanToStringTransformer();
68+
69+
$transformer->transform($value);
70+
}
71+
72+
/**
73+
* @param string $trueValue
74+
* @param string $falseValue
75+
*
76+
* @dataProvider providerTrueFalseValue
77+
*/
78+
public function testReverseTransform($trueValue, $falseValue)
79+
{
80+
$transformer = new BooleanToStringTransformer($trueValue, $falseValue);
81+
82+
$returnValue = $transformer->reverseTransform($trueValue);
83+
84+
$this->assertTrue($returnValue);
85+
86+
$returnValue = $transformer->reverseTransform($falseValue);
87+
88+
$this->assertFalse($returnValue);
89+
}
90+
91+
public function testReverseTransformNull()
92+
{
93+
$transformer = new BooleanToStringTransformer();
94+
95+
$returnValue = $transformer->reverseTransform(null);
96+
97+
$this->assertNull($returnValue);
98+
}
99+
100+
public function testReverseTransformEmptyString()
101+
{
102+
$transformer = new BooleanToStringTransformer();
103+
104+
$returnValue = $transformer->reverseTransform('');
105+
106+
$this->assertNull($returnValue);
107+
}
108+
109+
/**
110+
* @param mixed $value
111+
*
112+
* @dataProvider providerNoScalar
113+
*
114+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
115+
* @expectedExceptionMessage Expected a scalar.
116+
*/
117+
public function testReverseTransformNoScalar($value)
118+
{
119+
$transformer = new BooleanToStringTransformer();
120+
121+
$transformer->reverseTransform($value);
122+
}
123+
124+
/**
125+
* @param string $trueValue
126+
* @param string $falseValue
127+
*
128+
* @dataProvider providerTrueFalseValue
129+
*/
130+
public function testReverseTransformInvalidString($trueValue, $falseValue)
131+
{
132+
$transformer = new BooleanToStringTransformer($trueValue, $falseValue);
133+
134+
$this->setExpectedException(
135+
'Symfony\Component\Form\Exception\TransformationFailedException',
136+
sprintf('Expected a string "%s" or "%s".', $trueValue, $falseValue)
137+
);
138+
139+
$transformer->reverseTransform('foo');
140+
}
141+
142+
/**
143+
* @return array[]
144+
*/
145+
public function providerTrueFalseValue()
146+
{
147+
return array(
148+
'true/false' => array('true', 'false'),
149+
'yes/no' => array('yes', 'no'),
150+
'on/off' => array('on', 'off'),
151+
'1/0' => array('1', '0'),
152+
);
153+
}
154+
155+
/**
156+
* @return array[]
157+
*/
158+
public function providerNoBool()
159+
{
160+
return array(
161+
'int' => array(1),
162+
'float' => array(1.2),
163+
'string' => array('foo'),
164+
'array' => array(array('foo', 'bar')),
165+
'object' => array(new stdClass),
166+
'resource' => array(tmpfile()),
167+
'callable' => array(function () {})
168+
);
169+
}
170+
171+
/**
172+
* @return array[]
173+
*/
174+
public function providerNoScalar()
175+
{
176+
return array(
177+
'array' => array(array('foo', 'bar')),
178+
'object' => array(new stdClass),
179+
'resource' => array(tmpfile()),
180+
'callable' => array(function () {})
181+
);
182+
}
183+
}

0 commit comments

Comments
 (0)