β Testing
Cardikit uses Pest as its testing framework for writing elegant, expressive tests.
Tests can be executed using the built-in CLI command:
./cardikit test
π Example Test
Create your tests inside the tests/
directory. Hereβs a simple example:
<?php
test('example', function () {
expect(true)->toBeTrue();
});
π§ͺ Writing Testable Code
To ensure maintainability and readability, your code should be structured with testability in mind:
β Principles
Principle | Description |
---|---|
Separation of Concerns | Avoid mixing logic, validation, and DB access in one method |
Dependency Injection | Inject dependencies so they can be replaced or mocked during tests |
Pure Functions | Write functions that take input and return output without side effects |
Return Values | Avoid direct echo /die in logic, use return instead |
π Integration vs Unit Testing
Type | Focus | Pros | Cons |
---|---|---|---|
Unit Tests | Isolated methods, mocks for dependencies | Fast, targeted feedback | May miss real-world issues |
Integration Tests | Full flow including database or session | Tests actual application behavior | Slower, more setup/cleanup needed |
π Best Practice: Use unit tests for most logic and only write integration tests when needed(e.g., for full request/response flow).
π¦ Test Directory Structure
/tests
βββ Feature/
βββ Unit/
βββ ExampleTest.php
Organize tests by type or feature.