E
E
EVOSandru62019-12-13 20:03:48
PHPUnit
EVOSandru6, 2019-12-13 20:03:48

What is the reason for Laravel PHPUnit Future test in controller not accepting Entity variable when updating in route?

Good afternoon.
There is a test to update the service:

class ServiceTest extends TestCase
{
    use WithFaker;
    use DatabaseTransactions;

    public function test_category_update_success(): void
    {
        $this->withoutMiddleware();
        $this->signInAdmin();

        $service = factory(Service::class)->create([
            'parent_id' => function () {
                return Service::find(2)->id;
            }
        ]);

        $putArr = [
            'name' => $this->faker->name,
            'parent_id' => Service::find(1)->id
        ];

        $response = $this->put(route('admin.services.update', $service), $putArr);

        throw $response->exception;

        $this->assertDatabaseHas('services', $putArr);

        $response
            ->assertStatus(302)
            ->assertRedirect(route('admin.services.show', $service))
            ->assertSessionHas('success', __('messages.success'));
    }

    protected function signInAdmin()
    {
        $user = factory(User::class, 'admin')->create();
        $this->actingAs($user);
        return $user;
    }
}

There is a specific controller method.
class ServicesController extends Controller
{
 public function update(ServiceRequest $request, Service $service)
    {
        throw new Exception(print_r([
            'Controller',
            '$service->id' => $service->id,
            '$request->all' => $request->all()
        ]));

        $service->name = $request['name'];
        $service->save();

        return redirect()->route('admin.services.show', $service)->with('success', __('messages.success'));
    }
}

Deliberately throwing an exception in ServicesController::update to get the data:
In the test case, I get an empty $service->id :
Array
(
[0] => Controller
[$service->id] =>
[$request->all] = > Array
(
[name] => Ayden Ondricka Jr.
[parent_id] => 1
)
)

If in browser form submission mode, then $service->id gets :

(
[0] => Controller
[$service->id] => 1126
[$request->all] => Array
(
[name] => Ayden Ondricka Jr.
[parent_id] => 1
)
)

Listing of routes:
| | GET|HEAD | admin/services | admin.services.index | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
| | POST | admin/services | admin.services.store | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
| | GET|HEAD | admin/services/create | admin.services.create | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
| | DELETE | admin/services/{service} | admin.services.destroy | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
| | PUT|PATCH | admin/services/{service} | admin.services.update | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
| | GET|HEAD | admin/services/{service} | admin.services.show | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
| | GET|HEAD | admin/services/{service}/edit | admin.services.edit | App\Http\Controllers\Admin\[email protected] | web,auth,can:admin-panel |
I also printed the routes in the body of the test method to make sure that they are formed correctly:
throw new Exception(print_r([
     '$routeUpdate' => route('admin.services.update', $service),
     '$routeShow' => route('admin.services.show', $service)
]));

The output is correct:
[$routeUpdate] => localhost/admin/services/1676
[$routeShow] => localhost/admin/services/1676
Can you please tell me why the test fails?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVOSandru6, 2019-12-18
@EVOSandru6

$this->withoutMiddleware(VerifyCsrfToken::class);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question