I am looking at testing some Vue.js components, as part of a Laravel application. So, I have a component that is used in a blade template and makes a GET request during the mounted lifecycle hook. Say this request takes 800 ms. Is it possible to use phpunit in this situation- to check the resulting HTML after said request?
I tried using sleep(1) (yes, probably a horrible idea), to give the request time to finish up (not 100% on this methodology), but the expected text was not available on the page after this brief sleep.
Here is what I am hoping to do:
$this->actingAs($user)
->visit('/teams/' . $team->slug . '/players'); // request is made when this route is hit.
$this->see('There are currently no players for this team.')
->see('There are currently no temporary players for this team.');
The see() calls resulted in error- the HTML was not present. I also checked- through the HTML output by phpunit on error, and all that is available in the HTML is what is inside of the blade template- the Vue component is not rendered at the point in which the see() call is made.
I suppose I am after some advice or direction.
I appreciate that this may not be what you're after or even compatible based on the version of Laravel you're using but have you heard about Laravel Dusk in the latest version (5.4).
https://laravel.com/docs/5.4/dusk
Related
[Clarified]
I'm writing my first Laravel app using Vue components; it is a CRUD. I know how to report significant problems to laravel.log via the Log::error("There is an error") technique but that's only useful while I'm in the PHP code; as far as I can figure out, there's no way to write to laravel.log from within a Vue component. (Correct me if I'm wrong!!)
This raises the question of how I should report an error in a Vue component in a Laravel app. I know about console.log(), Debugger for Chrome, and Devtools and those are fine for development. But what about errors that might reasonably happen in production? Clearly, user errors like bad input on a form needs to be dealt with by notifying the user and letting the user correct their input but some errors are beyond the user's scope. For example, it's not hard to imagine my Vue component failing to access the database because it is down for some reason. Shouldn't that kind of problem be written to a log so that whoever monitors production apps can deal with it?
How would a professional app deal with that kind of situation?
My initial inclination is just to write it to laravel.log if possible but that may be either impossible or be considered a bad approach. I'd be curious to know what experienced Laravel developers do in such situations. Maybe automatically sending a text to a support person would be a better approach. I'm really not sure how this should be handled in a modern professional way.
In any case, whoever is responsible for situations beyond the user's control needs to be told somehow so they can begin the steps that would be necessary to fix the problem. Furthermore, this person needs to be given sufficient details of what happened to be able to solve the problem. I expect that would include things like stacktraces, error codes, etc. I wouldn't want to send all of that as a stream of texts, I'd want it all to be accessible in a log of some kind. Then, you simply notify the support person that there is a problem of such-and-such severity which occurred at such-and-such a time and remind them where to find the details.
My approach may be dated though and newer, better alternatives may exist. Those are what I'm looking for with my question.
I can give a general purpose answer for your question.
React introduced the concept of ErrorBoundary,
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.
Using Error Boundary in Vue
use vue-error-boundary
This simple code of handleError method shows ErrorBoundary receiving a callback function through the on-error prop.
<template>
<ErrorBoundary :on-error="handleError">...</ErrorBoundary>
<template>
<script>
// ...
methods: {
handleError (err, vm, info) {
// do something
}
}
// ...
</script>
read the docs for the npm module to know more.
while handling errors, you can pass the errors to a link to your production site.
eg. /logging so it would be like https://www.example.com/logging, and post the errors in a format eg Date: Error File: Error Message.
You can even use authentication tokens along this link (though no one would use it as it would be frontend errors everyone can see it at console).
Then use routes to log those errors to laravel logs.
fetch started throwing "only absolute urls are supported" whenever my redux app was rendered on the server, but worked perfectly fine client-side. It also used to work for relative URLs so I don't know what went wrong.
Help?
It turns out I was no longer importing fetch from domain-task - somehow I had lost the following line:
import { fetch } from 'domain-task';
Since fetch is also native, the compiler didn't complain, and it worked client-side. This line fixed it for the server-side render.
When I trigger a notification via a controller the MailMessage sends with no issue, all imaged contained.
However when the same notification is triggered via a schedule item that calls a console command the images cannot be found.
You haven't shown any code, but I have been bitten by something similar also so I think I know what you are talking about.
If your APP_URL includes a directory (eg http://localhost/myproject), and you use asset() to reference images in your views, the generated references will work fine when the view is generated from a browser request, but not from a queue, a scheduled job, or from CLI in general. From the CLI, the directory part of your APP_URL is lost in the generated references, and your images will be broken.
This is a known problem in Laravel that comes from Symfony. It looks like it has just been fixed, a month ago: https://github.com/laravel/framework/issues/14139
UPDATE
Here's a concrete example of the issue. I'm using Laravel 5.2 (though according to the issue above it was fixed in 5.4 at the end of August, so appears right up to recent versions) and sending a transactional email when a user does something on the site. I updated my email view to dump out some values.
Browser request, real-time mail, with Mail::send(), and QUEUE_DRIVER=sync:
env('APP_URL') http://localhost/myproject // OK
config('app.url') http://localhost/myproject // OK
asset('/images/foo.png') http://localhost/myproject/images/foo.png // OK
These values are all correct, and the URLs generated by asset() shows my email images correctly.
CLI request, queued mail, with Mail::queue(), and QUEUE_DRIVER=database:
env('APP_URL') http://localhost/myproject // OK
config('app.url') http://localhost/myproject // OK
asset('/images/foo.png') http://localhost/images/foo.png // xxxx
Note myproject/ is missing from the URL generated by asset(). All images references in my email are broken, images don't show up.
Short of upgrading to a version of Laravel where this is fixed, all you can do is not use asset() in any view that will be generated from CLI, and instead manually specify asset paths:
config('app.url') . '/images/' . $product->image
Side note - as #Desh901 pointed out in the comments, env() should never be used outside of config files:
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files.
I created an API in Laravel.
When I first call the API in Postman it shows 504ms, then I call it again and it shows 235ms in Postman. If I call it once again it returns about 220ms to 280ms max?
If I stop making calls to the api for few minutes (about 10-15 min) when I call it again it takes around 500ms for the first time and then it shows near 200.
Is it cached data or persistence call or Zend opCache?
Can anybody help me find out why or what is this?
Laravel supports popular caching backends like Memcached and Redis out of the box. But if you are not using their facades inside your application you shouldnt have any of this issues. Your problem is not the laravel but your server/environment configuration.
I'm trying to implement modular extensions into a codeigniter 2 setup but am having a few problems. I followed the installation instructions on the wiki and everything was working fine. Then I started to play around a bit and try and use it. All I did was create a new module called users with the required folders and added a model class called users_m. I then tried to load this from my welcome module controller. According to the wiki this should be very straightforward. I simply added this line
$this->load->model('users/users_m');
to the constructor of my welcome controller.
Unfortunately at this point I get the white screen of death.
So I tried something else. This time I removed the load model line and added
$this->output->enable_profiler(TRUE);
This time I got the welcome page displayed and I got the profiler, but at the top of the page I got this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$profiler
Filename: MX/Loader.php
Line Number: 145
I don't know if these two tries are related or not, but there's obviously something not right with my setup. Could someone point me in the right direction?
If you accessing the model from the controller in the same module, you can load it using just:
$this->load->model(‘user_m’);
You only have to do $this->load->model(‘module/model_name’); when your cross loading between modules.
Just to make sure, your model is located here right?
application/modules/users/models/users_m.php
As for the profiler error:
1) Have you done installation step 5 and put the Modular Extensions core files into application/core?
2) Do you have the latest version of HMVC? There have been updates to mx/loader.php in the last couple days.
Ps. great tutorial on HMVC: http://net.tutsplus.com/tutorials/php/hvmc-an-introduction-and-application/
if you have folder structure like:
application/
modules/
users/
models/
users_m.php
then use this to call modelsin you controller
$this->load->model('users/users_m','',TRUE);