-
-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Arch Test fails silently on incorrect interface implementations #1234
Comments
Wouldn't your Laravel application throw an error anyway if an interface wasn't applied correctly? |
Yeah it would. In this case the class was not tested yet and due to an update to an external component, the implementation didn't match anymore. Which would have resulted in a problem in production. The update was not pushed at this point. Tests did not detect it and the Arch Tests failed silently. |
Looking into it. As a side-note, this seems like something that should error our in tests regardless of whether or not you use architecture testing. |
After looking at the code, I believe one way to resolve this is to add a new Potential Implementation /**
* Asserts that the given expectation target implements all of its interfaces correctly.
*/
public function toImplementAllInterfacesCorrectly(): ArchExpectation
{
return Targeted::make(
$this,
function (ObjectDescription $object): bool {
$interfaceNames = $object->reflectionClass->getInterfaceNames();
$className = $object->name;
foreach ($interfaceNames as $interfaceName) {
if (! interface_exists($interfaceName)) {
return false;
}
$interfaceMethods = get_class_methods($interfaceName);
$classMethods = $object->methods->getIterator();
foreach ($interfaceMethods as $method) {
if (! in_array($method, $classMethods)) {
return false;
}
$interfaceMethod = new ReflectionMethod($interfaceName, $method);
$classMethod = new ReflectionMethod($className, $method);
// Check if the number of parameters matches
if ($interfaceMethod->getNumberOfParameters() !== $classMethod->getNumberOfParameters()) {
return false;
}
// Check if parameters are correctly typed
$interfaceParams = $interfaceMethod->getParameters();
$classParams = $classMethod->getParameters();
foreach ($interfaceParams as $index => $interfaceParam) {
if (! isset($classParams[$index])) {
return false;
}
$classParam = $classParams[$index];
if ($interfaceParam->getType() !== $classParam->getType()) {
return false;
}
}
}
}
return true;
},
"to implement all interfaces correctly '",
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
);
} StepsApparently, it seems that testing for these functions is being handled in pest-plugin-arch repository. So what I would need to do then is:
|
Yeah, I would have expected that too, but that doesn't seem to be the case. The Thanks for looking into it. |
What Happened
I tried to run the Pest Arch Laravel Preset on one of my projects and it simply failed silently without outputting anything.
With a lot of try and error I detected, that on of the classes used an interface, which implemented an incorrect method signature, which resulted in the failure. (I already solved the problem with the correcting the implementation, but I would have expected some kind of output to find the issue)
This one class was not covered with a test yet, because the project is WIP.
How to Reproduce
Here an example interface:
And the incorrect class implementation
Sample Repository
https://github.com/Jubeki/pestphp-arch-issue
Pest Version
3.0.4
PHP Version
8.3.11
Operation System
macOS
Notes
Running with the debug flag outputs the following (Nothing more):
The text was updated successfully, but these errors were encountered: