A common challenge in testing is not only HOW to test something, but WHAT you can test. All test examples focus on testing concepts and can be applied to all testing frameworks.

it('returns all products as JSON', function () { $product = Product::factory()->create(); $anotherProduct = Product::factory()->create(); $this->post('api/products') ->assertOk() ->assertJson([[ 'title' => $product->title, 'description' => $product->description, ], [ 'title' => $anotherProduct->title, 'description' => $anotherProduct->description, ], ]); });

it('stores a product', function () { $this->actingAs(User::factory()->create()) ->post('product', [ 'title' => 'Product name', 'description' => 'Product description', ])->assertSuccessful(); $this->assertDatabaseCount(Product::class, 1); $this->assertDatabaseHas(Product::class, [ 'title' => 'Product name', 'description' => 'Product description', ]); }); The example ensures that a product is created and stored in the database for our post route.

it('imports product', function() { Http::fake([ 'https://christoph-rumpel.com/import' => Http::response([ 'title' => 'My new product', 'description' => 'This is a description', ]), ]); $user = User::factory()->create(); (new ImportProductAction)->handle($user); $this->assertDatabaseHas(Product::class, [ 'title' => 'My new product', 'description' => 'This is a description', ]); }); Testing HTTP Calls
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners