Installing PHPUnit with Composer
- 44Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
How can PHPUnit be installed as a development-only Composer dependency?
Run composer require --dev phpunit/phpunit or confirm Composer’s prompt to install it as a development dependency.
Or swipe the card
44 cards
How can PHPUnit be installed as a development-only Composer dependency?
Run
composer require --dev phpunit/phpunitor confirm Composer’s prompt to install it as a development dependency.Why is PHPUnit generally installed as a development dependency rather than a production dependency?
Unit tests are normally run during development, so PHPUnit does not need to be installed on the production server.
Where does Composer record development-only dependencies?
In the
require-devsection ofcomposer.json.What is the purpose of the
bootstrapsetting in a PHPUnit XML configuration?It specifies a file PHPUnit loads before running tests.
Why can a PHPUnit configuration use
vendor/autoload.phpas its bootstrap file?It loads Composer’s autoloader so project classes and other autoloaded files are available to tests.
What is the purpose of enabling PHPUnit’s
colorsconfiguration option?It makes test results easier to read by displaying outcomes, such as passing and failing tests, in color.
What does a PHPUnit test suite configuration specify?
It gives a test suite a name and identifies the directory containing its tests.
How can a PHPUnit XML configuration specify which production source files to include?
Configure the source directory, such as
src, and include files matching a pattern such as*.php.Which PHPUnit configuration file takes precedence:
phpunit.xmlorphpunit.xml.dist?phpunit.xmltakes precedence; PHPUnit falls back tophpunit.xml.distif the first file is absent.Where does PHPUnit store its generated test cache, and why might that cache be ignored by Git?
It stores the cache in the configured cache directory, such as
.phpunit.cache; the cache can be ignored because it is generated locally.What directory convention is used for unit tests in the described project?
Place them in a plural
testsdirectory, with subdirectories that reflect the code being tested.How is a test namespace commonly organized in relation to the project namespace?
Use the project’s namespace as a base, then add a test namespace segment and the relevant subnamespace.
What naming convention identifies a PHPUnit test class for a
Userclass?Name it
UserTest.What naming convention identifies a test method in the described PHPUnit style?
Use a public method whose name begins with
test, such astestSequentialId.Why might a test method declare a
voidreturn type?Because the method performs assertions and does not return a value.
Why is a PHPUnit test class often declared
final?To prevent other classes from extending it, since test-class inheritance is usually unnecessary.
What effect does
declare(strict_types=1)have in a PHP test file?It makes PHP enforce declared scalar types strictly rather than silently coercing incompatible values.
Which PHPUnit class should a test class extend to use PHPUnit’s test features and assertions?
PHPUnit\Framework\TestCase.What is the role of the
setUpmethod in a PHPUnit test class?It prepares shared state or objects before each test runs.
Why should an overridden PHPUnit
setUpmethod callparent::setUp()?To preserve any setup performed by PHPUnit’s parent test-case implementation.
How can a test class reuse the same kind of object across its test methods while keeping tests isolated?
Declare a property for the object and initialize a fresh instance in
setUpbefore each test.Why should a test import the production class it exercises?
So the test can refer to and instantiate the class using its fully qualified namespace.
What does PHPUnit’s
assertSamecheck?That the expected and actual values are identical in both value and type, equivalent to PHP’s
===comparison.In what order should expected and actual values be passed to
assertSame?Pass the expected value first and the actual value second.
How can a test verify that a setter stores a sequential ID correctly?
Set a known ID on the entity, then use
assertSameto compare that ID with the value returned by the getter.How can a test verify that a user entity stores and returns a UUID?
Set a UUID string on the entity and use
assertSameto compare it with the value returned by the UUID getter.Why does the described Ramsey UUID usage call
toString()on a generated UUID?The UUID generator returns an object;
toString()converts it to the string value expected by the entity.How can PHPUnit run only one test method using a filter?
Use the test class name followed by
::and the test method name in the filter, such asUserTest::testOnSerialize.How can PHPUnit be run when it is installed locally through Composer rather than globally?
Run the project’s
vendor/bin/phpunitexecutable.Which executable can be used to run PHPUnit if it has been installed globally?
Run
phpunitfrom the terminal.What does a serialization test for a user entity verify?
That data passed to the entity’s unserialization method can be retrieved through the corresponding getters.
Which user ID field should a user entity serialization test verify?
Verify the input
idusing the entity’s sequential-ID getter.Which UUID field should a user entity serialization test verify?
Verify the input
user_uuidusing the entity’s UUID getter.Which name fields should a user entity serialization test verify?
Verify
first_namewith the first-name getter andlast_namewith the last-name getter.Which contact fields should a user entity serialization test verify?
Verify
emailwith the email getter andphonewith the phone getter.Which credential field should a user entity serialization test verify?
Verify
passwordusing the entity’s password getter.Which date field should a user entity serialization test verify?
Verify
created_dateusing the entity’s creation-date getter.Why should a test avoid asserting on session-token fields that the entity does not yet retrieve?
Because testing those fields is not meaningful until the entity implements their retrieval.
What is the purpose of
export-ignoreentries in a.gitattributesfile?They exclude specified files or directories from Git-generated archives, such as downloadable package archives.
How do
.gitignoreand.gitattributesexport-ignoreentries differ?.gitignorecontrols which untracked files Git ignores;export-ignorecontrols which tracked paths are omitted from exported archives.Why might a library exclude its
testsdirectory from a Composer-distributed archive?Tests are usually development files and may be unnecessary in the installed package.
Why might a project ignore the
.phpunit.cachedirectory?It is generated test-run data and does not need to be committed as source code.
Why might a project avoid committing a customized
phpunit.xmlfile?It may contain local configuration, while the shared
phpunit.xml.distcan provide the project’s default settings.What is a sensible workflow before committing changes to unit tests?
Run the test suite and confirm that the tests pass before committing.

Ask anything
We've read this whole study set, so questions about it get the best answers.