Lumen - Expect Authentication Error In Test - php

When I try to access an end point of the API without a token I get an error, as I should. For my test I want to test I can't access the an end point without a token. The error that appears is:
Invalid JSON was returned from the route. Perhaps an exception was thrown?
How would I test to expect this error?

Related

Symfony BadMethodCallException: Cannot serialize FilesystemAdapter When Using AWS Presigned URLs (DynamoDB Issue)

I'm working in Symfony 6.0 and PHP 8.1, and the following error appeared in my logs when injecting my custom AWSService into a custom object:
Uncaught PHP Exception BadMethodCallException: "Cannot serialize Symfony\Component\Cache\Adapter\FilesystemAdapter" at /app/vendor/symfony/cache/Traits/FilesystemCommonTrait.php line 176
Channel: request
Context: exception:
{
"class": "BadMethodCallException",
"message": "Cannot serialize Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter",
"code": 0,
"file": "/app/vendor/symfony/cache/Traits/FilesystemCommonTrait.php:176",
"trace": [
"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:241",
"/app/vendor/symfony/http-foundation/Session/Session.php:195",
"/app/vendor/symfony/http-kernel/EventListener/AbstractSessionListener.php:132",
"/app/vendor/symfony/event-dispatcher/EventDispatcher.php:270",
"/app/vendor/symfony/event-dispatcher/EventDispatcher.php:230",
"/app/vendor/symfony/event-dispatcher/EventDispatcher.php:59",
"/app/vendor/symfony/http-kernel/HttpKernel.php:185",
"/app/vendor/symfony/http-kernel/HttpKernel.php:173",
"/app/vendor/symfony/http-kernel/HttpKernel.php:74",
"/app/vendor/symfony/http-kernel/Kernel.php:202",
"/app/public/index.php:25"
]
}
I've added other logging throughout the code, and it appears that this happens AFTER my controller exits and returns successfully. I am not clear on how to further debug this issue, since after the controller exits it goes into Symfony-land.
My AWSService handles things like storage to and retrieval from AWS services, such as S3 or DynamoDB. I'm injecting it into an object that keeps the caller's info in the session for easy access, and for the call that throws this error, I'm using it to create a Presigned URL for one of the object's attributes.
The thing that seems strange to me is that the AWSService isn't accessing the filesystem explicitly... it's taking an object and creating a string from it using AWS' $s3->createPresignedRequest() call. Even the AWS docs for createPresignedRequest() imply that the function just spits out an answer based on the settings it was provided:
Important
The URL returned by this method is not validated to ensure that the bucket or key exists, nor does this method ensure that the object allows unauthenticated access.
Further, as stated above, the function call completes with out error, and the Controller that called it also completes without error, before this exception is thrown. So it's hard to believe that the AWSService itself is causing this problem.
Clearly this has something to do with how objects are serialized, but I'm not sure exactly what as this error is pretty vague, and I'm creating a string (not a resource). Any thoughts would be appreciated!
After banging my head against this for a few hours, I finally figured it out... and since I couldn't find anything here at StackOverflow that addresses the issue specifically, here are my notes.
It turns out that Symfony appears to serialize all of the objects in session so that they can be de/re-hydrated after the main processing is complete, and before listeners, post processors, etc. are called.
I got a hint from this StackOverflow post, which told me that resources are not supported in serialized data. But I didn't think that getting a Presigned URL would cause an issue... and as it turned out, that was correct.
I finally created a helper class for the object, and just set the attributes externally instead of internally when the object was constructed. Only at that point did I get a better error:
request.CRITICAL: Uncaught PHP Exception RuntimeException: "Instances of Aws\DynamoDb\DynamoDbClient cannot be serialized" at /app/vendor/aws/aws-sdk-php/src/AwsClient.php line 276
So... because the AWSService included an instance of DynamoDbClient, and because I was putting the AWSService into an object, which was then being stored in the session, Symfony was crashing when the object hit the internal serializer.
I'm not sure why that exception isn't bubbled up better, but... at least now it's documented here. Hope it helps someone else!

Return 500 from Laravel 7 to Google Cloud Task without abort()

