| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if (!extension_loaded('syck')) |
|---|
| 4 | dl('syck.so'); |
|---|
| 5 | |
|---|
| 6 | date_default_timezone_set('GMT'); |
|---|
| 7 | |
|---|
| 8 | require_once "PHPUnit/Framework/TestCase.php"; |
|---|
| 9 | require 'helpers.php'; |
|---|
| 10 | |
|---|
| 11 | error_reporting(E_ALL | E_STRICT); |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | class TestLoad extends PHPUnit_Framework_TestCase |
|---|
| 15 | { |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public function testTest() |
|---|
| 21 | { |
|---|
| 22 | $this->assertEquals(1, 1); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | public function testExtensionLoaded() |
|---|
| 26 | { |
|---|
| 27 | $this->assertTrue(function_exists('syck_load')); |
|---|
| 28 | $this->assertTrue(function_exists('syck_dump')); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | public function testNull() |
|---|
| 36 | { |
|---|
| 37 | $this->assertNull(syck_load("---\n")); |
|---|
| 38 | $this->assertNull(syck_load("")); |
|---|
| 39 | $this->assertNull(syck_load("~")); |
|---|
| 40 | $this->assertNull(syck_load("null")); |
|---|
| 41 | $this->assertNull(syck_load("Null")); |
|---|
| 42 | $this->assertNull(syck_load("NULL")); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | public function testBool() |
|---|
| 46 | { |
|---|
| 47 | $this->assertTrue(syck_load("yes")); |
|---|
| 48 | $this->assertTrue(syck_load("Yes")); |
|---|
| 49 | $this->assertTrue(syck_load("YES")); |
|---|
| 50 | $this->assertTrue(syck_load("true")); |
|---|
| 51 | $this->assertTrue(syck_load("True")); |
|---|
| 52 | $this->assertTrue(syck_load("TRUE")); |
|---|
| 53 | $this->assertTrue(syck_load("on")); |
|---|
| 54 | $this->assertTrue(syck_load("On")); |
|---|
| 55 | $this->assertTrue(syck_load("ON")); |
|---|
| 56 | $this->assertTrue(syck_load("!bool y")); |
|---|
| 57 | $this->assertTrue(syck_load("!bool Y")); |
|---|
| 58 | |
|---|
| 59 | $this->assertFalse(syck_load("no")); |
|---|
| 60 | $this->assertFalse(syck_load("No")); |
|---|
| 61 | $this->assertFalse(syck_load("NO")); |
|---|
| 62 | $this->assertFalse(syck_load("false")); |
|---|
| 63 | $this->assertFalse(syck_load("False")); |
|---|
| 64 | $this->assertFalse(syck_load("FALSE")); |
|---|
| 65 | $this->assertFalse(syck_load("off")); |
|---|
| 66 | $this->assertFalse(syck_load("Off")); |
|---|
| 67 | $this->assertFalse(syck_load("OFF")); |
|---|
| 68 | $this->assertFalse(syck_load("!bool n")); |
|---|
| 69 | $this->assertFalse(syck_load("!bool N")); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public function testString() |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | $this->assertSame(syck_load('a string'), 'a string'); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | $this->assertSame(syck_load('!str 12'), '12'); |
|---|
| 79 | $this->assertSame(syck_load('"12"'), '12'); |
|---|
| 80 | $this->assertSame(syck_load('"\ |
|---|
| 81 | 1\ |
|---|
| 82 | 2"'), '12'); |
|---|
| 83 | $this->assertSame(syck_load("'12'"), '12'); |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | $this->assertSame('ÐÑОвеÑ', syck_load("'ÐÑОвеÑ'")); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public function testInteger() |
|---|
| 90 | { |
|---|
| 91 | $this->assertSame(syck_load('685230'), 685230); |
|---|
| 92 | $this->assertSame(syck_load('+685,230'), 685230); |
|---|
| 93 | $this->assertSame(syck_load('02472256'), 685230); |
|---|
| 94 | $this->assertSame(syck_load('0x,0A,74,AE'), 685230); |
|---|
| 95 | $this->assertSame(syck_load('0xF00D'), 0xF00D); |
|---|
| 96 | $this->assertSame(syck_load('07654321'), 07654321); |
|---|
| 97 | $this->assertSame(syck_load('190:20:30'), 685230); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public function testFloatFix() |
|---|
| 101 | { |
|---|
| 102 | $this->assertSame(syck_load('99.0'), 99.0); |
|---|
| 103 | $this->assertSame(syck_load('!float 99'), 99.0); |
|---|
| 104 | $this->assertSame(syck_load('190:20:30.15'), 6.8523015e+5); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public function testFloatExponential() |
|---|
| 108 | { |
|---|
| 109 | $this->assertSame(syck_load('1.0e+1'), 10.0); |
|---|
| 110 | $this->assertSame(syck_load('1.0e-1'), 0.1); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | public function testInfinity() |
|---|
| 114 | { |
|---|
| 115 | $this->assertSame(syck_load('.inf'), INF); |
|---|
| 116 | $this->assertSame(syck_load('.Inf'), INF); |
|---|
| 117 | $this->assertSame(syck_load('.INF'), INF); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public function testNegativeInfinity() |
|---|
| 121 | { |
|---|
| 122 | $this->assertSame(syck_load('-.inf'), -INF); |
|---|
| 123 | $this->assertSame(syck_load('-.Inf'), -INF); |
|---|
| 124 | $this->assertSame(syck_load('-.INF'), -INF); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | public function testNan() |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | $this->assertEquals(syck_load('.nan'), NAN); |
|---|
| 131 | $this->assertEquals(syck_load('.NaN'), NAN); |
|---|
| 132 | $this->assertEquals(syck_load('.NAN'), NAN); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | public function testTimestamps() |
|---|
| 136 | { |
|---|
| 137 | |
|---|
| 138 | $this->assertType('DateTime', syck_load("2001-12-15T02:59:43.1Z")); |
|---|
| 139 | |
|---|
| 140 | $this->assertType('DateTime', syck_load("2001-12-14t21:59:43.10-05:00")); |
|---|
| 141 | |
|---|
| 142 | $this->assertType('DateTime', syck_load("2001-12-14 21:59:43.10 -05")); |
|---|
| 143 | |
|---|
| 144 | $this->assertType('DateTime', syck_load("2002-12-14")); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | $this->assertType('DateTime', syck_load("!php/object::Datetime 2002-12-14")); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | public function testArray() |
|---|
| 151 | { |
|---|
| 152 | $this->assertEquals(syck_load('[]'), array()); |
|---|
| 153 | $this->assertEquals(syck_load('[a, b, c]'), array('a', 'b', 'c')); |
|---|
| 154 | $this->assertEquals(syck_load('!php/array []'), array()); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | $this->assertEquals(new ArrayObject(), syck_load('!php/array::ArrayObject []')); |
|---|
| 158 | $this->assertEquals(new ArrayObject(array(1, 2, 3)), syck_load('!php/array::ArrayObject [1, 2, 3]')); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | try { |
|---|
| 162 | syck_load('!php/array::SyckTestSomeClass []'); |
|---|
| 163 | $this->assertTrue(false); |
|---|
| 164 | } catch (SyckException $e) { |
|---|
| 165 | $this->assertTrue(true); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | try { |
|---|
| 170 | syck_load('!php/array::SyckTestOtherClass []'); |
|---|
| 171 | $this->assertTrue(false); |
|---|
| 172 | } catch (SyckException $e) { |
|---|
| 173 | $this->assertTrue(true); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | public function testHash() |
|---|
| 178 | { |
|---|
| 179 | $this->assertEquals(array(), syck_load('{}')); |
|---|
| 180 | $this->assertEquals(array('a', 'b', 'c'), syck_load('{0: a, 1: b, 2: c}')); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | $this->assertEquals(array('пÑОвеÑ' => 'ЌОÑ'), syck_load('{"пÑОвеÑ": "ЌОÑ"}')); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | $this->assertEquals(new ArrayObject(), syck_load('!php/hash::ArrayObject {}')); |
|---|
| 187 | $this->assertEquals( |
|---|
| 188 | new ArrayObject(array('a' => 1, 'b' => 2, 3 => 3, 4 => 'd', 'e' => 5)), |
|---|
| 189 | syck_load('!php/hash::ArrayObject {a: 1, b: 2, 3: 3, 4: d, e: 5}') |
|---|
| 190 | ); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | try { |
|---|
| 194 | syck_load('!php/hash::SyckTestSomeClass {}'); |
|---|
| 195 | $this->assertTrue(false); |
|---|
| 196 | } catch (SyckException $e) { |
|---|
| 197 | $this->assertTrue(true); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | try { |
|---|
| 202 | syck_load('!php/hash::SyckTestOtherClass {}'); |
|---|
| 203 | $this->assertTrue(false); |
|---|
| 204 | } catch (SyckException $e) { |
|---|
| 205 | $this->assertTrue(true); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | public function testSerializable() |
|---|
| 210 | { |
|---|
| 211 | $obj = syck_load('!php/object::MySerializable teststring'); |
|---|
| 212 | |
|---|
| 213 | $this->assertType('MySerializable', $obj); |
|---|
| 214 | $this->assertSame('teststring', $obj->test()); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | public function testBug31() |
|---|
| 218 | { |
|---|
| 219 | try { |
|---|
| 220 | $obj = syck_load(file_get_contents(dirname(__FILE__).'/bug31.yaml')); |
|---|
| 221 | $this->assertTrue(false); |
|---|
| 222 | } catch (SyckException $e) { |
|---|
| 223 | $this->assertTrue(true); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | public function testBugPECL_14384() |
|---|
| 228 | { |
|---|
| 229 | $broken_string = 'test: |
|---|
| 230 | var: value |
|---|
| 231 | var2: value2'; |
|---|
| 232 | |
|---|
| 233 | try { |
|---|
| 234 | syck_load($broken_string); |
|---|
| 235 | $this->assertTrue(false); |
|---|
| 236 | } catch (SyckException $e) { |
|---|
| 237 | $this->assertTrue(true); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | } |
|---|