Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

niclm's avatar
Level 9

PHPUnit Test Checkbox On/Off

Does anyone have a working example of a feature test that a user can enable/disable a checkbox ? I have banged my head against my keyboard trying to get it working ... but the logic works perfectly fine in the browser, I just cant get a successful test working :(

 /** @test */
public function an_admin_can_enable_a_customers_account()
{
    $customer = $this->auth(factory(User::class)->create(['enabled' => false]));

    $this->auth(factory(User::class)->create(['role' => 'admin']));
        
    $this->assertFalse($customer->enabled);

    $this->patch('/customer/' . $customer->id, ['enabled' => 'on'])->assertRedirect('/customer/' . $customer->id);

    $this->assertTrue($customer->enabled); //Failed asserting that false is true.
}

Here is the 'enabled' logic in the update method, which I know works in a live browser test.

$request->enabled ? $customer->enable() : $customer->disable();

$customer->update($request->validated());

dd($customer);
#changes: array:1 [
    "enabled" => true
  ]

I have dumped the output every step of the way and right before the redirect it has definitely updated the 'enabled' column in the DB but my test still fails...Grrr!

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

Get a fresh instance of $customer; you are asserting against the instance created by the factory inside the method which is cached with enabled equal to false.

$this->assertTrue($customer->fresh()->enabled);

Please or to participate in this conversation.