In order for Google Cloud Tasks to automatically be re-queued I need my Laravel 7 app to return a 500 error, but everything short of a call to abort() seems to want to return a 200. I know that this ought to work:
return response('No dice son, you gotta work-a late.', 500);
...but no, the task still receives 200 and thus it's deleted from the queue as though it had succeeded.
The reason I'd prefer to avoid abort('Fall down go boom') is that it also raises an exception in Stackdriver, and that's unnecessary since I'm returning this particular error when a third party's API fails to provide data. In the event of errors on my side I'm killing the job outright.
The flow of my code is that I raise a custom exception when the third-party API returns null, then catch that exception and in the handler I call another method that does the work of cleaning up the job, which is when I intended to return 500... though right now I'm calling abort('No worky').
Is there some minutia in the docs that I managed to overlook?
In order to send HTTP 500, this usually would be:
return abort(500, 'Internal Server Error');
And the method usally doesn't matter, as it is just a helper, which returns Response.
Changing the error message does not chenge the fact that it's an intenral server error - and when returning 500, it is probably normal to have it logged as an error.

Unit test exception-messages formatted with sprintf

How can I test if an exception throws the expected error message, when I format my error messages like this:
throw new \Exception(sprintf('Random string: "%s".', 'blablabla'));
Obviously testing with $this->assertEquals() won't work, since I don't know what %s is going to be. Is there a straight forward way to test Exception messages that use sprintf?
I believe that you should know what '%s' is going to be by triggering the part of your code you want an exception in, in a controlled way.
You could trigger the exceptions you are expecting, then you can use assertEquals with the error message you are expecting to be triggered

How do I view the last request from Zend_Soap_Client->getLastRequest() after a soap fault exception?

I am using a Zend_Soap_Client to call a web service. I am getting an exception from the error which I suspect is down to me not sending something along with the request:
Fatal error: Uncaught SoapFault exception: [Sender] SOAP-ERROR:
Encoding: object hasn't 'V24Flag' propert
Because it throws an exception I want to try view what is the actual request being sent, however because of the exception, I can not use Zend_Soap_Client->getLastRequest() to get the request (it returns null). Looking at the Zend_Soap_Client->getLastRequest() its a thin wrapper for soapClient->__soapCall() which I'm not sure how to dig deeper from.
Your problem is actually slightly different to what you think it is. The reason Zend_Soap_Client::getLastRequest() returns NULL for you is that no request was ever made: the error is being thrown at the stage where the request internally is being compared against the WSDL you are using. So the method is perfectly correct to return NULL.
As to how to form the parameter for your SOAP call so this error isn't thrown, I can't be of much greater help right now (I expect it depends partly on the services you are integrating with), but there's a userland answer on the php.net page for SoapClient::__soapCall() which may point you in a fruitful direction. Essentially, some people seem to have got somewhere by doing a deep conversion of an array structure into stdClass objects. That doesn't seem to be the whole story (it isn't for the issue I'm currently investigating), and indeed this may be down to a bug in PHP, but I hope it helps you towards an answer.
How about
try {
$client = new Zend_Soap_CLient();
$client->doSomething($params);
} catch(SoapFault $f) {
echo $f->getTraceAsString();
}
Btw. Zend_Soap_Client->getLastRequest() wraps SoapClient::__getLastRequest();

Web Service Error:

I'm trying to get some values from a WebService, but I'm getting some error messages when accessing from PHP web application or SoapUI.
When I access the service from Visual Studio .NET I get the right values, but I really need to access this from PHP.
Here is the error message:
<faultstring xml:lang="pt-BR">The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'ConsultaFretePedido'. End element 'xmlPedido' from namespace 'http://tempuri.org/' expected. Found element 'Pedido' from namespace ''. Line 6, position 51.</faultstring>
I guess this is the error:
End element 'xmlPedido' from namespace 'http://tempuri.org/' expected.
Found element 'Pedido' from namespace ''. Line 6, position 51.
that means, that you have started with <xmlPedido> but ended with </Pedido>
look at the xml code, that the webservice is giving back, at line 6
I'd recommend using SOAPui to check the WebService.

Categories