Skip to content

Commit c3eef6c

Browse files
Moved AbstractCallHandler::assertGivenParametersMatchMethodSignature() to Dispatcher.
1 parent 1378a2c commit c3eef6c

7 files changed

Lines changed: 68 additions & 118 deletions

File tree

src/Magic/AbstractCallHandler.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,4 @@ protected function propertyName(
8282

8383
return null;
8484
}
85-
86-
protected function assertGivenParametersMatchMethodSignature(
87-
string $methodName,
88-
array $parameters,
89-
ClassMetadata $classMetadata
90-
)
91-
{
92-
$methodMetadata = $classMetadata->features[VirtualMethods::FEATURES_KEY][$methodName];
93-
$expectedParameterCount = count($methodMetadata->paramters);
94-
95-
$givenParametersCount = count($parameters);
96-
97-
if ($expectedParameterCount !== $givenParametersCount) {
98-
throw new \ArgumentCountError(
99-
sprintf(
100-
'Too %s arguments to function %s::%s(), %d passed and exactly %d expected',
101-
$expectedParameterCount > $givenParametersCount ? 'few' : 'many',
102-
$classMetadata->name,
103-
$methodName,
104-
count($parameters),
105-
$expectedParameterCount
106-
)
107-
);
108-
}
109-
110-
// TODO: The types of the parameters need to be checked
111-
}
11285
}

src/Magic/Dispatcher.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ private static function doInvocation(
9797

9898
// execute
9999
if ($callHandler->canHandle($methodName, $classMetadata, $options)) {
100+
$instance->assertGivenParametersMatchMethodSignature($methodName, $arguments, $classMetadata);
101+
100102
if (! $isStatic) {
101103
$return = $callHandler->execute($objectOrClassName, $methodName, $arguments, $classMetadata);
102104
} else {
103105
$return = $callHandler->executeStatic($objectOrClassName, $methodName, $arguments, $classMetadata);
104106
}
105107

106-
$instance->assertReturnType($objectOrClassName, $methodName, $return, $classMetadata);
108+
$instance->assertCorrectReturnType($objectOrClassName, $methodName, $return, $classMetadata);
107109

108110
return $return;
109111
}
@@ -124,10 +126,35 @@ private function classMetadata(string $className) : ClassMetadata
124126
->classMetadata[$className];
125127
}
126128

