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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners