Skip to content

Commit 5339eae

Browse files
Added $options parameter to Callhandler::canHandle().
1 parent cf1ef74 commit 5339eae

8 files changed

Lines changed: 12 additions & 104 deletions

File tree

src/Magic/AbstractCallHandler.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,6 @@ protected function propertyName(
7373
return null;
7474
}
7575

76-
protected function assertCanHandle(string $methodName, ClassMetadata $classMetadata)
77-
{
78-
if (! $this->canHandle($methodName, $classMetadata)) {
79-
throw new \Error(
80-
sprintf(
81-
'Call to undefined method %s::%s()',
82-
$classMetadata->name,
83-
$methodName
84-
)
85-
);
86-
}
87-
}
88-
8976
protected function assertGivenParametersMatchMethodSignature(
9077
string $methodName,
9178
array $parameters,

src/Magic/CallHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ interface CallHandler
2424
*
2525
* @return bool
2626
*/
27-
public function canHandle(string $methodName, ClassMetadata $classMetadata) : bool;
27+
public function canHandle(string $methodName, ClassMetadata $classMetadata, array $options) : bool;
2828

2929
/**
30+
* Executes a method on a given object.
31+
*
3032
* Do some assertions regarding the method, and execute the relevant code. For example:
3133
*
3234
* - Assert that the method can be handled be the CallHandler.

src/Magic/Dispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function invoke(
6363

6464
$callHandler = $instance->callHandlers[$callHandlerClassName];
6565

66-
if ($callHandler->canHandle($methodName, $classMetadata)) {
66+
if ($callHandler->canHandle($methodName, $classMetadata, [])) {
6767
return $callHandler->execute($object, $methodName, $arguments, $classMetadata);
6868
}
6969
}

src/Magic/FixtureBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class FixtureBuilder extends AbstractCallHandler
2323
{
2424
private const CONFIGURED_PROPERTIES = 'configuredProperties';
2525

26-
public function canHandle(string $methodName, ClassMetadata $classMetadata) : bool
26+
public function canHandle(string $methodName, ClassMetadata $classMetadata, array $options) : bool
2727
{
2828
// a build() method is always required
2929
if (! $this->checkForMethod('build', 0, $classMetadata)) {
@@ -55,7 +55,6 @@ public function canHandle(string $methodName, ClassMetadata $classMetadata) : bo
5555

5656
public function execute(object $object, string $methodName, array $arguments, ClassMetadata $classMetadata)
5757
{
58-
$this->assertCanHandle($methodName, $classMetadata);
5958
$this->assertGivenParametersMatchMethodSignature($methodName, $arguments, $classMetadata);
6059

6160
$toBeBuiltClassMetadata = $this->getMetadataOfClassToBeBuilt($classMetadata);

src/Magic/VirtualGetter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
final class VirtualGetter extends AbstractCallHandler
1919
{
20-
public function canHandle(string $methodName, ClassMetadata $classMetadata) : bool
20+
public function canHandle(string $methodName, ClassMetadata $classMetadata, array $options) : bool
2121
{
2222
return (
2323
$this->checkForMethod($methodName, 0, $classMetadata) &&
@@ -27,7 +27,6 @@ public function canHandle(string $methodName, ClassMetadata $classMetadata) : bo
2727

2828
public function execute(object $object, string $methodName, array $arguments, ClassMetadata $classMetadata)
2929
{
30-
$this->assertCanHandle($methodName, $classMetadata);
3130
$this->assertGivenParametersMatchMethodSignature($methodName, $arguments, $classMetadata);
3231

3332
$propertyName = $this->propertyName($methodName, 'get', false, $classMetadata);

tests/PhpUnit/Magic/AbstractCallHandlerTest.php

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -116,57 +116,6 @@ public function it_returns_the_corresponding_property_name_to_a_method_name(
116116
$this->assertSame($expectedPropertyResult, $result);
117117
}
118118

119-
/**
120-
* @test
121-
* @covers ::assertCanHandle()
122-
*/
123-
public function it_returns_void_on_assertCanHandle_when_it_can_handle_a_method()
124-
{
125-
// given a mocked AbstractCallHandler, and ClassMetadata of some object as provided in setUp()
126-
// and the AbstractCallHandler can handle the method
127-
$this->callHandler->method('canHandle')->willReturn(true);
128-
129-
// when asserting that a method can be handled
130-
$result = Reflection::methodOfClass(AbstractCallHandler::class, 'assertCanHandle')
131-
->invoke(
132-
$this->callHandler,
133-
'getSomeProperty',
134-
$this->classMetadata
135-
);
136-
137-
// then the $result was null
138-
$this->assertNull($result);
139-
}
140-
141-
/**
142-
* @test
143-
* @covers ::assertCanHandle()
144-
*/
145-
public function it_throws_an_exception_on_assertCanHandle_when_it_cannot_handle_a_method()
146-
{
147-
// given a mocked AbstractCallHandler, and ClassMetadata of some object as provided in setUp()
148-
// and the AbstractCallHandler can't handle the method
149-
$this->callHandler->method('canHandle')->willReturn(false);
150-
151-
// when asserting that a method can be handled
152-
// then an exception is thrown
153-
$this->expectException(\Error::class);
154-
$this->expectExceptionMessage(
155-
sprintf(
156-
'Call to undefined method %s::%s()',
157-
ClassForAbstractCallHandlerTesting::class,
158-
'someUnknownMethod'
159-
)
160-
);
161-
162-
Reflection::methodOfClass(AbstractCallHandler::class, 'assertCanHandle')
163-
->invoke(
164-
$this->callHandler,
165-
'someUnknownMethod',
166-
$this->classMetadata
167-
);
168-
}
169-
170119
/**
171120
* @test
172121
* @covers ::assertGivenParametersMatchMethodSignature()

tests/PhpUnit/Magic/FixtureBuilderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function it_knows_if_it_can_handle_a_method_call(string $methodName, bool
5353
->classMetadata[ClassForFixtureBuilderTesting::class];
5454

5555
// when checking if the handler is registered for a method
56-
$canHandle = $callHandler->canHandle($methodName, $classMetadata);
56+
$canHandle = $callHandler->canHandle($methodName, $classMetadata, []);
5757

5858
// then the result is as expeced
5959
$this->assertSame($expectedCanHandle, $canHandle);
@@ -73,14 +73,14 @@ public function it_cannot_handle_a_with_method_if_build_method_is_missing()
7373
->classMetadata[ClassForFixtureBuilderTesting::class];
7474
// and a method name that would be supported (if build() is available)
7575
$methodName = 'withSomeProperty';
76-
Assert::true($callHandler->canHandle($methodName, $classMetadata));
76+
Assert::true($callHandler->canHandle($methodName, $classMetadata, []));
7777
// but the ClassMetadata has no build() method
7878
/** @var ClassMetadata $classMetadata */
7979
$classMetadata = clone $classMetadata;
8080
unset($classMetadata->features[VirtualMethods::FEATURES_KEY]['build']);
8181

8282
// when checking if the handler can handle a with method
83-
$result = $callHandler->canHandle($methodName, $classMetadata);
83+
$result = $callHandler->canHandle($methodName, $classMetadata, []);
8484

8585
// then the result is false
8686
$this->assertFalse($result);
@@ -101,7 +101,7 @@ public function it_cannot_handle_a_with_method_if_builder_method_has_no_return_t
101101
->classMetadata[ClassForFixtureBuilderTesting::class];
102102
// and a method name that would be supported (if build() would have a return type)
103103
$methodName = 'withSomeProperty';
104-
Assert::true($callHandler->canHandle($methodName, $classMetadata));
104+
Assert::true($callHandler->canHandle($methodName, $classMetadata, []));
105105
// but the ClassMetadata's build() method has no return type
106106
$classMetadata = clone $classMetadata;
107107
$classMetadata->features[VirtualMethods::FEATURES_KEY]['build'] =
@@ -113,7 +113,7 @@ public function it_cannot_handle_a_with_method_if_builder_method_has_no_return_t
113113
);
114114

