|
| 1 | +<?php |
| 2 | +namespace mle86\Value\Tests; |
| 3 | + |
| 4 | +use mle86\Value\AbstractValue; |
| 5 | +use mle86\Value\Value; |
| 6 | +require_once 'helpers/TestWrapper4.php'; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Ensures that AbstractValue instances |
| 11 | + * cannot have any magic properties. |
| 12 | + * |
| 13 | + * Implementors might still define public properties |
| 14 | + * (although they're not supposed to), |
| 15 | + * but we cannot really prevent that. |
| 16 | + */ |
| 17 | +class MagicPropertiesTest |
| 18 | + extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + |
| 21 | + const VALID_VALUE = "41111"; |
| 22 | + |
| 23 | + /** |
| 24 | + * @return Value |
| 25 | + */ |
| 26 | + public function testInstance () { |
| 27 | + $tw = new TestWrapper4 (self::VALID_VALUE); |
| 28 | + |
| 29 | + $this->assertTrue(($tw && $tw instanceof TestWrapper4 && $tw instanceof AbstractValue && $tw instanceof Value)); |
| 30 | + $this->assertSame(self::VALID_VALUE, $tw->value()); |
| 31 | + |
| 32 | + return $tw; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @depends testInstance |
| 37 | + */ |
| 38 | + public function testSetMagicProperty (Value $o) { |
| 39 | + $prop = "magic_property_3453465110"; |
| 40 | + $setto = 86; |
| 41 | + |
| 42 | + $pv = (isset($o->{$prop})) ? $o->{$prop} : null; |
| 43 | + $this->assertNull($pv, |
| 44 | + "Newly-create object already has a magic property?!"); |
| 45 | + |
| 46 | + $e = null; |
| 47 | + try { |
| 48 | + $o->{$prop} = $setto; |
| 49 | + } catch (\Throwable $e) { |
| 50 | + // Good! |
| 51 | + // But we still need to check that property... |
| 52 | + } |
| 53 | + |
| 54 | + $this->assertNotNull($e, |
| 55 | + "Setting a magic property did NOT result in an exception!"); |
| 56 | + |
| 57 | + $pv = (isset($o->{$prop})) ? $o->{$prop} : null; |
| 58 | + $this->assertNotEquals($setto, $pv, |
| 59 | + "Setting a magic property WORKED, despite throwing an exception!"); |
| 60 | + $this->assertNull($pv, |
| 61 | + "Setting a magic property resulted in an exception, but also set the property to some unexpected value!"); |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
0 commit comments