Testing with Pest
Testing (Pest v4)
Section titled “Testing (Pest v4)”Pyxis CMS focuses on high data integrity and routing security. The entire API business logic is covered by functional feature tests using the Pest v4 framework (built on PHPUnit 12).
Running Tests
Section titled “Running Tests”Tests should be executed within the Docker environment (admin container).
Run all tests:
./vendor/bin/pestRun a specific test file:
./vendor/bin/pest tests/Feature/Api/PageControllerTest.phpWatch mode (TDD):
./vendor/bin/pest --watchTest Configuration
Section titled “Test Configuration”Database: Tests utilize an isolated SQLite in-memory database (:memory:). This ensures tests are extremely fast and do not interfere with your local development data.
Database State: We use the RefreshDatabase trait. This means migrations are run before each test, and the database is wiped immediately after the test completes.
Cache: The application cache (including global settings and localization) is flushed before each test case to prevent data leakage between tests.
Key Testing Areas
Section titled “Key Testing Areas”1. Page Visibility (isLive Logic)
Section titled “1. Page Visibility (isLive Logic)”We verify that the API correctly hides or shows content. A page is only accessible if:
- Status is published.
- Visibility is NOT set to private.
- Publication date (published_at) is in the past or null.
2. Hierarchical Routing
Section titled “2. Hierarchical Routing”Pyxis CMS supports nested pages (e.g., /about-us/team). We test:
- Slug Resolution: Ensuring the system finds the correct page based on the final segment of the URL.
- Path Mismatch: Ensuring that attempting to access a child page while bypassing the parent slug in the URL returns a 404 error.
3. Global Settings
Section titled “3. Global Settings”We verify that the homepage_id stored in the settings table correctly maps the root API request to the specific homepage model.
Best Practices
Section titled “Best Practices”Using Factories
Section titled “Using Factories”Never insert data manually into the database during tests. Always use Factories to ensure proper relationship handling and UUID generation.
Example of creating a draft page: [INSERT CODE: Page::factory()->create([‘status’ => ‘draft’])]
Roles & Permissions
Section titled “Roles & Permissions”UserFactory automatically handles the creation of required roles in the test database to maintain foreign key integrity.
Troubleshooting Common Test Errors
Section titled “Troubleshooting Common Test Errors”No such table: settings
Usually means a middleware (like SetLocale) is trying to fetch data before migrations have finished. Ensure your test uses the RefreshDatabase trait.
Unique constraint failed: roles.slug
Occurs when multiple tests try to create a role with the same slug (e.g., “admin”). Use unique slugs in your RoleFactory.
Integrity constraint violation: users.role_id
Every user in Pyxis CMS must have a role. Ensure your UserFactory always returns a valid role_id.