| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
use netWarning: Unexpected character in input: '\' (ASCII=92) state=1 in Unknown on line 0xjconfWarning: Unexpected character in input: '\' (ASCII=92) state=1 in Unknown on line 0convertersWarning: Unexpected character in input: '\' (ASCII=92) state=1 in Unknown on line 0PrimitiveValueConverter; |
|---|
| 10 |
Mock::generate('net\xjconf\Tag', 'MockTag'); |
|---|
| 11 |
Mock::generate('net\xjconf\definitions\Definition', 'MockDefinition'); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class PrimitiveValueConverterTestCase extends UnitTestCase |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
* a mocked definition |
|---|
| 22 |
* |
|---|
| 23 |
* @var SimpleMock |
|---|
| 24 |
*/ |
|---|
| 25 |
protected $definition; |
|---|
| 26 |
|
|---|
| 27 |
* a mocked tag |
|---|
| 28 |
* |
|---|
| 29 |
* @var SimpleMock |
|---|
| 30 |
*/ |
|---|
| 31 |
protected $tag; |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
* set up test resources |
|---|
| 35 |
*/ |
|---|
| 36 |
public function setUp() |
|---|
| 37 |
{ |
|---|
| 38 |
$this->tag = new MockTag(); |
|---|
| 39 |
$this->definition = new MockDefinition(); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* test if converting to boolean works |
|---|
| 44 |
*/ |
|---|
| 45 |
public function testBoolean() |
|---|
| 46 |
{ |
|---|
| 47 |
$converter = new PrimitiveValueConverter('boolean'); |
|---|
| 48 |
$this->assertEqual($converter->getType(), 'boolean'); |
|---|
| 49 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 50 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 51 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 52 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 53 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 54 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 55 |
$this->tag->setReturnValueAt(3, 'getData', 'true'); |
|---|
| 56 |
$this->assertTrue($converter->convertValue($this->tag, $this->definition)); |
|---|
| 57 |
$this->tag->setReturnValueAt(4, 'getData', 1); |
|---|
| 58 |
$this->assertTrue($converter->convertValue($this->tag, $this->definition)); |
|---|
| 59 |
$this->tag->setReturnValueAt(5, 'getData', '1'); |
|---|
| 60 |
$this->assertTrue($converter->convertValue($this->tag, $this->definition)); |
|---|
| 61 |
$this->tag->setReturnValueAt(6, 'getData', null); |
|---|
| 62 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* test if converting to boolean works |
|---|
| 67 |
*/ |
|---|
| 68 |
public function testBool() |
|---|
| 69 |
{ |
|---|
| 70 |
$converter = new PrimitiveValueConverter('bool'); |
|---|
| 71 |
$this->assertEqual($converter->getType(), 'bool'); |
|---|
| 72 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 73 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 74 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 75 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 76 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 77 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 78 |
$this->tag->setReturnValueAt(3, 'getData', 'true'); |
|---|
| 79 |
$this->assertTrue($converter->convertValue($this->tag, $this->definition)); |
|---|
| 80 |
$this->tag->setReturnValueAt(4, 'getData', 1); |
|---|
| 81 |
$this->assertTrue($converter->convertValue($this->tag, $this->definition)); |
|---|
| 82 |
$this->tag->setReturnValueAt(5, 'getData', '1'); |
|---|
| 83 |
$this->assertTrue($converter->convertValue($this->tag, $this->definition)); |
|---|
| 84 |
$this->tag->setReturnValueAt(6, 'getData', null); |
|---|
| 85 |
$this->assertFalse($converter->convertValue($this->tag, $this->definition)); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* test if converting to integer works |
|---|
| 90 |
*/ |
|---|
| 91 |
public function testInteger() |
|---|
| 92 |
{ |
|---|
| 93 |
$converter = new PrimitiveValueConverter('integer'); |
|---|
| 94 |
$this->assertEqual($converter->getType(), 'integer'); |
|---|
| 95 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 96 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 97 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 98 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 99 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 100 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 101 |
$this->tag->setReturnValueAt(3, 'getData', 'true'); |
|---|
| 102 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 103 |
$this->tag->setReturnValueAt(4, 'getData', 1); |
|---|
| 104 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1); |
|---|
| 105 |
$this->tag->setReturnValueAt(5, 'getData', '1'); |
|---|
| 106 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1); |
|---|
| 107 |
$this->tag->setReturnValueAt(6, 'getData', 2.6); |
|---|
| 108 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2); |
|---|
| 109 |
$this->tag->setReturnValueAt(7, 'getData', '2.7'); |
|---|
| 110 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2); |
|---|
| 111 |
$this->tag->setReturnValueAt(8, 'getData', null); |
|---|
| 112 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
* test if converting to integer works |
|---|
| 117 |
*/ |
|---|
| 118 |
public function testInt() |
|---|
| 119 |
{ |
|---|
| 120 |
$converter = new PrimitiveValueConverter('int'); |
|---|
| 121 |
$this->assertEqual($converter->getType(), 'int'); |
|---|
| 122 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 123 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 124 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 125 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 126 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 127 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 128 |
$this->tag->setReturnValueAt(3, 'getData', 'true'); |
|---|
| 129 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 130 |
$this->tag->setReturnValueAt(4, 'getData', 1); |
|---|
| 131 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1); |
|---|
| 132 |
$this->tag->setReturnValueAt(5, 'getData', '1'); |
|---|
| 133 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1); |
|---|
| 134 |
$this->tag->setReturnValueAt(6, 'getData', 2.6); |
|---|
| 135 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2); |
|---|
| 136 |
$this->tag->setReturnValueAt(7, 'getData', '2.7'); |
|---|
| 137 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2); |
|---|
| 138 |
$this->tag->setReturnValueAt(8, 'getData', null); |
|---|
| 139 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
* test if converting to double works |
|---|
| 144 |
*/ |
|---|
| 145 |
public function testDouble() |
|---|
| 146 |
{ |
|---|
| 147 |
$converter = new PrimitiveValueConverter('double'); |
|---|
| 148 |
$this->assertEqual($converter->getType(), 'double'); |
|---|
| 149 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 150 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 151 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 152 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 153 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 154 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 155 |
$this->tag->setReturnValueAt(3, 'getData', 'true'); |
|---|
| 156 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 157 |
$this->tag->setReturnValueAt(4, 'getData', 1); |
|---|
| 158 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1.0); |
|---|
| 159 |
$this->tag->setReturnValueAt(5, 'getData', '1'); |
|---|
| 160 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1.0); |
|---|
| 161 |
$this->tag->setReturnValueAt(6, 'getData', 2.6); |
|---|
| 162 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2.6); |
|---|
| 163 |
$this->tag->setReturnValueAt(7, 'getData', '2.7'); |
|---|
| 164 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2.7); |
|---|
| 165 |
$this->tag->setReturnValueAt(8, 'getData', null); |
|---|
| 166 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
* test if converting to float works |
|---|
| 171 |
*/ |
|---|
| 172 |
public function testFloat() |
|---|
| 173 |
{ |
|---|
| 174 |
$converter = new PrimitiveValueConverter('float'); |
|---|
| 175 |
$this->assertEqual($converter->getType(), 'float'); |
|---|
| 176 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 177 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 178 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 179 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 180 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 181 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 182 |
$this->tag->setReturnValueAt(3, 'getData', 'true'); |
|---|
| 183 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 184 |
$this->tag->setReturnValueAt(4, 'getData', 1); |
|---|
| 185 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1.0); |
|---|
| 186 |
$this->tag->setReturnValueAt(5, 'getData', '1'); |
|---|
| 187 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 1.0); |
|---|
| 188 |
$this->tag->setReturnValueAt(6, 'getData', 2.6); |
|---|
| 189 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2.6); |
|---|
| 190 |
$this->tag->setReturnValueAt(7, 'getData', '2.7'); |
|---|
| 191 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 2.7); |
|---|
| 192 |
$this->tag->setReturnValueAt(8, 'getData', null); |
|---|
| 193 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 0.0); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
* test if converting to string works |
|---|
| 198 |
*/ |
|---|
| 199 |
public function testString() |
|---|
| 200 |
{ |
|---|
| 201 |
$converter = new PrimitiveValueConverter('string'); |
|---|
| 202 |
$this->assertEqual($converter->getType(), 'string'); |
|---|
| 203 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 204 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), 'false'); |
|---|
| 205 |
$this->tag->setReturnValueAt(1, 'getData', 0); |
|---|
| 206 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), '0'); |
|---|
| 207 |
$this->tag->setReturnValueAt(2, 'getData', '0'); |
|---|
| 208 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), '0'); |
|---|
| 209 |
$this->tag->setReturnValueAt(3, 'getData', true); |
|---|
| 210 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), '1'); |
|---|
| 211 |
$this->tag->setReturnValueAt(4, 'getData', false); |
|---|
| 212 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), ''); |
|---|
| 213 |
$this->tag->setReturnValueAt(5, 'getData', 1); |
|---|
| 214 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), '1'); |
|---|
| 215 |
$this->tag->setReturnValueAt(6, 'getData', 2.6); |
|---|
| 216 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), '2.6'); |
|---|
| 217 |
$this->tag->setReturnValueAt(7, 'getData', null); |
|---|
| 218 |
$this->assertEqual($converter->convertValue($this->tag, $this->definition), ''); |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
* assert that any other value and type converts to null |
|---|
| 223 |
*/ |
|---|
| 224 |
public function testOther() |
|---|
| 225 |
{ |
|---|
| 226 |
$converter = new PrimitiveValueConverter('stdClass'); |
|---|
| 227 |
$this->assertNull($converter->getType()); |
|---|
| 228 |
$this->tag->setReturnValueAt(0, 'getData', 'false'); |
|---|
| 229 |
$this->assertNull($converter->convertValue($this->tag, $this->definition)); |
|---|
| 230 |
} |
|---|
| 231 |
} |
|---|
| 232 |
?> |
|---|