laravel error Call to undefined method Illuminate\Hashing\BcryptHasher::driver() - laravel

I'm trying to test my laravel application, run phpunit, but get an error:
Error: Call to undefined method Illuminate\Hashing\BcryptHasher::driver()
laravel 5.5, PHPUnit 7.0
My UserTest:
namespace Tests\Feature;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserTest extends TestCase
{
use RefreshDatabase;
/** #test */
function name_should_not_be_too_long()
{
$response = $this->post('/users', [
'name' => str_repeat('a', 51),
'email' => $this->user->email,
'password' => 'secret',
]);
$response->assertStatus(302);
$response->assertSessionHasErrors([
'name' => 'The name may not be greater than 50 characters.'
]);
}
}

In your CreatesApplication class change this line:
Hash::driver('bcrypt')->setRounds(4);
To this:
Hash::setRounds(4);
After that, do a composer dumpautoload
If still no luck, downgrade to phpunit 6.0.

Related

Why users credentials are not working in my login unit testing?

I'm using PHPUnit for testing my Laravel project.
?php
namespace Tests\Feature;
use App\Models\User;
use Tests\TestCase;
class LoginTest extends TestCase
{
public function test_user_can_login():
{
$user = User::factory()->create();
$response = $this->postJson('/api/auth/login', [
'email' => $user->email,
'password' => $user->password,
]);
$response
->assertStatus(200)
->assertHeader('Authorization')
->assertJson([
'success' => 'Usuário logado com sucesso!',
]);
}
}
I want to create a new user for login test flow, but it returns a 400 status response.
Tests\Feature\LoginTest > user can login
Expected status code 200 but received 400.
Failed asserting that 200 is identical to 400.
I have no idea what is going wrong. Any suggestions?

Laravel - How to set faker locale in phpunit tests?

I'm able to change faker locale in my application in config/app.php to pt_BR changing 'faker_locale' => 'pt_BR', and it works fine in my factories but not in my test cases. This is how im importing faker in my Tests:
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Proprietario;
class ProprietarioTest extends TestCase
{
use WithFaker, RefreshDatabase;
public function testStore(){
$attributes = [
'name' => $this->faker->name,
'email' => $this->faker->email,
'address' => $this->faker->city,
'phone' => $this->faker->cellPhoneNumber,
'municipio' => $this->faker->randomDigit,
];
$response = $this->post('/api/v1/proprietario', $attributes);
$response->assertStatus(201);
$createdArea = Proprietario::latest();
$this->assertDatabaseHas('proprietarios', $attributes);
}
The test will fail in $this->faker->cellPhoneNumber because it's not available in default locale. I'm using Laravel 5.8 and PHP 7.2
The WithFaker trait gives you a method you can use
$this->faker('nl_NL')->postcode // dutch postcode
If you want to use it for all tests, overide the setupFaker in your test(s)
protected function setUpFaker()
{
$this->faker = $this->makeFaker('nl_NL');
}

"The system cannot find the path specified. " stdout: "" command: /usr/local/bin/wkhtmltopdf

im using this package
https://github.com/barryvdh/laravel-snappy
to generate pdf files, i follow up the docs for installation but im getting this error
Config/snappy.php
<?php
return array(
'pdf' => array(
'enabled' => true,
'binary' => base_path('vendor/h4cc/wkhtmltopdf-i386/bin/wkhtmltopdf-i386'),
'timeout' => false,
'options' => array(),
'env' => array(),
),
);
PrintCntroller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
use App\Caisse;
class PrintController extends Controller
{
public function testPdf()
{
$caisse = Caisse::where('type',1)->first();
$pdf = PDF::loadView('print.test',[
'date' => date('M-Y'),
'caisse' => $caisse,
]);
return $pdf->download('invoice.pdf');
}
}
error image
a cashe problem :( i solved it with this
php artisan config:cache
php artisan cache:clear

Eloquent 5.5 paginate() not working outside laravel

I am using Illuminate/database package outside Laravel with my CodeIgniter setup. The initialization is done using Capsule class like this
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
class Capsule extends CapsuleManager
{
public function __construct()
{
parent::__construct();
require_once __DIR__.'/../config/database.php';
$db = (object) $db['default'];
$this->addConnection(array(
'driver' => 'mysql',
'host' => $db->hostname,
'database' => $db->database,
'username' => $db->username,
'password' => $db->password,
'charset' => $db->char_set,
'collation' => $db->dbcollat,
'prefix' => $db->dbprefix,
));
$this->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods... (optional)
$this->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$this->bootEloquent();
}
}
I had it running on illuminate/database 5.2. Recently I updated it to illuminate/database 5.5. My Eloquent paginate() has stopped working. The links() method on eloquent collection gives following error.
Call to a member function make() on null
With a stack trace to
return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
in vendor\illuminate\pagination\LengthAwarePaginator.php on Line 90
Any help in this regard will be appreciated.

Getting ReflectionException while using a Package

I am using a Package for roles and permissions management in my laravel 5.1 application and getting error while trying to create roles by using following code.
<?php
use Bican\Roles\Models\Role;
use Illuminate\Database\Seeder;
class RoleTableSeeder extends Seeder
{
public function run()
{
$adminRole = Role::create([
'name' => 'Admin',
'slug' => 'admin',
'description' => '', // optional
'level' => 1, // optional, set to 1 by default
]);
}
}
and I am seeding it using following command on CLI.
php artisan db:seed --class=RoleTableSeeder
but unfortunately I am getting ReflectionException
"[ReflectionException] Class RoleTableSeeder does not exist"...
What might be wrong with it? Looking for your help...
Link to Package: https://github.com/romanbican/roles
Problem solved using the following command on CLI.
composer dump-autoload
Acknowledged: Package Owner

Resources