How can I extend Laravel Dusk Browser? - laravel

I wish to override \Laravel\Dusk\Browser methods and extend it with my own, plus a few enhancements. Here is what I did so far but it gives me this error:
TypeError: Argument 1 passed to Tests\Browser\SequentialAppTest::Tests\Browser{closure}() must be an instance of Tests\MyBrowser, instance of Laravel\Dusk\Browser given,
called in
/var/www/gtest/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php on
line 67
/var/www/gtest/tests/Browser/SequentialAppTest.php:135
/var/www/gtest/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:67
/var/www/gtest/tests/Browser/SequentialAppTest.php:157
file: tests/MyBrowser.php
<?php
namespace Tests;
class MyBrowser extends \Laravel\Dusk\Browser
{
}
file: tests/Browser/SequentialAppTest.php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Tests\MyBrowser as Browser;
...
class SequentialAppTest extends DuskTestCase
{
....
}

Override newBrowser() in your test or in DuskTestCase:
use Tests\DuskTestCase;
use Tests\MyBrowser as Browser;
class SequentialAppTest extends DuskTestCase
{
protected function newBrowser($driver)
{
return new Browser($driver);
}
}

Related

Laravel Testing/phpunit, Too few arguments passed when using dependency injection

I'm trying to create a simple test with Laravel. My test code is as below;
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Controllers\Abc\AbcController;
class AbcTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
private $abcController;
public function __construct (AbcController $abcController) {
$this->abcController = $abcController;
}
public function testExample()
{
$this->assertTrue(true);
}
However, when i run the test, i'm hitting this error,
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Tests\Feature\abc::__construct(), 0 passed in /var/www/nex/backend/vendor/phpunit/phpunit/src/Framework/TestSuite.php on line 151 and exactly 1 expected in /var/www/nex/backend/tests/Feature/abc.php:28
I've been using this method of performing dependency injections for the rest of my project. I'm not sure why its not working on this particular code.
All help is appreciated.
Thanks!
Check https://laravel.com/docs/5.8/testing you should not use Dependency Injection on controller. Instead you should call the endpoint.
Example
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Controllers\Abc\AbcController;
class AbcTest extends TestCase
{
public function testExample()
{
$response = $this->get('/url');
$response->assertOk();
}
}

How to use namespaces in case of inheritance?

I am new to OOPS thus want to clarify things. I have this code below which works perfectly fine.
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Now I don't intend to use use keyword as it is going to be used once also wanted to experiment and thus, I used the code below.
namespace App\Http\Controllers\Admin;
class AdminController extends App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Now the above mentioned code without use keyword throws a fatal error exception. Why does that happen? In theory,I think I am doing exactly what is supposed to be done then why the exception?
You will have to import it from global namespace , below code will work fine
namespace App\Http\Controllers\Admin;
class AdminController extends \App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Use keyword import class from global namespace, but
class AdminController extends App\Http\Controllers\Controller
it will import parent class relative you current namespace (namespace App\Http\Controllers\Admin) , so translated path will be: App\Http\Controllers\Admin\App\Http\Controllers\Controller which is invalid.

Dependency resolving with App::make not working when bind is done by a provider in Laravel

