|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of the pinepain/js-sandbox PHP library. |
| 4 | + * |
| 5 | + * Copyright (c) 2016-2017 Bogdan Padalko <pinepain@gmail.com> |
| 6 | + * |
| 7 | + * Licensed under the MIT license: http://opensource.org/licenses/MIT |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the |
| 10 | + * LICENSE file that was distributed with this source or visit |
| 11 | + * http://opensource.org/licenses/MIT |
| 12 | + */ |
| 13 | + |
| 14 | + |
| 15 | +namespace Pinepain\JsSandbox\Extractors\PlainExtractors; |
| 16 | + |
| 17 | + |
| 18 | +use DateTime; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use PHPUnit_Framework_MockObject_MockObject; |
| 21 | +use Pinepain\JsSandbox\Extractors\Definition\ExtractorDefinitionInterface; |
| 22 | +use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface; |
| 23 | +use Pinepain\JsSandbox\Extractors\ExtractorInterface; |
| 24 | +use Pinepain\JsSandbox\Extractors\ObjectComponents\ExtractorsObjectStoreInterface; |
| 25 | +use UnexpectedValueException; |
| 26 | +use V8\Context; |
| 27 | +use V8\Isolate; |
| 28 | +use V8\NumberValue; |
| 29 | +use V8\ObjectValue; |
| 30 | + |
| 31 | + |
| 32 | +class NativeObjectExtractorTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var ExtractorsObjectStoreInterface|PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + private $object_store; |
| 38 | + /** |
| 39 | + * @var Isolate |
| 40 | + */ |
| 41 | + private $isolate; |
| 42 | + /** |
| 43 | + * @var Context |
| 44 | + */ |
| 45 | + private $context; |
| 46 | + /** |
| 47 | + * @var ExtractorInterface |
| 48 | + */ |
| 49 | + private $extractor; |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritDoc |
| 53 | + */ |
| 54 | + protected function setUp() |
| 55 | + { |
| 56 | + $this->object_store = $this->getMockForAbstractClass(ExtractorsObjectStoreInterface::class); |
| 57 | + $this->extractor = $this->getMockForAbstractClass(ExtractorInterface::class); |
| 58 | + |
| 59 | + $this->isolate = new Isolate(); |
| 60 | + $this->context = new Context($this->isolate); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @expectedException \Pinepain\JsSandbox\Extractors\ExtractorException |
| 65 | + * @expectedExceptionMessage Value must be of the type object to be able to find instance, number given |
| 66 | + */ |
| 67 | + public function testNonObject() |
| 68 | + { |
| 69 | + $e = new NativeObjectExtractor($this->object_store); |
| 70 | + |
| 71 | + $scalar = new NumberValue($this->isolate, 42); |
| 72 | + |
| 73 | + $definition = $this->getMockForAbstractClass(PlainExtractorDefinitionInterface::class); |
| 74 | + |
| 75 | + $e->extract($this->context, $scalar, $definition, $this->extractor); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @expectedException \Pinepain\JsSandbox\Extractors\ExtractorException |
| 80 | + * @expectedExceptionMessage Unable to find bound native object |
| 81 | + */ |
| 82 | + public function testNotMatching() |
| 83 | + { |
| 84 | + $obj = new ObjectValue($this->context); |
| 85 | + $definition = $this->getMockForAbstractClass(PlainExtractorDefinitionInterface::class); |
| 86 | + |
| 87 | + $this->object_store->expects($this->any()) |
| 88 | + ->method('get') |
| 89 | + ->willThrowException(new UnexpectedValueException()); |
| 90 | + |
| 91 | + $e = new NativeObjectExtractor($this->object_store); |
| 92 | + |
| 93 | + $e->extract($this->context, $obj, $definition, $this->extractor); |
| 94 | + } |
| 95 | + |
| 96 | + public function testWithoutNext() |
| 97 | + { |
| 98 | + $obj = new ObjectValue($this->context); |
| 99 | + $proto = $this->createMock(DateTime::class); |
| 100 | + |
| 101 | + $this->object_store->expects($this->any()) |
| 102 | + ->method('get') |
| 103 | + ->willReturn($proto); |
| 104 | + |
| 105 | + $e = new NativeObjectExtractor($this->object_store); |
| 106 | + |
| 107 | + $definition = $this->getMockForAbstractClass(PlainExtractorDefinitionInterface::class); |
| 108 | + |
| 109 | + $definition->expects($this->any()) |
| 110 | + ->method('getNext') |
| 111 | + ->willReturn(null); |
| 112 | + |
| 113 | + $res = $e->extract($this->context, $obj, $definition, $this->extractor); |
| 114 | + |
| 115 | + $this->assertSame($proto, $res); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @expectedException \Pinepain\JsSandbox\Extractors\ExtractorException |
| 120 | + * @expectedExceptionMessage Native object value constraint failed: value is not an instance of given classes/interfaces |
| 121 | + */ |
| 122 | + public function testNoVariations() |
| 123 | + { |
| 124 | + $obj = new ObjectValue($this->context); |
| 125 | + $proto = $this->createMock(DateTime::class); |
| 126 | + |
| 127 | + $this->object_store->expects($this->any()) |
| 128 | + ->method('get') |
| 129 | + ->willReturn($proto); |
| 130 | + |
| 131 | + $e = new NativeObjectExtractor($this->object_store); |
| 132 | + |
| 133 | + $next = $this->getMockForAbstractClass(ExtractorDefinitionInterface::class); |
| 134 | + $next->expects($this->any()) |
| 135 | + ->method('getVariations') |
| 136 | + ->willReturn([]); |
| 137 | + |
| 138 | + $definition = $this->getMockForAbstractClass(PlainExtractorDefinitionInterface::class); |
| 139 | + |
| 140 | + $definition->expects($this->any()) |
| 141 | + ->method('getNext') |
| 142 | + ->willReturn($next); |
| 143 | + |
| 144 | + $e->extract($this->context, $obj, $definition, $this->extractor); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @expectedException \Pinepain\JsSandbox\Extractors\ExtractorException |
| 149 | + * @expectedExceptionMessage Native object value constraint failed: value is not an instance of given classes/interfaces |
| 150 | + */ |
| 151 | + public function testWithoutMatchingVariations() |
| 152 | + { |
| 153 | + $obj = new ObjectValue($this->context); |
| 154 | + $proto = $this->createMock(DateTime::class); |
| 155 | + |
| 156 | + $this->object_store->expects($this->any()) |
| 157 | + ->method('get') |
| 158 | + ->willReturn($proto); |
| 159 | + |
| 160 | + $e = new NativeObjectExtractor($this->object_store); |
| 161 | + |
| 162 | + $variation = $this->getMockForAbstractClass(ExtractorDefinitionInterface::class); |
| 163 | + $variation->method('getName') |
| 164 | + ->willReturn(self::class); |
| 165 | + |
| 166 | + $next = $this->getMockForAbstractClass(ExtractorDefinitionInterface::class); |
| 167 | + $next->method('getVariations') |
| 168 | + ->willReturn([$variation]); |
| 169 | + |
| 170 | + $definition = $this->getMockForAbstractClass(PlainExtractorDefinitionInterface::class); |
| 171 | + |
| 172 | + $definition->method('getNext') |
| 173 | + ->willReturn($next); |
| 174 | + |
| 175 | + $res = $e->extract($this->context, $obj, $definition, $this->extractor); |
| 176 | + $this->assertSame($proto, $res); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + public function testWithVariations() |
| 181 | + { |
| 182 | + $obj = new ObjectValue($this->context); |
| 183 | + $proto = $this->createMock(DateTime::class); |
| 184 | + |
| 185 | + $this->object_store->expects($this->any()) |
| 186 | + ->method('get') |
| 187 | + ->willReturn($proto); |
| 188 | + |
| 189 | + $e = new NativeObjectExtractor($this->object_store); |
| 190 | + |
| 191 | + $variation = $this->getMockForAbstractClass(ExtractorDefinitionInterface::class); |
| 192 | + $variation->method('getName') |
| 193 | + ->willReturn(DateTime::class); |
| 194 | + |
| 195 | + $next = $this->getMockForAbstractClass(ExtractorDefinitionInterface::class); |
| 196 | + $next->method('getVariations') |
| 197 | + ->willReturn([$variation]); |
| 198 | + |
| 199 | + $definition = $this->getMockForAbstractClass(PlainExtractorDefinitionInterface::class); |
| 200 | + |
| 201 | + $definition->method('getNext') |
| 202 | + ->willReturn($next); |
| 203 | + |
| 204 | + $res = $e->extract($this->context, $obj, $definition, $this->extractor); |
| 205 | + $this->assertSame($proto, $res); |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments