Laravel dusk - classList is null - php

I have got a problem with laravel dusk. I have tests like this (two more similar to this one):
LoginTest.php:
`
public function test_user_can_be_edited(){
$user = User::factory()->create([
'password' => Hash::make('password')
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/users/'.$user->id.'/edit')
->type('first_name', $user->first_name)
->type('last_name', $user->last_name)
->type('city', 'Leżajsk')
->type('postcode', '37-300')
->type('street', 'pogodna')
->type('building_number', '12')
->select('country', 'PL')
->press(__('Save changes'))
->assertPathIs('/users/'.$user->id.'/edit');
});
}`
In DuskTestCase.php in driver() function:
return RemoteWebDriver::create(
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
php artisan dusk with port 9515 throwing this errors:
`DevTools listening on ws://127.0.0.1:50974/devtools/browser/db295343-6eab-4a1d-8f1f-30dc1059819b
[0118/201030.598:INFO:CONSOLE(23707)] "Uncaught TypeError: Cannot read properties of null (reading 'classList')", source: http://localhost:8000/demo1/plugins/global/plugins.bundle.js (23707)
.
DevTools listening on ws://127.0.0.1:51169/devtools/browser/90643afb-e2de-4c1b-8ada-62bb510620ce
[0118/201041.697:INFO:CONSOLE(23707)] "Uncaught TypeError: Cannot read properties of null (reading 'classList')", source: http://localhost:8000/demo1/plugins/global/plugins.bundle.js (23707)
E[0118/201045.317:INFO:CONSOLE(23707)] "Uncaught TypeError: Cannot read properties of null (reading 'classList')", source: http://localhost:8000/demo1/plugins/global/plugins.bundle.js (23707)
and then:
Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"first_name"}
(Session info: headless chrome=109.0.5414.75)
I don't know what is missing. It seems like it something with js but I can't locate it. There is no errors in the browser console, just when I run tests
I tried to change the port but it doesn't help

Related

Using separate data provider class with PHPUnit and attributes

I would like to separate Tests and Data Providers. Using PHP 8 attributes, I cannot get the following test to run when referencing an external Data Provider:
#[Test]
#[DataProviderExternal(RouterDataProvider::class, 'registerGetRouteData')]
public function itRegistersGetRoute(Route $route, array $expectedResult)
{
$this->router->get($route);
$this->assertEquals($expectedResult, $this->router->getRoutes());
}
My data provider class:
class RouterDataProvider
{
public static function registerGetRouteData(): array
{
return [
$route = new Route('/', ['IndexController', 'index']),
[
'GET' => [
'/' => $route,
],
'POST' => []
]
];
}
}
How could I get this test to run with the desired provider method?
By running PHPUnit with the following flags, I was able to see exactly what my issue was:
./vendor/bin/phpunit --display-deprecations --display-warnings --diplay-errors --display-notices
The data set was invalid. Changing the return to yield and updating the return type for the registerGetRouteData method from array to \Generator resolved this.
I was running phpunit with the --testdox flag, so I'm not sure if this is what stopped me seeing any errors initially and assume the test was being skipped.

Predis sub/pub on multiple channels with laravel

**Hi every one **,
I'm using predis with laravel in microservice architecture, and I have the following scenario :
App 1 :
in a controller I published an event in the 'timeslot' channel.
Redis::connection('timeslot')->publish('timeslot/updateStatus', json_encode([
'timeslot' => $this->timeSlot,
'customer' => $this->file->customer_id
]));
App2:
I subscribed to this channel 'timeslot' and I have a process that makes a modification in a model (that works).
I have a listener in this model when updating I launch another redis event to app 1 in other channel (event)
static::updated(function (TimeSlot $model) {
if($model->isDirty('state') && $model->state->value === 'booked'){
$result = json_encode(array_merge($model->toArray(), $model->agency->toArray()));
Redis::connection('event')->publish('timeslot/booked', $result);
}
});
when executing this part I would have the following error:
fclose(): supplied resource is not a valid stream resource
at vendor/predis/predis/src/Connection/StreamConnection.php:247
243▕ */
244▕ public function disconnect()
245▕ {
246▕ if ($this->isConnected()) {
➜ 247▕ fclose($this->getResource());
248▕ parent::disconnect();
249▕ }
250▕ }
251▕
Diagram
Any help please.
Thank you
I tried to do it on same channel but also i got an error

Why my API call works on my local server and not online? (error 500)

I have two different website (two different domains) which use laravel.
One of them (which I'll call here api.com) provides an api with different routes and is actually on a local server (111.111.1.111) :
routes/api.php :
Route::prefix('news')->group(function () {
Route::get('', [ApiController::class, 'newsIndex']);
Route::get('{id}', [ApiController::class, 'newsShow']);
});
ApiController :
public function newsIndex()
{
$news = News::orderByDesc('ordre')
->where('site_destination', 'like', '%other%')
->where('statut', '=', 1)
->get();
return response()->json($news);
}
public function newsShow($id)
{
$news = News::findOrfail($id);
return response()->json($news);
}
I have to call these APIs from my second website (which I will call here request.com).
I want to deploy this one online (planethoster server). I succeed to deploy it but my page where I called API from api.com doesn't work : return 500 error.
Into my controller on request.com :
public function index()
{
$newsListFromApi = json_decode(file_get_contents("http://111.111.1.111/api/news"));
$newsFirstPictureList = [];
foreach ($newsListFromApi as $key => $value) {
$newsFirstPictureList[$value->id] = json_decode(file_get_contents("http://111.111.1.111/api/news/" . $value->id . "/firstPicture"));
}
return View::make('client.news.index', [
'newsListFromApi' => $newsListFromApi,
'newsFirstPictureList' => $newsFirstPictureList,
]);
}
public function show($newsId)
{
$news = json_decode(file_get_contents("http://111.111.111/api/news/" . $newsId));
$newsDocs = json_decode(file_get_contents("http://111.111.1.111/api/news/" . $newsId . "/docs"));
// dump($newsDocs);
return View::make('client.news.show', [
'news' => $news,
'newsDocs' => $newsDocs,
]);
}
If request.com is on localhost (production or development mode) it works fine. I only have warning message in console about mixed contents that I'm aware of. But if I deploy request.com I have a 500 error.
On the logs I have this entry :
*[2022-06-13 13:55:03] production.ERROR: file_get_contents(http://111.111.1.111/api/news): failed to open stream: Connection timed out {"userId":x,"email":"xx","exception":"[object] (ErrorException(code: 0): file_get_contents(http://111.111.1.111/api/news): failed to open stream: Connection timed out at /home/xx/laravel/releases/20220613-120400/app/Http/Controllers/Client/NewsController.php:12)
[stacktrace]
The api.com et request.com api aren't on the same server on development.
Do you think that it can be caused by the mixed contents error? I think that the browser will only not display images but display the page anyway...
Also I think it can be a problem of cross origin but I hadn't such an error message of it.
I suppose, thanks to the log, that it has a problem with this line of code when I call the API :
$newsListFromApi = json_decode(file_get_contents("http://111.111.1.111/api/news"));
I am used to call API on JS with fetch(Ajax) but not with PHP...
Why does it work locally, but not online?
You can connect to your local server from outside of your network only if it has public access (open TCP/IP ports and public IP). http://111.111.1.111 is a local IP address in this context.
You could deploy your API service to another server, and then the two would be able to connect.

Pusher Undefined property: stdClass::$channels in Laravel Lumen

I'm making a POC with Lumen and Vue.JS. For now it just has to send a "hello world" message from the Lumen back-end to the Vue.JS front-end (which works). I have made an event which is triggered upon loading the page like this:
public function sendMessage(Request $request)
{
event(new MessageEvent('hello world'));
}
The MessageEvent looks like this (got this from the Pusher getting started help):
class MessageEvent extends Event implements ShouldBroadcast
{
/**
* Create a new event instance.
*
* #return void
*/
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return ['my-channel'];
}
public function broadcastAs()
{
return 'my-event';
}
}
This part is working, since I'm receiving this in the Vue.JS application:
Pusher : : ["Event recd",{"event":"my-event","channel":"my-channel","data":{"message":"hello world"}}]
Now comes the problem when i check the queue log. triggered with php artisan queue:listen, I see the following:
[2021-03-14 11:57:03][Bh7373O9EETAZc39M2RCSPmUTjwSbSmL] Processing: App\Events\MessageEvent
[2021-03-14 11:57:04][Bh7373O9EETAZc39M2RCSPmUTjwSbSmL] Failed: App\Events\MessageEvent
When I check the Lumen log files it says the following:
[2021-03-14 11:43:12] local.ERROR: Undefined property: stdClass::$channels {"exception":"[object] (ErrorException(code: 0): Undefined property: stdClass::$channels at /var/www/vendor/pusher/pusher-php-server/src/Pusher.php:538)
So I went ahead and checkt the Pusher.php file:
536: $result = json_decode($response['body']);
537:
538: if ($result->channels) {
539: $result->channels = get_object_vars($result->channels);
540: }
I decided to check what $response was, it gives the following:
[2021-03-14 11:57:04] local.INFO: array (
'body' => '{}',
'status' => 200,
)
Of course it can't get to $result->channels if response["body"]["channels"] doesn't exist.
When I go and check the Pusher API reference it says the following:
Which would mean that the body should indeed contain a JSON response. But when I scroll a bit further I see this:
Which should mean you don't have to set the info parameter, since it's optional and experimental.
[EXPERIMENTAL] If the info parameter is sent, then it returns a hash of unique channels that were triggered to. The hash maps from channel name to a hash of attributes for that channel (may be empty).
The response it expects with the info parameter set is this:
{
"channels": {
"presence-foobar": {
"user_count": 42,
"subscription_count": 51
},
"presence-another": {
"user_count": 123,
"subscription_count": 140
},
"another": {
"subscription_count": 13
}
}
}
Which is the channels object asked for.
My question is, did I miss something or is this a bug from Pusher? I'm really breaking a leg on this one so I hope someone can help me out.
Pusher API reference: https://pusher.com/docs/channels/library_auth_reference/rest-api
Fix composer.json
I have created an issue on the PHP package: https://github.com/pusher/pusher-http-php/issues/295
It is true this version is broken, but the fix should be in the composer.json file. Mine looked like this:
{
...
"require": {
...
"pusher/pusher-php-server": "5.0"
...
},
...
}
This means I specifically say that it should use version 5.0. But now I won't get version 5.2 for example, which means I won't get patches. According to the people who answered my issue on Github, I should change my composer.json and add a ^ in front of the version number so it does get the patches for that version. So it should be changed like this:
{
...
"require": {
...
"pusher/pusher-php-server": "^5.0"
...
},
...
}
Don't forget to run composer update afterwards.
According to Graham Campbell on Github:
Anyone who puts 5.0 in their composer.json file has made an error, since composer resolves this to the version 5.0.0.0 and not the version constraint ^5.0.
Fix pusher.php (not recommended)
Another workaround would be to directly edit /vendor/pusher/pusher-php-server/src/Pusher.php. Although not recommended, it does work.
536: $result = json_decode($response['body']);
537:
538: if ($result->channels) {
539: $result->channels = get_object_vars($result->channels);
540: }
This doesn't work since channels doesnt exist in the result object. It should check if the channels object exists first. You can do that by changing the above code to this:
536: $result = json_decode($response['body']);
537:
538: if (property_exists($result, 'channels')) {
539: $result->channels = get_object_vars($result->channels);
540: }

Laravel 5: Mail::to() ends with PHP Fatal error: Maximum function nesting level of '512' reached, aborting

I've a HTTP Request, which is used by the following function:
public function register(Request $request)
{
...
$UserRegistration->sendEmail($request);
...
}
This function calls the function to send an email using the data of the $request.
use Illuminate\Support\Facades\Mail; // included above class line
public function sendEmail($request = NULL)
{
$data = $this->subject('Your Registration')
->view('emails.user.registration')
->text('emails.user.registration_plain')
->attach('/files/some_file.pdf', [
'as' => 'some_file.pdf',
'mime' => 'application/pdf',
])
->attach('files/another_file.pdf', [
'as' => 'another_file.pdf',
'mime' => 'application/pdf',
])
->with([
'email' => $request['email'],
'language' => $request['language'],
'registration_token' => $request['registration_token'],
]);
Mail::to($request['email'])->send($data);
return response()->json(['message' => 'Request completed']);
}
Unfortunately fails the line with Mail:to() with a HTTP 500 error:
2017/01/13 18:29:15 [error] 1484#1484: *102 FastCGI sent in stderr: "PHP message: PHP Fatal error: Maximum function nesting level of '512' reached, aborting! in /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php on line 475
PHP message: PHP Stack trace:
PHP message: PHP 1. {main}() /home/vagrant/user-mgmt/laravel/public/index.php:0
PHP message: PHP 2. Illuminate\Foundation\Http\Kernel->handle() /home/vagrant/user-mgmt/laravel/public/index.php:53
PHP message: PHP 3. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:117
PHP message: PHP 4. Illuminate\Pipeline\Pipeline->then() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:150
PHP message: PHP 5. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
PHP message: PHP 6. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:33
PHP message: PHP 7. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:137
PHP message: PHP 8. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46
PHP message: PHP 9. Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
PHP message: PHP 10. Illuminate\Routing\Router->dispatch() /home/vagrant/user-mgmt/laravel/vendor/laravel/framework/src/Illumi
.
. // Another PHP messages until 'PHP message: PHP 510'
.
I've no clue, what is causing this, because var_dump($request) returns a very small object, which only contains a maximum of 10 POST based inputs. When I comment out this Mail:to() line, the application doesn't throw any error and just works - sure, it doesn't send an email.
Well... I've just read this discussion, but it didn't even fix my problem, when I use a very high value like 2000: https://laracasts.com/discuss/channels/general-discussion/l5-maximum-function-nesting-level-of-100-reached-aborting
How can I solve this issue? Did I something wrong?

Categories