friends-of-phpspec/phpspec-expect
Adds an expect() helper for PhpSpec, enabling expressive assertions like expect($value)->toBe(true) in your specs. Lightweight, dev-only dependency with compatibility across multiple PhpSpec and PHP versions.
Install the package as a dev dependency:
composer require --dev friends-of-phpspec/phpspec-expect:^4.1
First Use Case: Replace a simple file existence check in a Laravel feature spec:
// Before (legacy)
$fileExists = file_exists('storage/logs/app.log');
expect($fileExists)->toBe(true);
// After (phpspec-expect)
expect(file_exists('storage/logs/app.log'))->toBe(true);
Where to Look First:
phpspec.yml for existing matcher configurationstests/spec directory for existing spec filesphp -v must show ≥8.1)// In a Laravel service spec
expect($this->subject->generateToken())->toBeString()->toHaveLength(60);
// Testing Eloquent model behavior
$user = $this->subject->findOrFail(1);
expect($user->email)->toBe('test@example.com');
expect($user->posts()->count())->toBe(3);
// In a Laravel HTTP spec
$response = $this->get('/api/users');
expect($response->status())->toBe(200);
expect($response->json())->toBeArray()->toHaveCount(2);
// In a spec bootstrap file
$container = new Container();
$container->singleton('expect', function() {
return new \FriendsOfPhpSpec\Expect\Expectation();
});
// Using Laravel factories in specs
$user = User::factory()->create();
expect($user->password)->toBeHashed();
// Testing dispatched jobs
$job = new SendEmailJob($user);
expect($job->shouldQueue())->toBe(true);
// Testing event listeners
Event::fake();
$event = new UserRegistered($user);
$event->dispatch();
expect(Event::assertDispatched(UserRegistered::class))->toBeTrue();
PHP Version Mismatch:
Your PHP version (7.4) is not supported by phpspec-expect 4.1^4.0 in composer.jsonMatcher Conflicts:
Matcher "toBeArray" not foundLazy Evaluation Issues:
// Instead of:
expect($this->subject->value)->toBe('test')
// Use:
$value = $this->subject->value();
expect($value)->toBe('test')
./vendor/bin/phpspec run --verbose
./vendor/bin/phpspec run --filter="testName"
./vendor/bin/phpspec describe --help
Custom Matchers:
src/Matchers/ and reference in phpspec.yml:extensions:
- src/Matchers/CustomMatcher
Laravel Environment:
suites:
laravel:
environment:
variables:
APP_ENV: testing
Database Transactions:
public function let(): void
{
$this->setUpDatabase();
}
protected function setUpDatabase(): void
{
Artisan::call('migrate:fresh');
}
// Create a Laravel-specific expectation helper
class LaravelExpectation extends Expectation
{
public function toBeHttpSuccess(): void
{
$this->toBeBetween(200, 299);
}
}
// Create a trait for common Laravel expectations
trait LaravelExpectations
{
protected function expectResponse($response): LaravelExpectation
{
return new LaravelExpectation($response);
}
}
# phpspec.yml
formatter:
name: phpspec\codeCoverage\Report\CodeCoverage
options:
output: tests/coverage
format: [clover, html]
How can I help you explore Laravel packages today?