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

Wakanda's avatar
Level 10

Failed asserting that two strings are equal.

I have a test that can test if an admin can edit products

and when I run it I get the error Failed asserting that two strings are equal. how can I fix this?

Test

public function test_admin_can_edit_products()
    {
        $this->withExceptionHandling();

        $user = User::factory()->create(['admin' => 1]);

        $product = Product::factory()->create();

        $response = $this->actingAs($user)->patch('/admin/product/' . $product->slug, [
            'name' => 'iPhone X',
            'description' => 'This is a new iphone',
            'price' => 220,
        ]);

        $response->assertRedirect(route('product.index'));
    }

my edit controller

public function update(Request $request, Product $product)
    {
        $validated  = $request->validate([
            'name' => 'required|unique:products,name,' . $product->id,
            'slug' => 'required|unique:products,slug,' . $product->id,
            'price' => 'required|integer',
            'description' => 'required',
        ]);


        $product->update($validated);

        $this->calculateDiscount($product);

        return redirect(route('product.index'));
    }

private function calculateDiscount(Product $product)
    {
        if($product->price >= 112 && $product->price <= 115) {
            $percentage = 0.25;
            $price = $product->price;

            $discount = ($percentage / 100) * $price;

            if($product->discount()->exists()){
                $product->discount()->update(['discount' => $discount]);

            } else {

                $product->discount()->create(['discount' => $discount]);
            }
        }

        if($product->price >= 120) {
            $percentage = 0.50;
            $price = $product->price;

            $discount = ($percentage / 100) * $price;

            $product->discount()->create(['discount' => $discount]);
        }
    }
0 likes
13 replies
Wakanda's avatar
Level 10

@laracoft works the same way I changed for testing purposes but still the same error and I think the error is coming from

private function calculateDiscount(Product $product)

Wakanda's avatar
Level 10

@laracoft

<!DOCTYPE html>\n
<html>\n
    <head>\n
        <meta charset="UTF-8" />\n
        <meta http-equiv="refresh" content="0;url='http://ecom.test'" />\n
\n
        <title>Redirecting to http://ecomtest.test</title>\n
    </head>\n
    <body>\n
        Redirecting to <a href="http://ecomtest.test">http://ecom.test</a>.\n
    </body>\n
</html>
laracoft's avatar

@wakanda

Sorry, it should have been

$response->dumpHeaders();
dump(route('product.index'));

This should identify the problem.

Wakanda's avatar
Level 10

@wakanda

array:5 [
  "cache-control" => array:1 [
    0 => "no-cache, private"
  ]
  "date" => array:1 [
    0 => "Sat, 24 Apr 2021 17:49:59 GMT"
  ]
  "location" => array:1 [
    0 => "http://ecomtest.test"
  ]
  "content-type" => array:1 [
    0 => "text/html; charset=UTF-8"
  ]
  "set-cookie" => array:2 [
    0 => "XSRF-TOKEN=eyJpdiI6IkxWWmhjdlpSa3RkZmpJUmtURytZdFE9PSIsInZhbHVlIjoiR0ZYK1BEeGRSbTNhMENrbldlSXU4MnVQSGtCWnpwUW8wSnhoQnZnTm0wRzZnQ01RWDZ2WjlWaUxkRjBOWjVzcVZ2TisrSUdienFBakIxdFZsL3RwZW0yUThCQjgrcTVQWUxHQm5lVFVPZTN2SjhOUDg4Y0JMaEZvdkZrK2FJeFYiLCJtYWMiOiI5MGQxZWExYmMyNjNmMjU1ZTRkMGVjNWEwY2Y3MWRjNjU3MzViNGQzMjQwNDEyYjg0NmNkZWZiMGE0MzJjOTNlIn0%3D; expires=Sat, 24-Apr-2021 19:49:59 GMT; Max-Age=7200; path=/; samesite=lax"
    1 => "laravel_session=eyJpdiI6ImQ5Z0Q1NE9Nd3VwWWNRNFFFMm5QSUE9PSIsInZhbHVlIjoiRm9mdThkRTE4aTFtM25SQkRNOGRJMTcySjBicFZlN1RadTdzd0xIUE5JNjB1Z0Q3NWFVTnhlVWJuME1wQ2JsZnZjMjI1UUMrdXh5aWNnZ3dGZHFiL0RPeURxdXRueWxvRTJHVk1FVm9ENmhMSk9IeUZreEpwa0JsSlZwQ2RHWGoiLCJtYWMiOiIxNDgyYmI3NGYxYzg5YTVmMWZlNGIyMWZmZmE4YTg3MGJhZDNlZjU0N2U5ODlhNmJiMmRlYTJkZjhlYmU1NjA1In0%3D; expires=Sat, 24-Apr-2021 19:49:59 GMT; Max-Age=7200; path=/; httponly; samesite=lax"
  ]
]
laracoft's avatar

@wakanda

Add this to your test class

use \Illuminate\Foundation\Testing\WithoutMiddleware;

laracoft's avatar
laracoft
Best Answer
Level 27

@wakanda

I would put a dd("this is update") after the validation of your update(...) to be sure you are hitting it. You are validating for 'slug', but I don't see it in your test.

Please or to participate in this conversation.