Here’s a pretty complicated tests file. You can see the sections clearly:
<?php namespace Example; use Exception; // Test $bot = new Bot($terminal); $username = $bot->getUsername(); // Input $terminal = new Terminal(); // Output $terminal->write('Type your username and press Enter:'); $terminal->read(); // return 'john'; $username = 'john'; // Input $terminal = new Terminal(); // Output $terminal->write('Type your username and press Enter:'); $terminal->read(); // return ''; throw new Exception('Invalid username');
This section contains the code that you’re testing. Usually, you’ll call one method here (a unit of code) to test its behavior.
You’ll get code-coverage credit for any lines of source code that are executed while evaluating this section.
This section lists the input values for your test.
Nothing here is real. Any objects that you instantiate here are transformed into mock objects automatically by Lens. That way, your unit tests depend only on the code that you are testing, and not on the implementation details of some dependency.
If you’d like to use a live dependency, you can simply move the dependency up into the “// Tests” section: Your test will no longer be a unit test, but it can still be useful.
Here’s an introduction to Mocks (with working examples).
This section lists any side effects that should happen as a result of running the test. You can list the expected method calls, assign variables, throw an exception, define a constant, or trigger any other side effects that should be produced by the code under test. Lens will verify that exactly those side effects are produced. If there are any discrepancies, Lens will let you know.
If you need it, here is the full syntax of a tests file. (With one exception: you can always use multi-line comments anywhere within the file—exactly as you’d expect.)