|
16 | 16 | use ScaleUpStack\EasyObject\Magic\NamedConstructor; |
17 | 17 | use ScaleUpStack\EasyObject\Magic\VirtualGetter; |
18 | 18 | use ScaleUpStack\EasyObject\Tests\Resources\Magic\ClassForDispatcherTesting; |
| 19 | +use ScaleUpStack\EasyObject\Tests\Resources\Magic\ClassForNamedConstructorTesting; |
19 | 20 | use ScaleUpStack\EasyObject\Tests\Resources\TestCase; |
20 | 21 | use ScaleUpStack\Reflection\Reflection; |
21 | 22 |
|
@@ -90,6 +91,23 @@ public function it_invokes_a_virtual_static_method_on_some_object_via_some_call_ |
90 | 91 | ); |
91 | 92 | } |
92 | 93 |
|
| 94 | + /** |
| 95 | + * @test |
| 96 | + * @covers ::doInvocation() |
| 97 | + */ |
| 98 | + public function it_can_execute_the_static_method_non_statically_on_objects() |
| 99 | + { |
| 100 | + // given an object with a __call() method that does not handle the static method itself but has a __callStatic() |
| 101 | + // method that can handle it |
| 102 | + $object = ClassForNamedConstructorTesting::create('some string', 42); |
| 103 | + |
| 104 | + // when calling the factory method non-statically |
| 105 | + $newObject = $object->create('some other string', 11); |
| 106 | + |
| 107 | + // then the invocation is redirected to __callStatic(), and the result is as expected from the CallHandler |
| 108 | + $this->assertInstanceOf(ClassForNamedConstructorTesting::class, $newObject); |
| 109 | + } |
| 110 | + |
93 | 111 | /** |
94 | 112 | * @test |
95 | 113 | * @covers ::assertGivenParametersMatchMethodSignature() |
@@ -123,6 +141,30 @@ public function it_throws_an_exception_when_the_number_of_parameters_is_invalid( |
123 | 141 | ); |
124 | 142 | } |
125 | 143 |
|
| 144 | + /** |
| 145 | + * @test |
| 146 | + * @covers ::doInvocation() |
| 147 | + * @covers \ScaleUpStack\EasyObject\Magic\AbstractCallHandler::requiresObjectContext() |
| 148 | + */ |
| 149 | + public function it_throws_an_exception_when_executing_a_call_handler_statically_that_requires_object_context() |
| 150 | + { |
| 151 | + // given a mocked AbstractCallHandler, and ClassMetadata of some object as provided in setUp() |
| 152 | + |
| 153 | + // when calling a CallHandler, that requires an object context, statically |
| 154 | + // then an exception is thrown |
| 155 | + $this->expectException(\Error::class); |
| 156 | + $this->expectExceptionMessage("Calling a non-static method when not in object context."); |
| 157 | + |
| 158 | + Dispatcher::invokeStatically( |
| 159 | + ClassForDispatcherTesting::class, |
| 160 | + 'getSomeProperty', |
| 161 | + [], |
| 162 | + [ |
| 163 | + VirtualGetter::class |
| 164 | + ] |
| 165 | + ); |
| 166 | + } |
| 167 | + |
126 | 168 | /** |
127 | 169 | * @test |
128 | 170 | * @covers ::assertCorrectReturnType() |
|
0 commit comments