2

I'm trying to write a simple web test to test my silex api using phpunit but I keep getting this error when i try to run it..

1) App\Tests\BlogControllerTest::testInitialPage
TypeError: Argument 1 passed to Symfony\Component\HttpKernel\Client::__construct() must implement interface Symfony\Component\HttpKernel\HttpKernelInterface, null given, called in C:\vendor\silex\silex\src\Silex\WebTestCase.php on line 63

This is my web test

<?php
namespace App\Tests;

use Silex\WebTestCase;

class BlogControllerTest extends WebTestCase
{

    public function createApplication()
    {
        require dirname(dirname(__DIR__)) . '/src/Application.php';
    }
    public function testInitialPage()
    {
        $client  = $this->createClient();
        $crawler = $client->request('GET', '/Blog/1');

        $this->assertTrue($client->getResponse()->isOk());

    }
}

The content of my Composer file:

{
    "require": {
        "silex/silex": "~2.0",
        "symfony/validator": "^4.0",
        "crell/api-problem": "~1.7",
        "tobiassjosten/responsible-service-provider": "^1.0"
    },
    "require-dev": {
        "phpunit/phpunit": "6",
        "symfony/browser-kit": "^4.0",
        "symfony/css-selector": "^4.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "Tests\\": "tests/"
        }
    }
}
2
  • which exactly is line 63? is it the createClient()? Then it is like what the error says, you have to add a parameter to the function that implements HttpKernelInterface (or a mock)
    – Edwin
    Commented Jan 12, 2018 at 11:24
  • 1) App\Tests\BlogControllerTest::testInitialPage TypeError: Argument 1 passed to Symfony\Component\HttpKernel\Client::__construct() must implement interface Symfony\Component\HttpKernel\HttpKernelInterface, null given, called in C:\vendor\silex\silex\src\Silex\WebTestCase.php on line 63 C:\vendor\symfony\http-kernel\Client.php:42 C:\vendor\silex\silex\src\Silex\WebTestCase.php:63 C:\tests\CarType\CarTypeControllerTest.php:15 Commented Jan 12, 2018 at 12:15

1 Answer 1

1

From documentation:

For your WebTestCase, you will have to implement a createApplication method, which returns your application instance:

public function createApplication()
{
    // app.php must return an Application instance
    return require __DIR__.'/path/to/app.php';
}

So here's what you need:

public function createApplication()
{
    return new Application();
}

But in the most cases it would be better to return configured application, from your bootstrap (bootstrap example):

public function createApplication()
{
    require __DIR__.'../web/index-test.php'; // path to your bootstrap file
    return $app;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.