115115
// when checking if the handler can handle a with method
116-
$result = $callHandler->canHandle($methodName, $classMetadata);
116+
$result = $callHandler->canHandle($methodName, $classMetadata, []);
117117

118118
// then the result is false
119119
$this->assertFalse($result);

tests/PhpUnit/Magic/VirtualGetterTest.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function it_knows_if_it_can_handle_a_virtual_method(
7373
$metadata = $this->getClassMetadata($annotationTag, $annotationArguments);
7474

7575
// when checking if the handler is registered for a property
76-
$canHandle = $handler->canHandle($methodName, $metadata);
76+
$canHandle = $handler->canHandle($methodName, $metadata, []);
7777

7878
// then the result is as expeced (as provided by the test method's parameter)
7979
$this->assertSame($expectedCanHandle, $canHandle);
@@ -134,34 +134,6 @@ public function it_throws_an_exception_when_the_returned_value_is_of_wrong_type(
134134
$handler->execute($object, 'getSomeProperty', [], $metadata);
135135
}
136136

137-
/**
138-
* @test
139-
* @covers ::execute()
140-
*/
141-
public function it_throws_an_exception_when_it_cannot_handle_the_method()
142-
{
143-
// given a VirtualGetter call handler, an object, and the object's ClassMetadata
144-
$handler = new VirtualGetter();
145-
$object = new ClassForMagicTesting();
146-
$metadata = $this->getClassMetadata(
147-
'method',
148-
'string getSomeProperty()'
149-
);
150-
151-
// when executing a virtual method that it cannot handle
152-
// then an exception is thrown
153-
$this->expectException(\Error::class);
154-
$this->expectExceptionMessage(
155-
sprintf(
156-
'Call to undefined method %s::%s()',
157-
ClassForMagicTesting::class,
158-
'unknownMethod'
159-
)
160-
);
161-
162-
$handler->execute($object, 'unknownMethod', [], $metadata);
163-
}
164-
165137
/**
166138
* @test
167139
* @covers ::execute()

0 commit comments

Comments
 (0)