Call to a member function connection() on null Laravel 5.4 - laravel

Try write a unit test and i need do sql query
class UpdateThrowsTest extends TestCase
{
protected $bgame;
protected $game_id = 95;
public function setUp(){
$game = new Game();
$game = $game::find($this->game_id);
}
}
and then i write "phpunit" in console and try exception
Call to a member function connection() on null.

If anyone bounce to this error during test with Laravel 6 project.
Try to check if the extends TestCase is using the right TestCase.
It could be due to Laravel 6 make:test generated test using the wrong TestCase.
Change
use PHPUnit\Framework\TestCase;
To
use Tests\TestCase;
The problem should solve.

I had this error on laravel 7 (nothing worked even php artisan serve) and fixed it with
composer dumpautoload
Before that update my vendor with composer update and then everything worked fine.

Related

PHPUnit show errors but not show successful alert

I use Laravel 5.8 and phphunit version 7.5. When I run PHPUnit with error, show me error but when not has error show me only this line
PHPUnit 7.5.0 by Sebastian Bergmann and contributors.
My test class:
use Tests\TestCase;
class leadTest extends TestCase
{
public $Array= ['lead_name' => 'Jon','lead_family'=>'Doe'];
public function test_store()
{
$this->withoutExceptionHandling();
$this->post('leads', $this->Array());
$this->assertDatabaseHas('leads', $this->Array);
}
}
That is the reason of your error:
$this->withoutExceptionHandling();
Try without it.
Look also at class name it should be: LeadTest, and the file should be named LeadTest.php

Laravel PHPUnit Call to a member function connection() on null

I am learning testing on Laravel and I am unable to continue testing Eloquent by this error:
Error: Call to a member function connection() on null
This is my code:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
I am also using the trait use DatabaseTransactions;
The database is already created with 2 records on users table. Do I need to config anything else, thanks!
replace
PHPUnit\Framework\TestCase
with
Tests\TestCase

TestCase Laravel Custom Command returns empty response

I'm trying to test my custom command, but when I run it does not return anything. I do not know if it's a problem with my assertion. I'm using Laravel 5.6
class CommandsTest extends TestCase
{
//Command morty:bloquear_usuarios_demitidos
public function test_if_can_run_command_morty_bloquear_usuarios_demitidos()
{
$response = $this->artisan('morty:bloquear_usuarios_demitidos');
$response->assertContains('Executado');
}
}
phpunit test
I am not sure what you are expecting an artisan command to 'return'. They don't 'return' things, they just get executed. There could be an exit code, but that is it.
If you stop and look at the method you are calling artisan it returns an int. You can't expect anything other than an int which would be the exit code.
Laravel 5.6 API - InteractsWithConsole#artisan

Class 'Eloquent' not found when mocking in Laravel

I'm following through Jeffrey Way's Laravel Testing Decoded and I've hit an issue I can't seem to fix.
I'm actually work through this tutorial: http://net.tutsplus.com/tutorials/php/testing-laravel-controllers/ Which is an excerpt from his book.
Basically I have a test like so:
class PostsTest extends TestCase {
public function __construct()
{
$this->mock = Mockery::mock('Eloquent', 'Post');
}
And that like for mocking Eloquent and Post returns:
PHP Fatal error: Class 'Eloquent' not found
When I run phpunit. Incidentally if I use Jeffrey's Laravel Generators and just generate some scaffold e.g.
php artisan generate:scaffold post --fields="title:string, body:string"
And run phpunit I get same error. He's using the same:
$this->mock = Mockery::mock('Eloquent', 'Post');
To mock the classes. Does anyone have any suggestions on what the issue could be?
I've been working through the tutorial again from scratch and am still getting the same error. I've pushed it to a public repo so people can see: https://github.com/RyanHavoc/tdd-laravel
Just pull it down, run composer install/update and phpunit.
I found a solution to the issue.
//Causes the Class 'Eloquent' not found error
public function __construct()
{
$this->mock = Mockery::mock('Eloquent', 'Post');
}
//Setting the mocks in the setUp() method instead works
public function setUp()
{
parent::setUp();
$this->mock = Mockery::mock('Eloquent', 'Post');
}

Laravel 4 model unit test with Codeception - Class 'Eloquent' not found

I'm trying to use Codeception to run my Laravel 4 unit tests.
Running a test for a simple class with no dependencies works fine. But when I instantiate a model which depends on Eloquent, I get this fatal error:
PHP Fatal error: Class 'Eloquent' not found in /var/www/project/app/models/Role.php on line 4
Unit test:
<?php
use Codeception\Util\Stub;
class TestModel extends \Codeception\TestCase\Test
{
public function testExample()
{
$role = new Role;
$role->name = 'superuser';
$this->assertEquals('superuser', $role->name);
}
}
Model:
<?php
class Role extends Eloquent
{
public function users()
{
return $this->belongsToMany('User');
}
}
Project structure:
I'm running vendor/bin/codecept run unit from the project root, with this file structure:
/project
codeception.yml
/vendor/bin/codecept
/app
/tests
unit.suite.yml
/unit
ExampleTest.php
/models
Role.php
...etc
What am I doing wrong?
By looking at the Codeception L4 sample app, I was able to see how to bootstrap the autoload to resolve this issue, by adding these lines to project/app/tests/_boostrap.php:
include __DIR__.'/../../vendor/autoload.php';
$app = require_once __DIR__.'/../../bootstrap/start.php';
\Codeception\Util\Autoload::registerSuffix('Page', __DIR__.DIRECTORY_SEPARATOR.'_pages');
Edit: when upgrading from Laravel 4.0 to 4.1, it is also necessary to add an extra line:
$app->boot();
I'm probably late to the party, but if you don't need the codecept stuff. You should be extending laravel's implementation of PHPUnit_Framework_TestCase called just TestCase. Like this:
class TestModel extends TestCase {
}
The answer to this question is a little outdated now. With Laravel 5 I got the same error (Class 'Eloquent' not found...) and solved it by copying the code from Laravels base TestCase.php file. This file is used for testing within the Laravel framework (NOT using codeception).
To fix the 'Eloquent not found' error, add the following lines to project/tests/unit/_bootstrap.php
<?php
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
Honestly I'm not sure why it works, but it does! I'll edit if I figure out why or someone comments.
The Eloquent class cannot be found when you are running your unit tests.
Try adding use Illuminate\Database\Eloquent\Model as Eloquent; to Role.php.
You can go to TestCase class and override method refreshApplication (add method to TestCase) with adding auth or some:
protected function refreshApplication()
{
$this->app = $this->createApplication();
$this->client = $this->createClient();
$this->app->setRequestForConsoleEnvironment();
$this->app->boot();
// authenticate your user here, when app is ready
$user = new User(array('username' => 'John', 'password' => 'test'));
$this->be($user);
}
I solved a similar problem with Laravel 4 and Codeception by adding the following lines to _bootstrap.php
$app = require __DIR__.'/../../bootstrap/start.php';
$app->boot();
Hope this helps a fellow googler!

Resources