|
| 1 | +<?php |
| 2 | + |
| 3 | +use JsPhpize\JsPhpize; |
| 4 | + |
| 5 | +class MagicMethodObject |
| 6 | +{ |
| 7 | + public function __get($name) |
| 8 | + { |
| 9 | + if ($name === 'foo') { |
| 10 | + return 'bar'; |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + public function __call($name, array $args) |
| 15 | + { |
| 16 | + if ($name === 'bar') { |
| 17 | + return 'biz'; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public function __isset($name) |
| 22 | + { |
| 23 | + return $name === 'foo'; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class DotHelperTest extends \PHPUnit_Framework_TestCase |
| 28 | +{ |
| 29 | + protected function getDotHelper() |
| 30 | + { |
| 31 | + return eval('return ' . file_get_contents(__DIR__ . '/../src/JsPhpize/Compiler/Helpers/Dot.h') . ';'); |
| 32 | + } |
| 33 | + |
| 34 | + public function testArrayValue() |
| 35 | + { |
| 36 | + $dotHelper = $this->getDotHelper(); |
| 37 | + |
| 38 | + $this->assertSame(42, $dotHelper(array( |
| 39 | + 'foo' => 42, |
| 40 | + ), 'foo')); |
| 41 | + $this->assertSame('biz', $dotHelper(array( |
| 42 | + 'foo' => array( |
| 43 | + 'bar' => 'biz', |
| 44 | + ), |
| 45 | + ), 'foo', 'bar')); |
| 46 | + $this->assertSame(null, $dotHelper(array( |
| 47 | + 'foo' => array( |
| 48 | + ), |
| 49 | + ), 'foo', 'bar')); |
| 50 | + $this->assertSame(null, $dotHelper(array( |
| 51 | + 'foo' => array( |
| 52 | + ), |
| 53 | + ), 'biz', 'bar')); |
| 54 | + } |
| 55 | + |
| 56 | + public function testObjectMember() |
| 57 | + { |
| 58 | + $dotHelper = $this->getDotHelper(); |
| 59 | + |
| 60 | + $this->assertSame(42, $dotHelper((object) array( |
| 61 | + 'foo' => 42, |
| 62 | + ), 'foo')); |
| 63 | + $this->assertSame('biz', $dotHelper((object) array( |
| 64 | + 'foo' => (object) array( |
| 65 | + 'bar' => 'biz', |
| 66 | + ), |
| 67 | + ), 'foo', 'bar')); |
| 68 | + $this->assertSame(null, $dotHelper((object) array( |
| 69 | + 'foo' => (object) array( |
| 70 | + ), |
| 71 | + ), 'foo', 'bar')); |
| 72 | + $this->assertSame(null, $dotHelper((object) array( |
| 73 | + 'foo' => (object) array( |
| 74 | + ), |
| 75 | + ), 'biz', 'bar')); |
| 76 | + } |
| 77 | + |
| 78 | + public function testMixedObjectMemberAndArayValue() |
| 79 | + { |
| 80 | + $dotHelper = $this->getDotHelper(); |
| 81 | + |
| 82 | + $this->assertSame('biz', $dotHelper(array( |
| 83 | + 'foo' => (object) array( |
| 84 | + 'bar' => 'biz', |
| 85 | + ), |
| 86 | + ), 'foo', 'bar')); |
| 87 | + $this->assertSame('biz', $dotHelper((object) array( |
| 88 | + 'foo' => array( |
| 89 | + 'bar' => 'biz', |
| 90 | + ), |
| 91 | + ), 'foo', 'bar')); |
| 92 | + $this->assertSame(42, $dotHelper((object) array( |
| 93 | + 'foo' => array( |
| 94 | + 'bar' => (object) array( |
| 95 | + 'biz' => 42, |
| 96 | + ), |
| 97 | + ), |
| 98 | + ), 'foo', 'bar', 'biz')); |
| 99 | + $this->assertSame(42, $dotHelper(array( |
| 100 | + 'foo' => (object) array( |
| 101 | + 'bar' => array( |
| 102 | + 'biz' => 42, |
| 103 | + ), |
| 104 | + ), |
| 105 | + ), 'foo', 'bar', 'biz')); |
| 106 | + } |
| 107 | + |
| 108 | + public function testMagicMethod() |
| 109 | + { |
| 110 | + $dotHelper = $this->getDotHelper(); |
| 111 | + $object = new MagicMethodObject(); |
| 112 | + |
| 113 | + $this->assertSame('bar', $dotHelper($object, 'foo')); |
| 114 | + $this->assertSame('biz', call_user_func($dotHelper($object, 'bar'))); |
| 115 | + $this->assertSame(null, call_user_func($dotHelper($object, 'biz'))); |
| 116 | + } |
| 117 | +} |
0 commit comments