5

I'm upgrading an old Laravel personal project from 5.2 to 5.4. The upgrade to 5.3 seems to have gone OK, but now I'm moving to 5.4 I've run into an issue.

The project used the old testing layer so I've installed the BrowserKit testing package to maintain backward compatibility. I also created the following base test case for the Browserkit tests:

<?php

namespace Tests;

use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

abstract class BrowserKitTestCase extends BaseTestCase
{
    use CreatesApplication;

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

The tests for the models, which use the normal test case, work fine, but when I run any of the tests that use the BrowserKit test case, I see the following error message:

PHP Fatal error:  Class 'PHPUnit\Framework\Constraint\Constraint' not found in /home/matthew/Projects/myproject/vendor/laravel/browser-kit-testing/src/Constraints/PageConstraint.php on line 10
PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php:895
Stack trace:
#0 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(735): Illuminate\Container\Container->notInstantiable('Illuminate\\Cont...')
#1 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(608): Illuminate\Container\Container->build('Illuminate\\Cont...')
#2 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(575): Illuminate\Container\Container->resolve('Illuminate\\Cont...')
#3 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(728): Illuminate\Container\Container->make('Illuminate\\Cont...')
#4 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExce in /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 895

Google hasn't been much use with this and the error message is not terribly illuminating. It seems to be something to do with the namespace because the class PHPUnit\Framework\Constraint\Constraint doesn't appear to exist, but I'm not sure how to resolve the issue. I've upgraded the version of PHPUnit to 5.7 as necessary, but that doesn't resolve the issue. Can anyone suggest what the issue might be?

EDIT: Just thought to try downgrading the version to 1.0 and that seems to solve the problem for now, so maybe version 2.0 is intended to work with PHPUnit 6? Still, hopefully this post will help someone out in future.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Matthew Daly
  • 9,212
  • 2
  • 42
  • 83

3 Answers3

6

I ran into this today also.

Instead of downgrading laravel/browser-kit-testing from ~2.0 to ~1.0, I upgraded "phpunit/phpunit" from 5.7 to ~6.0 and that fixed the problem.

I agree that it is related to the PHPUnit namespaces. There is a commit to laravel/browser-kit-testing from May 25 with a title of "Use PHPUnit 6.0 namespaced classes."

mmccaff
  • 1,281
  • 1
  • 11
  • 20
3

Downgrading laravel/browser-kit-testing to 1.0 seemed to resolve the issue, so I'm guessing it's something to do with the namespaces for PHPUnit.

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
  • I prefer this answer as the upgrade guide for Laravel 5.4 specifically mentions to use version 1.* – vonec Feb 12 '18 at 04:40
1

I think you need to instruct your test runner to use the bootstrap autoloader file containing PHPUnit class aliases.

try

phpunit --bootstrap bootstrap/autoload_test.php

If you are using a phpunit.xml configuration file, make sure your phpunit tag contains bootstrap="bootstrap/autoload_test.php" amongst your other set options, like the following:

<phpunit bootstrap="bootstrap/autoload_test.php">

You can instruct your test runner to read your phpunit.xml like so:

phpunit --configuration phpunit.xml

Jose Mujica
  • 678
  • 1
  • 6
  • 9