Checking Laravel Resource Resources Gives NotFoundHttpException a Second Time

I come across a very strange thing when testing with Laravel 4. This seems like a bug, but probably a logical explanation.

I reproduced the “error” in a clean Laravel installation and here is my code:

My resource controller /app/controllers/TestController.php:

(Created by php artisan controller:make TestController)

class TestController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        return Response::json(array());
    }

    // The end of the file is unchanged

In my app/routes.php:

Route::get('/', function()
{
    return View::make('hello');
});

// Note the composed part of the URL.
// My problem isn't present if I just use `myapi` or `path` as a url
Route::resource('myapi/path', 'TestController');

Posted in /app/test/ExampleTest.php:

public function testTest()
{
    $res = $this->call('GET', 'myapi/path');

    // Until here everything works right
    $this->assertEquals(json_decode($res->getContent()), array());

    // Now I call the same URL a second time
    $res = $this->call('GET', 'myapi/path');

    $this->assertEquals(json_decode($res->getContent()), array());
}

Now I run phpunit and this is what I get:

There was 1 error:

1) ExampleTest::testTest
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: 

/home/me/Web/laraveltest/bootstrap/compiled.php:5531
/home/me/Web/laraveltest/bootstrap/compiled.php:4848
/home/me/Web/laraveltest/bootstrap/compiled.php:4836
/home/me/Web/laraveltest/bootstrap/compiled.php:4828
/home/me/Web/laraveltest/bootstrap/compiled.php:721
/home/me/Web/laraveltest/bootstrap/compiled.php:702
/home/me/Web/laraveltest/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/home/me/Web/laraveltest/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:332
/home/me/Web/laraveltest/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:51
/home/me/Web/laraveltest/app/tests/ExampleTest.php:25

In my other project, I get a slightly different backtrace, but I get the impression that the same problem: (but I don’t know why the other is compiling and the other is not)

2) UnitModelTest::testOther
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: 

/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:148
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1049
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1017
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:996
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
/home/me/Web/my-project/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/home/me/Web/my-project/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:327
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:51
/home/me/Web/my-project/app/tests/UnitModelTest.php:32

In both cases, the line specified in the trace for the test file corresponds to the second test call.

routes.php, URL- , .

, api .

, NotFoundHttpException StackOverflow, . .

? ?

+4
2

, call, , URI . , :

  • MyAPI/
  • MyAPI/MyAPI/

, URL- /, .

public function testTest()
{
    $res = $this->call('GET', '/myapi/path');

    // Until here everything works right
    $this->assertEquals(json_decode($res->getContent()), array());

    // Now I call the same URL a second time
    $res = $this->call('GET', '/myapi/path');

    $this->assertEquals(json_decode($res->getContent()), array());
}


,

$this->refreshApplication();

( client , , )

+4

- , public public_html register \App\Providers\AppServiceProvider.

public function register()
{
    // ...

    $this->app->bind('path.public', function() {
        return base_path('public_html');
    });
}
0

Source: https://habr.com/ru/post/1569143/


All Articles