129+
private function assertGivenParametersMatchMethodSignature(
130+
string $methodName,
131+
array $parameters,
132+
ClassMetadata $classMetadata
133+
)
134+
{
135+
$methodMetadata = $classMetadata->features[VirtualMethods::FEATURES_KEY][$methodName];
136+
$expectedParameterCount = count($methodMetadata->paramters);
137+
138+
$givenParametersCount = count($parameters);
139+
140+
if ($expectedParameterCount !== $givenParametersCount) {
141+
throw new \ArgumentCountError(
142+
sprintf(
143+
'Too %s arguments to function %s::%s(), %d passed and exactly %d expected',
144+
$expectedParameterCount > $givenParametersCount ? 'few' : 'many',
145+
$classMetadata->name,
146+
$methodName,
147+
count($parameters),
148+
$expectedParameterCount
149+
)
150+
);
151+
}
152+
}
153+
127154
/**
128155
* @param object|string $objectContext
129156
*/
130-
private function assertReturnType(
157+
private function assertCorrectReturnType(
131158
$objectContext,
132159
string $methodName,
133160
$returnValue,

src/Magic/FixtureBuilder.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ public function canHandle(string $methodName, ClassMetadata $classMetadata, arra
5555

5656
public function execute(object $object, string $methodName, array $arguments, ClassMetadata $classMetadata)
5757
{
58-
$this->assertGivenParametersMatchMethodSignature($methodName, $arguments, $classMetadata);
59-
6058
$toBeBuiltClassMetadata = $this->getMetadataOfClassToBeBuilt($classMetadata);
6159

6260
if ('build' === $methodName) {
6361
return $this->executeBuild($object, $toBeBuiltClassMetadata);
64-
} else {
65-
$this->executeWith($object, $methodName, $arguments, $toBeBuiltClassMetadata);
66-
return $object;
6762
}
63+
64+
$this->executeWith($object, $methodName, $arguments, $toBeBuiltClassMetadata);
65+
return $object;
6866
}
6967

7068
private function executeBuild(object $object, ClassMetadata $toBeBuildClassMetadata)

src/Magic/VirtualGetter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function canHandle(string $methodName, ClassMetadata $classMetadata, arra
2727

2828
public function execute(object $object, string $methodName, array $arguments, ClassMetadata $classMetadata)
2929
{
30-
$this->assertGivenParametersMatchMethodSignature($methodName, $arguments, $classMetadata);
31-
3230
$propertyName = $this->propertyName($methodName, 'get', false, $classMetadata);
3331
return Reflection::getPropertyValue($object, $propertyName);
3432
}

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 ::assertGivenParametersMatchMethodSignature()
122-
*/
123-
public function it_returns_void_on_assertGivenParametersMatchMethodSignature_when_number_of_parameters_is_valid()
124-
{
125-
// given a mocked AbstractCallHandler, and ClassMetadata of some object as provided in setUp()
126-
127-
// when asserting the parameters
128-
$result = Reflection::methodOfClass(AbstractCallHandler::class, 'assertGivenParametersMatchMethodSignature')
129-
->invoke(
130-
$this->callHandler,
131-
'withSomeProperty',
132-
[
133-
'some value'
134-
],
135-
$this->classMetadata
136-
);
137-
138-
// then the $result is null
139-
$this->assertNull($result);
140-
}
141-
142-
/**
143-
* @test
144-
* @covers ::assertGivenParametersMatchMethodSignature()
145-
*/
146-
public function it_throws_an_exception_on_assertGivenParametersMatchMethodSignature_when_number_of_parameters_is_invalid()
147-
{
148-
// given a mocked AbstractCallHandler, and ClassMetadata of some object as provided in setUp()
149-
150-
// when asserting the parameters
151-
// then an exception is thrown
152-
$this->expectException(\ArgumentCountError::class);
153-
$this->expectExceptionMessage(
154-
sprintf(
155-
'Too few arguments to function %s::%s(), 0 passed and exactly 1 expected',
156-
ClassForAbstractCallHandlerTesting::class,
157-
'withSomeProperty'
158-
)
159-
);
160-
161-
$result = Reflection::methodOfClass(AbstractCallHandler::class, 'assertGivenParametersMatchMethodSignature')
162-
->invoke(
163-
$this->callHandler,
164-
'withSomeProperty',
165-
[],
166-
$this->classMetadata
167-
);
168-
}
169-
170119
/**
171120
* @test
172121
* @covers ::executeStatic()

tests/PhpUnit/Magic/DispatcherTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ final class DispatcherTest extends TestCase
3131
* @covers ::classMetadata()
3232
* @covers ::invoke()
3333
* @covers ::doInvocation()
34-
* @covers ::assertReturnType()
34+
* @covers ::assertGivenParametersMatchMethodSignature()
35+
* @covers ::assertCorrectReturnType()
3536
*/
3637
public function it_invokes_a_virtual_non_static_method_on_some_object_via_some_call_handler()
3738
{
@@ -91,7 +92,40 @@ public function it_invokes_a_virtual_static_method_on_some_object_via_some_call_
9192

9293
/**
9394
* @test
94-
* @covers ::assertReturnType()
95+
* @covers ::assertGivenParametersMatchMethodSignature()
96+
*/
97+
public function it_throws_an_exception_when_the_number_of_parameters_is_invalid()
98+
{
99+
// given an object, and a list of supported call handlers
100+
$object = new ClassForDispatcherTesting();
101+
$supportedCallHandlers = [
102+
VirtualGetter::class
103+
];
104+
105+
// when invoking an allowed method with the wrong number of parameters
106+
// then an exception is thrown
107+
$this->expectException(\ArgumentCountError::class);
108+
$this->expectExceptionMessage(
109+
sprintf(
110+
'Too many arguments to function %s::%s(), 1 passed and exactly 0 expected',
111+
ClassForDispatcherTesting::class,
112+
'getSomeProperty'
113+
)
114+
);
115+
116+
$result = Dispatcher::invoke(
117+
$object,
118+
'getSomeProperty',
119+
[
120+
'no parameter allowed'
121+
],
122+
$supportedCallHandlers
123+
);
124+
}
125+
126+
/**
127+
* @test
128+
* @covers ::assertCorrectReturnType()
95129
*/
96130
public function it_throws_an_exception_when_the_return_type_is_invalid()
97131
{
@@ -151,4 +185,3 @@ public function it_throws_an_exception_if_no_call_handler_can_handle_the_method(
151185
);
152186
}
153187
}
154-

tests/PhpUnit/Magic/VirtualGetterTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,5 @@ public function it_executes_a_virtual_getter($returnType)
109109
// then the result is the properties value
110110
$this->assertSame(42, $result);
111111
}
112-
113-
/**
114-
* @test
115-
* @covers ::execute()
116-
*/
117-
public function it_throws_an_exception_when_provided_to_many_method_parameters()
118-
{
119-
// given a VirtualGetter call handler, an object, and the object's ClassMetadata
120-
$handler = new VirtualGetter();
121-
$object = new ClassForMagicTesting();
122-
$metadata = $this->getClassMetadata(
123-
'method',
124-
'int getSomeProperty()'
125-
);
126-
127-
// when providing any parameter to the virtual method
128-
// then an exception is thrown
129-
$this->expectException(\ArgumentCountError::class);
130-
$this->expectExceptionMessage(
131-
sprintf(
132-
'Too many arguments to function %s::%s(), 1 passed and exactly 0 expected',
133-
ClassForMagicTesting::class,
134-
'getSomeProperty'
135-
)
136-
);
137-
138-
$handler->execute($object, 'getSomeProperty', ['some param value'], $metadata);
139-
}
140112
}
141113

0 commit comments

Comments
 (0)