If the code feels confusing, do not start with the test line. Start by finding the files first. This lesson shows where the app code lives, where test files live, and how the test can see the class.
Summary: Learn where classes and test files live, and how they connect.
Here are the most important files to know:
src/
App.php
LearningService.php
LessonRepository.php
bootstrap.php
public/
index.php
tests/
unit/
functional/
http/
regression/
Here is the simplest mental model for how the test finds the class:
// 1. bootstrap.php tells PHP how to find classes
require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
// 2. import the class name from its namespace
use SmallLearning\LearningService;
use SmallLearning\LessonRepository;
// 3. now the test can create the objects
$lessons = new LessonRepository();
$service = new LearningService($lessons);
Why this test works
- App code lives in src/.
- The browser entry file is public/index.php.
- Unit tests live in tests/unit/.
- Functional tests live in tests/functional/.
- HTTP tests live in tests/http/.
- src/bootstrap.php autoloads classes in the SmallLearning namespace.
- That is why tests can write use SmallLearning\LearningService; and then create the object.
- When you get lost, first open the class in src/, then open the matching test file.
Real-life examples
- Example: on a shop website, product price logic may live in src/ProductService.php and its unit test may live in tests/unit/ProductServiceTest.php.
- Example: on a booking website, search API code may live in src/SearchController.php and its HTTP test may live in tests/http/SearchApiTest.php.
Remember this
- Find class in src/
- Find test in tests/
- bootstrap.php loads classes
- use imports class name
- Start with file locations first
Run this learning module with:
Open this lesson in the browser, then go to Testing Basics, then Unit Test