Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 90d8244

Browse files
committed
Rename "instance" extractor to "native_object" one
1 parent f0b0ff6 commit 90d8244

File tree

4 files changed

+215
-6
lines changed

4 files changed

+215
-6
lines changed

src/Extractors/PlainExtractors/InstanceExtractor.php renamed to src/Extractors/PlainExtractors/NativeObjectExtractor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use V8\Value;
2727

2828

29-
class InstanceExtractor implements PlainExtractorInterface
29+
class NativeObjectExtractor implements PlainExtractorInterface
3030
{
3131
/**
3232
* @var ExtractorsObjectStoreInterface
@@ -54,12 +54,12 @@ public function extract(Context $context, Value $value, PlainExtractorDefinition
5454
}
5555
}
5656

57-
throw new ExtractorException('Instance value constraint failed: value is not an instance of given classes/interfaces');
57+
throw new ExtractorException('Native object value constraint failed: value is not an instance of given classes/interfaces');
5858
}
5959

6060
return $instance;
6161
} catch (UnexpectedValueException $e) {
62-
throw new ExtractorException('Unable to find instance for the given object');
62+
throw new ExtractorException('Unable to find bound native object');
6363
}
6464
}
6565

src/Extractors/PlainExtractors/PlainExtractorInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface;
20+
use Pinepain\JsSandbox\Extractors\ExtractorException;
2021
use Pinepain\JsSandbox\Extractors\ExtractorInterface;
2122
use V8\Context;
2223
use V8\Value;
@@ -28,10 +29,10 @@ interface PlainExtractorInterface
2829
* @param Context $context
2930
* @param Value $value
3031
* @param PlainExtractorDefinitionInterface $definition
31-
*
3232
* @param ExtractorInterface $extractor
3333
*
3434
* @return mixed
35+
* @throws ExtractorException
3536
*/
3637
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor);
3738
}

src/Laravel/JsSandboxServiceProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
use Pinepain\JsSandbox\Extractors\PlainExtractors\DateExtractor;
3636
use Pinepain\JsSandbox\Extractors\PlainExtractors\DateTimeExtractor;
3737
use Pinepain\JsSandbox\Extractors\PlainExtractors\FunctionExtractor;
38-
use Pinepain\JsSandbox\Extractors\PlainExtractors\InstanceExtractor;
38+
use Pinepain\JsSandbox\Extractors\PlainExtractors\NativeObjectExtractor;
3939
use Pinepain\JsSandbox\Extractors\PlainExtractors\JsonableExtractor;
4040
use Pinepain\JsSandbox\Extractors\PlainExtractors\JsonExtractor;
4141
use Pinepain\JsSandbox\Extractors\PlainExtractors\NullExtractor;
@@ -249,7 +249,7 @@ protected function registerExtractor()
249249
$collection->put('datetime', $datetime = new DateTimeExtractor());
250250
$collection->put('object', $object = new ObjectExtractor());
251251
$collection->put('function', $function = new FunctionExtractor());
252-
$collection->put('instance', $instance = $app->make(InstanceExtractor::class));
252+
$collection->put('native_object', $instance = $app->make(NativeObjectExtractor::class));
253253

254254
$collection->put('assoc', $assoc = new AssocExtractor());
255255
$collection->put('json', $json = new JsonExtractor());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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

Comments
 (0)