Mocking Guzzle client inside Laravel job for testing - laravel

I have a job class which purpose is to send some user data to HubSpot, and to do that it uses Guzzle client to call the create or update API endpoint since the HS PHP client seems to lack this feature.
The problem is that during job testing I don't want to really call HS API, so I need a way to mock the Guzzle client from the outside of the job, but I can't find none.
I've also thought to add the client as a constructor arg and create the job via the service container to take advantage of Laravel's dependancy injection system, but I've discarded the idea since the job is never called/created in the application via service container and it therefore require to update the entire application where needed.
How can I test the job without really making a call to the HS API?

You can set Http::fake method in your tests. e.g.:
Http::fake([
// Stub a string response for Google endpoints...
'google.com/*' => Http::response('Hello World', 200, $headers),
]);
And I would pair this with my new favorite test helper for Preventing Stray Requests that will throw exeption if you have real HTTP requests called from your tests:
Http::preventStrayRequests();

Related

spring boot - how to stop the backend rest api processing after the frontend is closed

I'm using spring boot to create a restful backend application and the frontend is using vue. When someone sends a rest request to my backend application via my frontend webpage, is it possible to stop the backend processing thread after the webpage or the web browser is closed?
HTTP Request cannot be cancelled. General guideline is, the REST calls should be very short. If in case, your REST calls are long running, recommendation is to break into granular calls.
If that is not an option and if you want to cancel a back-end processing, following option can be tried
For every back-end call, return a job id using which server can uniquely identify and return it to the client
Detect browser close
Expose a new Service to cancel based on the Unique Job Id
Handle logic in Server
This will require considerable amount of change!

Laravel log external call

I'm using a laravel plugin to make calls to an external API.
I have issues with the API so I'd like to log all the calls that happen to the API. I already log every incoming request with a middleware function. How can I log all calls from my server to the API?
Sidenote: The plugin uses guzzle and I know you can add logging there, but then I have to edit the plugin which is no good practice.

Laravel 5.2 Pass Middleware Param to Register Service Container in Service Provider

I seem to have a circular dependency where we have a service provider that registers an object with the service container, and boots a config file and some middleware, but the middleware now needs to be passed a param parsed from requests JWT token to the service container prior to it being instantiated, as well as the guard of the user since the service provider needs to be able to get the authenticated user and there are 5 different types (Applicant, Manager, Admin, etc) so the guard is needed to Auth::guard($guard)->getUser() within the constructor since it defaults to the Applicant if null like Auth::guard()->getUser().
How do you work around something like this? If it helps I'm using tymons/JWTAuth (develop) branch to make use of Laravel's guard API.
Should I refactor and just bind to the service container directly within the middleware and not use a service provider at all so I can use the parameter to instantiate the service container object, and the guard. Seems like the only likely solution, but before I refactor all of this I wanted to ask if there was a better way.
Can I use two service providers? One that adds middleware, and a deferred one that will somehow eventually register an object with the service container??? If so how would you pass the parameter in a config? Thinking this isn't possible based on what I know.
Adding a setter to the object isn't an option to prevent anyone accidentally invoking it anywhere else.
Are you using tymon/jwt-auth? If so, your service provider could set up an event listener that fires when the token is decoded. Something like:
event()->listen('tymon.jwt.valid', function($user){
app()->singleton(YourInterface::class, function() use ($user){
//this will only register once your token is decoded
}
});
You could still listen for an event if you aren't using this package, then bind once that fires. This is basically the same as your option 1, but at least it keeps the code organized where you want it.

Mock OAuth server for testing

I'd like to know if it is possible to simulate the oAuth(1,2) authentication flow. I'd like to test without the need to connect to the provider itself. It should be possible as it is just some communication exchange. I'm not looking for something like this where they still communicate with remote server. I'd like to be completely offline, when testing.
Maybe I can run my own oAuth server. I should be using Google oAuth services so the server should behave same like they do. Does google provide some code for their oAuth server, or is it possible to create some fake server. Note the test should be more integration test. I would like to command the server to return some predefined responses. Switching to live oAuth providers will be just changing the remote URL.
Maybe just some http server is ok, I just need to care about the proper format of communicated messages.
Take a look at Client Side REST Tests section of Spring Reference docs. With this support you can easily fake the server and record desired behaviour into MockRestServiceServer.
Here are some examples I created.
Please see steps below to mock OAuth2 token to be used for faster local development using SOAPUI.
Steps:
Create a REST soapUI project, create a POST resource for URL "http://localhost:9045/oauth/token".
Create a Mock Service for above resource.
Create a Mock response as shown below, you can add your own parameters and values depending on your requirements.
{
"access_token":"MockOauth2TokenForLocaldevelopmentnTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3",
"token_type":"bearer",
"expires_in":35999,
"scope":"read write",
"jti":"4d540b94-1854-45fa-b1d6-c2039d94b681"
}
Start the mock service.
Test using your local REST POST request.
Mock Response:
Mock Oauth2 SOAPUI testing:

Difference between parse.com cloud code and rest api

I am interested in using parse and am a little confused as to the difference between their products.
I am looking to run server code on the platform and I can see that there is a cloud code option as well as a REST api option.
What is the difference between the two? For me it it seems as though they are the same in the sense that they both run server code
Cloud Code allows you to define JavaScript functions that run server-side.
These methods are exposed to the REST interface, so your confusion is valid, but you only put/request information via REST, not perform logic.
So say you defined a Cloud Code method to run before you saved a Parse Object. When you make a PUT request through REST with your object body, that Cloud Code method will run.

Resources