I'm using Laravel 5.2. I tried to resolve a dependency in Laravel out of the IOCContainer as follows.(with App::make method)
App/FooController.php:-
<?php
namespace App\Http\Controllers;
use App\Bind\FooInterface;
use Illuminate\Support\Facades\App;
class FooController extends Controller
{
public function outOfContainer(){
dd(App::make('\App\bind\FooInterface')); // Focus: program dies here!!
}
}
Bindings for the FooInterface done in the AppServiceProvider as follows
App/Providers/AppServiceProvider.php:-
<?php
namespace App\Providers;
use App\Bind\Foo;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('\App\Bind\FooInterface', function() {
return new Foo();
});
}
}
Structure of the Foo class as follows.
App/Bind/Foo.php:-
<?php
namespace App\Bind;
class Foo implements FooInterface {
}
Structure of the FooInterface interface as follows:-
<?php
namespace App\Bind;
interface FooInterface {
}
Then I created a route as follows.
Route::get('/outofcontainer', 'FooController#outOfContainer');
But when I navigate to this route it throws an exception with the error text:
BindingResolutionException in Container.php line 748:
Target [App\bind\FooInterface] is not instantiable.
What is going wrong with this?
How to use App:make() with the AppServiceProvider?
In your service provider, you're binding the string '\App\Bind\FooInterface'. In your controller, you're attempting to make the string '\App\bind\FooInterface'. These strings are not the same, as they have differing cases (Bind vs bind). Since the strings are not the same, Laravel cannot find the binding in the container.
Correct the casing in your make statement and it should work:
dd(App::make('\App\Bind\FooInterface'));

Laravel IoC doesn't automatically resolving the interface

I've just crossed this example, but it fails to resolve binding interface to implementation.
Having the follow code files:
// File: app/App/Services/Talkable.php
<?php
namespace App\Services;
interface Talkable {
public function talk();
}
// File: app/App/Services/Cat.php
<?php
namespace App\Services;
use App\Services\Talkable;
class Cat implements Talkable
{
public function talk()
{
return 'meow meow';
}
}
// File: app/Jobs/MakeSomeNoise.php
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Services\Talkable;
class MakeSomeNoise extends Job
{
private $talkable;
public function __construct(Talkable $talkable)
{
$this->talkable = $talkable;
}
public function handle()
{
return ($this->talkable->talk());
}
}
The binding are taken place in app/Providers/AppServiceProvider.php
// File: app/Providers/AppServiceProvider.php
...
$this->app->bind('App\\Services\\Talkable', 'App\\Services\\Cat');
The MakeSomeNoise job is dispatched from a Controller
// File: any controller
public function makeNoises()
{
return $this->dispatch(new MakeSomeNoise); // (*)
}
At the (*), I expect Laravel will automatically resolve the binding, but it doesn't. Here the error,
Argument 1 passed to App\Jobs\MakeSomeNoise::__construct() must be an instance of App\Services\Talkable, none given, called in ...
But if I just inject into controller constructor, it works fine.
Any thought on this?
My mistake in the code. The DI should be taken in handle() method, not constructor.
public function handle(Talkable $talkable) {
// blah lbah
}

how to access to laravel global Classes in packages

I am developing a Laravel package . In the packeges I need to File::delete for delete files, but The following error message is displayed :
Class 'Chee\Image\File' not found
Can you help me?
You have to declare it in the top of your class:
namespace Chee\Image;
use File;
class Whatever()
{
}
Instead of using the File Facade, you can also get it directly from the Laravel IoC container:
$app = app();
$app['files']->delete($path)
In a service provider, you can inject it as a dependency your package class:
class Provider extends ServiceProvider {
public function register()
{
$this->app['myclass'] = $this->app->share(function($app)
{
return new MyClass($app['files']);
});
}
}
And receive it in your class:
class MyClass {
private $fileSystem;
public function __construcy($fileSystem)
{
$this->fileSystem = $fileSystem;
}
public function doWhatever($file)
{
$this->fileSystem->delete($file)
}
}
You should be able to just use:
\File
Namespaces are relative to the namespace you declare for the class you are writing. Adding a "\" in front of a call to a class is saying that we want to look for this class in the root namespace which is just "\". The Laravel File class can be accessed this way because it is an alias that has an declared in the root namespace.
Assuming you have file something like that:
<?php
namespace Chee\Image;
class YourClass
{
public function method() {
File::delete('path');
}
}
you should add use directive:
<?php
namespace Chee\Image;
use Illuminate\Support\Facades\File;
class YourClass
{
public function method() {
File::delete('path');
}
}
Otherwise if you don't use PHP is looking for File class in your current namespace so it is looking for Chee\Image\File. You could look at How to use objects from other namespaces and how to import namespaces in PHP if you want

Resources