unable to execute integration test for DB seeder - Error : Call to a member function connection() on null - laravel

I am trying to write an integration test for my db seeder, but it does not seem to be returning an instance of that seeder. so it fails to execute
when i debug it the $adminUserSeeder is showing the following
this is my test
namespace Tests\Unit\user;
use AdminUserSeeder;
use App\Test;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\TestCase;
class AdminSeederTest extends TestCase
{
use DatabaseTransactions;
public function setUp()
{
parent::setUp();
}
public function testRun()
{
$adminUserSeeder = new AdminUserSeeder();
$adminUserSeeder->run();
$this->assertDatabaseHas('users', ['name' => 'admin']);
}
}
and this is my seeder class
use Illuminate\Database\Seeder;
class AdminUserSeeder extends Seeder
{
public function run()
{
$user = (new App\User)->where('name','admin')->first();
if($user)
{
$user->delete();
}
factory(App\User::class,'admin')->create();
}
}
added the stack trace
/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1138
/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1104
/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:936
/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:880
/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:843
/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1477
/database/seeds/AdminUserSeeder.php:10
/tests/Unit/AdminSeederTest.php:21

Related

Call to a member function reply() on null laravel botman

This is the code of model. I am unable to change access the function of conversation using the model method as i am passing data from view to botman conversation constructor through request.
<?php
This is the conversation class
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Conversations\TestConversation;
class CheckSaved extends Model
{
public function hasDate($scheduled_date):bool
{
if($scheduled_date)
{
$testconv = new TestConversation($scheduled_date);
$testconv->showOther();
}
return false;
}
}][1]
<?php
namespace App\Conversations;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Conversations\Conversation;
use App\Http\Traits\PropertyDetails;
use App\Models\CheckSaved;
class TestConversation extends Conversation
{
public $is_saved;
public function __construct($request)
{
if(isset($request->scheduled_date))
{
$checkDate = new CheckSaved();
$this->is_saved = $checkDate->hasDate($request->scheduled_date);
}
}
public function sayHi()
{
// dd($request);
$this->say("<input type='datetime-local' id='birthdaytime' name='birthdaytime'>");
$this->say("<button class='btn-date' onclick='test()'>Save</button>");
}
public function showOther()
{
$this->say('Hi');
}
public function run()
{
$this->sayHi();
}
}
This is the image of Model
I am getting reply() on null if try to call a function called "showOther()" inside TestConversation. I am stuck on it please someone help or contact me.

How to automatically create sqlite test db when executing parallel tests?

When executing tests separately I have added this functions to make sure sqlite db would be created if it's not there:
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations;
public function setUp(): void
{
$this->initializeTestDB();
parent::setUp();
Notification::fake();
if ($this->tenancy) {
$this->initializeTenancy();
}
}
public function initializeTestDB(): void
{
$dbname = env('DB_DATABASE');
$filePath= "./" . $dbname;
if (!file_exists($filePath)) {
touch($filePath);
}
}
But this does not help when using php artisan test --parallel. I have tried to move this method into AppServiceProvider as in the documentation. Also didn't helped:
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
ParallelTesting::setUpProcess(function ($token) {
$this->handleTestDB();
});
Any ideas to handle this properly for both parallel and separate tests ?

Laravel :: call method in helper class with constructor

as I mentioned in last question I am beginner in Laravel therefore I need some help ,I have to make helper class to create random Id for some tables ,I create this class with table constructor to recieve deffirent tables :
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\DB;
class RandomId
{
public function __construct($my_table)
{
$this->my_table=$my_table;
}
public function get_id()
{
$id = mt_rand(1000000000, 9999999999);
if($this->check_id($id)){
get_id();
}
return $id;
}
public function check_id($id)
{
$table=DB::table($this->my_table)->where('id',$id)->get();
if (count($course)==0){
return false;
}
return true;
}
}
then I add it as alias in config/app.php
'RandomId' => App\Helpers\RandomId::class,
now I want to call it in controller ,I did somethinf like this but don't work :
<?php
namespace App\Http\Controllers\course;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\RandomId;
class Course_controller extends Controller
{
public function add(Request $request)
{
$id=RandomId::get_id('courses');
dd($id);
}
}
I get this error :Non-static method App\Helpers\RandomId::get_id() should not be called statically
It's because get_id function is not static. You should add static keyword when defining static methods:
public static function get_id()
{
....

How do I do the type-hint 'automatic injection' custom class laravel

Below is the EmailNotifier Class
class EmailNotifier
{
public function notify()
{
echo 'Sending payment notification via email' ;
}
}
Below is my AppServiceProvider
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->make(App\EmailNotifier::class); // resolve the EmailNotifier Class
}
}
Below is billing class
class Billing
{
protected $notifier;
public function __construct(EmailNotifier $notifier)
{
$this->notifier = $notifier;
}
public function pay()
{
// Process the bill payment
$this->notifier->notify();
}
}
and in my controller I did
$data = new Billing(1);
As you can see I already resolve the EmailNotifier Class at the AppServiceProvider Class but when I call that like the code above, it throws an error said 'must be an instance of EmailNotifier'
and based on the laravel documentation, it's stated that :
you may "type-hint" the dependency in the constructor of a class that
is resolved by the container (for the automatic injection)
how do I achieve automatic injection for the type-hint in laravel ?
Use $data = resolve(Billing::class); instead of $data = new Billing(1); and you can remove $this->app->make(App\EmailNotifier::class); // resolve the EmailNotifier Class from service provider's register method.

How can call artisan commands before Test:suite

Is there a way to call artisan commands before my entire test suite? Installing Cartalyst extensions takes time so I do not want to do it before every test but I am presuming i cannot call facades from a static method like setUpBeforeClass, I receive this error
RuntimeException: A facade root has not been set. in /Users/lance/Desktop/FastLMS/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;
class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
Artisan::call('migrate');
Artisan::call('extension:install');
Artisan::call('extension:enable');
}
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
Hash::setRounds(4);
return $app;
}
public function setUp()
{
parent::setUp();
$this->app['Illuminate\Contracts\Http\Kernel']->disableMiddleware();
}
}

Resources