mahmoud310
mahmoud310

Reputation: 300

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.

Upvotes: 3

Views: 1311

Answers (3)

Abozanona
Abozanona

Reputation: 2285

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');
        

    }
}

Upvotes: 3

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

You cannot do anything with this. Laravel 5.4 needs PHP 5.6 or later (proof here) so it doesn't have anything in common with browser kit. The last one Laravel that supports PHP 5.4 was version 5.0 (proof here).

So either you don't know what version of PHP you are using or the problem is somewhere else. It shouldn't be possible to install Laravel 5.1+ on PHP 5.4 as you suggested.

Upvotes: 0

Little Phild
Little Phild

Reputation: 805

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 ...
 }

Upvotes: 2

Related Questions