|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the pinepain/js-sandbox PHP library. |
| 5 | + * |
| 6 | + * Copyright (c) 2016-2017 Bogdan Padalko <pinepain@gmail.com> |
| 7 | + * |
| 8 | + * Licensed under the MIT license: http://opensource.org/licenses/MIT |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the |
| 11 | + * LICENSE file that was distributed with this source or visit |
| 12 | + * http://opensource.org/licenses/MIT |
| 13 | + */ |
| 14 | + |
| 15 | + |
| 16 | +namespace Pinepain\JsSandbox\Tests\Wrappers\FunctionComponents\Runtime; |
| 17 | + |
| 18 | + |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use PHPUnit_Framework_MockObject_MockObject; |
| 21 | +use Pinepain\JsSandbox\NativeWrappers\NativeFunctionWrapperInterface; |
| 22 | +use Pinepain\JsSandbox\Specs\FunctionSpecInterface; |
| 23 | +use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ExecutionContext; |
| 24 | +use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunctionInterface; |
| 25 | +use Pinepain\JsSandbox\Wrappers\WrapperInterface; |
| 26 | +use V8\Context; |
| 27 | +use V8\FunctionCallbackInfo; |
| 28 | +use V8\FunctionObject; |
| 29 | +use V8\Isolate; |
| 30 | +use V8\ObjectValue; |
| 31 | + |
| 32 | + |
| 33 | +class ExecutionContextTest extends TestCase |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var WrapperInterface|PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $wrapper; |
| 39 | + /** |
| 40 | + * @var RuntimeFunctionInterface|PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $runtime_function; |
| 43 | + /** |
| 44 | + * @var FunctionCallbackInfo|PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + private $args; |
| 47 | + /** |
| 48 | + * @var FunctionSpecInterface|PHPUnit_Framework_MockObject_MockObject |
| 49 | + */ |
| 50 | + private $spec; |
| 51 | + /** |
| 52 | + * @var Isolate |
| 53 | + */ |
| 54 | + private $isolate; |
| 55 | + /** |
| 56 | + * @var Context |
| 57 | + */ |
| 58 | + private $context; |
| 59 | + |
| 60 | + public function setUp() |
| 61 | + { |
| 62 | + $this->wrapper = $this->getMockBuilder(WrapperInterface::class)->getMockForAbstractClass(); |
| 63 | + $this->runtime_function = $this->getMockBuilder(RuntimeFunctionInterface::class)->getMockForAbstractClass(); |
| 64 | + $this->args = $this->getMockBuilder(FunctionCallbackInfo::class) |
| 65 | + ->setMethods(['getIsolate', 'getContext']) |
| 66 | + ->getMockForAbstractClass(); |
| 67 | + |
| 68 | + $this->isolate = new Isolate(); |
| 69 | + $this->context = new Context($this->isolate); |
| 70 | + |
| 71 | + $this->args->expects($this->any()) |
| 72 | + ->method('getIsolate') |
| 73 | + ->willReturn($this->isolate); |
| 74 | + |
| 75 | + $this->args->expects($this->any()) |
| 76 | + ->method('getContext') |
| 77 | + ->willReturn($this->context); |
| 78 | + |
| 79 | + $this->spec = $this->getMockBuilder(FunctionSpecInterface::class)->getMockForAbstractClass(); |
| 80 | + } |
| 81 | + |
| 82 | + public function testDelegatedGetters() |
| 83 | + { |
| 84 | + $isolate = new Isolate(); |
| 85 | + $context = new Context($isolate); |
| 86 | + $this_obj = new ObjectValue($context); |
| 87 | + |
| 88 | + $this->args = $this->getMockBuilder(FunctionCallbackInfo::class) |
| 89 | + ->setMethods(['getIsolate', 'getContext', 'this']) |
| 90 | + ->getMockForAbstractClass(); |
| 91 | + |
| 92 | + $this->args->expects($this->any()) |
| 93 | + ->method('getIsolate') |
| 94 | + ->willReturn($isolate); |
| 95 | + |
| 96 | + $this->args->expects($this->any()) |
| 97 | + ->method('getContext') |
| 98 | + ->willReturn($context); |
| 99 | + |
| 100 | + $this->args->expects($this->any()) |
| 101 | + ->method('this') |
| 102 | + ->willReturn($this_obj); |
| 103 | + |
| 104 | + $exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec); |
| 105 | + |
| 106 | + $this->assertSame($isolate, $exec->getIsolate()); |
| 107 | + $this->assertSame($context, $exec->getContext()); |
| 108 | + $this->assertSame($this_obj, $exec->getThis()); |
| 109 | + } |
| 110 | + |
| 111 | + public function testGetters() |
| 112 | + { |
| 113 | + $exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec); |
| 114 | + |
| 115 | + $this->assertSame($this->wrapper, $exec->getWrapper()); |
| 116 | + $this->assertSame($this->runtime_function, $exec->getRuntimeFunction()); |
| 117 | + $this->assertSame($this->args, $exec->getFunctionCallbackInfo()); |
| 118 | + $this->assertSame($this->spec, $exec->getFunctionSpec()); |
| 119 | + } |
| 120 | + |
| 121 | + public function testWrapping() |
| 122 | + { |
| 123 | + $value = 'test string'; |
| 124 | + $wrapped = $this->getMockBuilder(FunctionObject::class) |
| 125 | + ->disableOriginalConstructor() |
| 126 | + ->getMock(); |
| 127 | + |
| 128 | + $this->wrapper = $this->getMockBuilder(WrapperInterface::class) |
| 129 | + ->setMethods(['wrap']) |
| 130 | + ->getMockForAbstractClass(); |
| 131 | + |
| 132 | + $this->wrapper->expects($this->once()) |
| 133 | + ->method('wrap') |
| 134 | + ->with($this->isolate, $this->context, $value) |
| 135 | + ->willReturn($wrapped); |
| 136 | + |
| 137 | + $exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec); |
| 138 | + |
| 139 | + $this->assertSame($wrapped, $exec->wrap($value)); |
| 140 | + |
| 141 | + |
| 142 | + $this->wrapper = $this->getMockBuilder(WrapperInterface::class) |
| 143 | + ->setMethods(['wrap']) |
| 144 | + ->getMockForAbstractClass(); |
| 145 | + |
| 146 | + $this->wrapper->expects($this->once()) |
| 147 | + ->method('wrap') |
| 148 | + ->with($this->isolate, $this->context, $this->runtime_function) |
| 149 | + ->willReturn($wrapped); |
| 150 | + |
| 151 | + $exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec); |
| 152 | + |
| 153 | + $this->assertSame($wrapped, $exec->getFunctionObject()); |
| 154 | + } |
| 155 | + |
| 156 | + public function testWrapNativeFunction() |
| 157 | + { |
| 158 | + /** @var ObjectValue|PHPUnit_Framework_MockObject_MockObject $recv */ |
| 159 | + $recv = $this->getMockBuilder(ObjectValue::class) |
| 160 | + ->disableOriginalConstructor() |
| 161 | + ->getMock(); |
| 162 | + |
| 163 | + /** @var FunctionObject|PHPUnit_Framework_MockObject_MockObject $target */ |
| 164 | + $target = $this->getMockBuilder(FunctionObject::class) |
| 165 | + ->disableOriginalConstructor() |
| 166 | + ->getMock(); |
| 167 | + |
| 168 | + $exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec); |
| 169 | + |
| 170 | + $wrapper = $exec->wrapNativeFunction($recv, $target); |
| 171 | + |
| 172 | + $this->assertInstanceOf(NativeFunctionWrapperInterface::class, $wrapper); |
| 173 | + } |
| 174 | +} |
0 commit comments