Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in phpunit laravel 5.4

I need to use unit testing in laravel but they need to download library laravel/browser-kit-testing When i download it tell me they need php 5.6 And i using php 5.4.

 public function testBasicTest()
    {
        $this->visit('/home')
            ->seePageIs('/login');
        $response = $this->call('GET', '/dradmin');
        $this->assertEquals(200, $response->status());
        $this->visit('/login')
            ->type('[email protected]', 'email')
            ->type('123456', 'password')
            ->press('Login')
            ->seePageIs('/home');

    }
}

when use phpunit

Error: Call to undefined method Tests\Feature\UserTest::visit()

/Users/mahmoud310/ecare/tests/Feature/UserTest.php:21

I read in other problems Laravel 5.4 HTTP testing - method seeInElement

use command laravel/browser-kit-testing can solve problem

in my case not work

Using version ^1.0 for laravel/browser-kit-testing
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - The requested package phpunit/phpunit (locked at 5.7.19, required as 4.8.*) is satisfiable by phpunit/phpunit[5.7.19] but these conflict with your requirements or minimum-stability.
  Problem 2
    - Conclusion: don't install phpunit/phpunit 4.8.35
    ...
    - Conclusion: don't install phpunit/phpunit 4.8.1
    - phpunit/phpunit 4.8.0 conflicts with phpunit/phpunit-mock-objects[3.4.3].
    ...
    - phpunit/phpunit 4.8.0 conflicts with phpunit/phpunit-mock-objects[3.4.3].
    - Installation request for phpunit/phpunit 4.8.* -> satisfiable by phpunit/phpunit[4.8.0, 4.8.1, ..., 4.8.9].
    - Installation request for phpunit/phpunit-mock-objects (locked at 3.4.3) -> satisfiable by phpunit/phpunit-mock-objects[3.4.3].


Installation failed, reverting ./composer.json to its original content.
like image 917
mahmoud310 Avatar asked Jul 30 '26 11:07

mahmoud310


2 Answers

Type in the terminal

composer require laravel/browser-kit-testing --dev

Then use the downloaded files in your project

use Tests\CreatesApplication;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

And don't forget to extend from BaseTestCase.

Sample code

<?php

namespace Tests\Feature;

use Tests\CreatesApplication;
use Tests\TestCase;
use App\User;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

class UserTest extends BaseTestCase
{
    use CreatesApplication;
    public $baseUrl = 'http://localhost';
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {


        $this->visit('/home')
            ->seePageIs('/login');
        

    }
}
like image 147
Yazan W Yusuf Avatar answered Aug 01 '26 09:08

Yazan W Yusuf


BrowserKit provides a backwards compatibility layer for Laravel 5.3 style "BrowserKit" testing on Laravel 5.4.

First, install this package:

composer require laravel/browser-kit-testing --dev

Next, modify your application's base TestCase class to extend Laravel\BrowserKitTesting\TestCase instead of Illuminate\Foundation\Testing\TestCase:

And your userTest.php should be something like

<?php

  namespace Tests;

  use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

  abstract class TestCase extends BaseTestCase
  {
      use CreatesApplication;

      public $baseUrl = 'http://localhost';

      //your codes here ...
 }
like image 42
Little Phild Avatar answered Aug 01 '26 09:08

Little Phild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!