|
| 1 | +<?php |
| 2 | +namespace mle86\Value\Tests; |
| 3 | + |
| 4 | +use mle86\Value\AbstractSerializableValue; |
| 5 | +use mle86\Value\Value; |
| 6 | +require_once 'aux/TestSWrapper6.php'; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests a simple AbstractSerializableValue implementation |
| 11 | + * and its default methods inherited from AbstractSerializableValue. |
| 12 | + */ |
| 13 | +class SerializableValueTest |
| 14 | + extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + |
| 17 | + const VALID_INPUT = "61234"; |
| 18 | + const INVALID_INPUT = "61234 "; |
| 19 | + const VALID_INPUT2 = "69999"; |
| 20 | + |
| 21 | + |
| 22 | + public function testClassExists () { |
| 23 | + $class = 'mle86\\Value\\AbstractValue'; |
| 24 | + $interface = 'mle86\\Value\\Value'; |
| 25 | + |
| 26 | + $this->assertTrue(class_exists($class), |
| 27 | + "Class {$class} not found!"); |
| 28 | + $this->assertTrue(is_a($class, $interface, true), |
| 29 | + "Class {$class} does not implement the {$interface} interface!"); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @depends testClassExists |
| 34 | + * @return AbstractSerializableValue |
| 35 | + */ |
| 36 | + public function testInstance () { |
| 37 | + $tw = new TestSWrapper6 (self::VALID_INPUT); |
| 38 | + |
| 39 | + $this->assertTrue(($tw && $tw instanceof TestSWrapper6 && $tw instanceof AbstractSerializableValue && $tw instanceof Value), |
| 40 | + "new TestSWrapper6() did not result in a valid object"); |
| 41 | + |
| 42 | + return $tw; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @depends testInstance |
| 47 | + */ |
| 48 | + public function testString (AbstractSerializableValue $tw) { |
| 49 | + $s = "<{$tw}>"; |
| 50 | + $expected = "<" . self::VALID_INPUT . ">"; |
| 51 | + |
| 52 | + $this->assertSame($expected, $s, |
| 53 | + "serializable wrapper has a __toString() method, but returned wrong value!"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @depends testInstance |
| 58 | + */ |
| 59 | + public function testJson (AbstractSerializableValue $tw) { |
| 60 | + $j = json_decode(json_encode( array($tw ) )); |
| 61 | + $expected = array($tw->value()); |
| 62 | + |
| 63 | + $this->assertSame($expected, $j, |
| 64 | + "serializable wrapper has a jsonSerialize() method, but returned wrong value!"); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @depends testInstance |
| 69 | + * @depends testString |
| 70 | + */ |
| 71 | + public function testBuiltinEquals (AbstractSerializableValue $tw) { |
| 72 | + $this->assertTrue(($tw == self::VALID_INPUT), |
| 73 | + "serializable wrapper failed builtin== equality check with own initializer!"); |
| 74 | + $this->assertFalse(($tw == self::VALID_INPUT2), |
| 75 | + "serializable wrapper considered other valid initializer as ==equal !"); |
| 76 | + $this->assertFalse(($tw == self::INVALID_INPUT), |
| 77 | + "serializable wrapper considered other, invalid initializer as ==equal !"); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @depends testInstance |
| 82 | + * @depends testBuiltinEquals |
| 83 | + * @expectedException \PHPUnit_Framework_Error |
| 84 | + */ |
| 85 | + public function testBuiltinEqualsZero (AbstractSerializableValue $tw) { |
| 86 | + $this->assertFalse(($tw == 0), |
| 87 | + "serializable wrapper considered zero as ==equal !"); |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
0 commit comments