Unit testing assertion commands
Functinal testing assertion commands
UI testing assertion commands
Performance testing assertion commands
Unit testing assertion commands
assertSame() — exact value and type
Syntax: $this->assertSame($expected, $actual);
$this->assertSame(
['confirmed', 'cancelled'],
$transitions['pending']
);
- assertEquals() — equivalent value
Syntax: $this->assertEquals($expected, $actual);
$this->assertEquals(4500, $calculatedTripPrice);
For prices, prefer assertSame() when you also need to enforce the result’s type.
- assertTrue() — condition must be true
Syntax: $this->assertTrue($condition);
$this->assertTrue($booking->canBeCancelled());
- assertFalse() — condition must be false
Syntax: $this->assertFalse($condition);
$this->assertFalse($completedBooking->canBeCancelled());
- assertNull() — value must be null
Syntax: $this->assertNull($actual);
$this->assertNull($booking->cancelled_at);
- assertNotNull() — value must exist
Syntax: $this->assertNotNull($actual);
$this->assertNotNull($confirmedBooking->confirmed_at);
- assertEmpty() — collection or value must be empty
Syntax: $this->assertEmpty($actual);
$this->assertEmpty($transitions['completed']);
- assertNotEmpty() — collection or value must contain something
Syntax: $this->assertNotEmpty($actual);
$this->assertNotEmpty($availableTripDates);
- assertCount() — exact number of items
Syntax: $this->assertCount($expectedCount, $actual);
$this->assertCount(3, $trip->itineraryDays);
- assertContains() — item appears in a collection
Syntax: $this->assertContains($expectedItem, $actualCollection);
$this->assertContains('confirmed', $transitions['pending']);
11.** assertNotContains()** — item is absent from a collection
Syntax: $this->assertNotContains($unexpectedItem, $actualCollection);
$this->assertNotContains('confirmed', $transitions['cancelled']);
- assertArrayHasKey() — array contains a key
Syntax: $this->assertArrayHasKey($key, $array);
$this->assertArrayHasKey('pending', $transitions);
- assertArrayNotHasKey() — array does not contain a key
Syntax: $this->assertArrayNotHasKey($key, $array);
$this->assertArrayNotHasKey('deleted', $transitions);
- assertGreaterThan() — actual value is larger
Syntax: $this->assertGreaterThan($minimum, $actual);
$this->assertGreaterThan(0, $trip->availableSeats());
- assertLessThanOrEqual() — actual value is at most the limit
Syntax: $this->assertLessThanOrEqual($maximum, $actual);
$this->assertLessThanOrEqual($trip->capacity, $requestedGuestCount);
- assertInstanceOf() — object has the expected class
Syntax: $this->assertInstanceOf($expectedClass, $actual);
$this->assertInstanceOf(Booking::class, $booking);
- assertIsArray() — value must be an array
Syntax: $this->assertIsArray($actual);
$this->assertIsArray($trip->getItinerary());
- assertIsInt() — value must be an integer
Syntax: $this->assertIsInt($actual);
$this->assertIsInt($trip->availableSeats());
- assertStringContainsString() — text contains a phrase
Syntax: $this->assertStringContainsString($expectedText, $actualText);
$this->assertStringContainsString(
'Royal Family Luxury Getaway',
$trip->title
);
- assertMatchesRegularExpression() — text matches a pattern
Syntax: $this->assertMatchesRegularExpression($pattern, $actualText);
$this->assertMatchesRegularExpression(
'/^HL-[0-9]{6}$/',
$booking->reference
);
For example, HL-123456 passes that
Functinal testing assertion commands
Successful API response: assertStatus()
Syntax: $response->assertStatus($expectedStatus);
$response = $this->getJson('/api/trips/15');
$response->assertStatus(200);
- JSON contains an exact field: assertJsonPath()
Syntax: $response->assertJsonPath($path, $expectedValue);
$response = $this->getJson('/api/trips/15');
$response->assertJsonPath('data.id', 15);
- Invalid booking is rejected: assertUnprocessable()
Syntax: $response->assertUnprocessable();
$response = $this->postJson('/api/bookings', [
'trip_id' => 15,
'guests' => 0,
]);
$response->assertUnprocessable();
- Validation identifies a field: assertJsonValidationErrors()
Syntax: $response->assertJsonValidationErrors($fields);
$response = $this->postJson('/api/bookings', [
'trip_id' => 15,
'guests' => 0,
]);
$response->assertJsonValidationErrors(['guests']);
- Booking exists in the database: assertDatabaseHas()
Syntax: $this->assertDatabaseHas($table, $matchingColumns);
$this->postJson('/api/bookings', [
'trip_id' => 15,
'guests' => 2,
]);
$this->assertDatabaseHas('bookings', [
'trip_id' => 15,
'guests' => 2,
]);
Use a dedicated test database for this example.
- Invalid booking was not saved: assertDatabaseMissing()
Syntax: $this->assertDatabaseMissing($table, $matchingColumns);
$this->postJson('/api/bookings', [
'trip_id' => 15,
'guests' => 0,
]);
$this->assertDatabaseMissing('bookings', [
'trip_id' => 15,
'guests' => 0,
]);
- Guest is sent to login: assertRedirect()
Syntax: $response->assertRedirect($expectedUrl);
$response = $this->get('/bookings/create?trip_id=15');
$response->assertRedirect('/login');
UI testing assertion commands
hese examples run inside a Dusk browser test, typically in a callback such as:
$this->browse(function (Browser $browser) {
// UI checks go here.
});
- Text appears on a page: assertSee()
Syntax: $browser->assertSee($text);
$browser->visit('/trips/royal-family-getaway')
->assertSee('Royal Family Luxury Getaway');
- Text does not appear: assertDontSee()
Syntax: $browser->assertDontSee($text);
$browser->visit('/trips/royal-family-getaway/reviews')
->assertDontSee('Reviews for Desert Safari');
- Button is visible: assertVisible()
Syntax: $browser->assertVisible($selector);
$browser->visit('/trips/royal-family-getaway')
->assertVisible('@view-reviews');
Here @view-reviews is a Dusk selector you add to the button.
11.** Click leads to the right page: assertPathIs()**
Syntax: $browser->assertPathIs($expectedPath);
$browser->visit('/trips/royal-family-getaway')
->click('@view-reviews')
->assertPathIs('/trips/royal-family-getaway/reviews');
- Form contains the expected value: assertInputValue()
Syntax: $browser->assertInputValue($field, $expectedValue);
$browser->visit('/bookings/create?trip_id=15')
->assertInputValue('guests', '1');
Browser input values are strings, hence '1'.
- Booking button is disabled: assertDisabled()
Syntax: $browser->assertDisabled($selector);
$browser->visit('/bookings/create?trip_id=15')
->assertDisabled('@submit-booking');
For example, this could check an unavailable trip date.
- Validation error appears after a click: assertSee()
Syntax: $browser->assertSee($expectedMessage);
$browser->visit('/bookings/create?trip_id=15')
->type('guests', '0')
->click('@submit-booking')
->assertSee('The guests field must be at least 1.');
Performance testing assertion commands
For these examples, $elapsedMs means a measured duration in milliseconds. Choose the limit from your own environment and requirements; the numbers below are illustrations. Very tight timing assertions can fail when a test server is busy.
- Calculation completes under a limit: assertLessThan()
Syntax: $this->assertLessThan($maximum, $actual);
$start = hrtime(true);
$price = $calculator->totalForGuests(4500, 4);
$elapsedMs = (hrtime(true) - $start) / 1_000_000;
$this->assertSame(18000, $price);
$this->assertLessThan(50, $elapsedMs);
- Search duration stays within a limit: assertLessThanOrEqual()
Syntax: $this->assertLessThanOrEqual($maximum, $actual);
$start = hrtime(true);
$results = $searchService->search('Nainital');
$elapsedMs = (hrtime(true) - $start) / 1_000_000;
$this->assertNotEmpty($results);
$this->assertLessThanOrEqual(200, $elapsedMs);
- Query count stays within a limit: assertLessThanOrEqual()
Syntax: $this->assertLessThanOrEqual($maximumQueries, $actualQueries);
DB::enableQueryLog();
$response = $this->get('/trips/royal-family-getaway');
$queryCount = count(DB::getQueryLog());
$response->assertOk();
$this->assertLessThanOrEqual(20, $queryCount);
DB::disableQueryLog();
Run this against a test database. Query count checks can reveal problems such as repeatedly loading reviews one at a time.
- Results are paginated: assertCount()
Syntax: $this->assertCount($expectedCount, $collection);
$reviews = $reviewService->forTrip(tripId: 15, limit: 10);
$this->assertCount(10, $reviews);
This checks that the service does not return more reviews than requested; it does not measure speed.
- Memory growth stays within a limit: assertLessThan()
Syntax: $this->assertLessThan($maximumBytes, $actualBytes);
$before = memory_get_usage(true);
$reviews = $reviewService->forTrip(tripId: 15, limit: 10);
$growth = memory_get_usage(true) - $before;
$this->assertLessThan(10 * 1024 * 1024, $growth);
PHP allocates memory in chunks, so use a sensible limit and repeat measurements before relying on this check.
- Response has a maximum payload size: assertLessThan()
Syntax: $this->assertLessThan($maximumBytes, $actualBytes);
$response = $this->getJson('/api/trips/15/reviews?limit=10');
$response->assertOk();
$this->assertLessThan(
100 * 1024,
strlen($response->getContent())
);
This checks that the reviews API does not return an unexpectedly large body.
Key distinction: Tests 15–17 measure execution or queries; tests 18–20 check factors that can affect performance. For reliable real traffic performance numbers, use a load test alongside these PHPUnit checks.
Commands to run them
cd /opt/lampp/htdocs/holiday-new/booking
php vendor/bin/phpunit --testdox tests/Feature
php artisan dusk
php vendor/bin/phpunit --testsuite Performance
Top comments (0)