Installing PHPUnit with Composer
- 41Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which Composer command installs PHPUnit as a project dependency?
Which Composer command installs PHPUnit as a project dependency?
41 questions
- 1
Which Composer command installs PHPUnit as a project dependency?
- A
composer install phpunit/phpunit - B
composer create-project phpunit/phpunit - C
composer update --autoload phpunit/phpunit - D
composer require phpunit/phpunit
Correct answer
Dcomposer require phpunit/phpunitExplanation
The command
composer require phpunit/phpunitadds PHPUnit to the project. During installation, Composer can prompt whether to add it as a development dependency. - A
- 2
What PHPUnit version does the setup describe installing?
- A
Version 8.2.3
- B
Version 11.2.3
- C
Version 10.2.3
- D
Version 9.2.3
Correct answer
CVersion 10.2.3
Explanation
The setup identifies version 10.2.3 as the latest version being installed at that point.
- A
- 3
Why should PHPUnit be installed as a development dependency rather than a production dependency?
- A
PHPUnit can only run when the application has no production dependencies.
- B
Tests are generally run during development, so production servers do not need PHPUnit.
- C
Production dependencies cannot include packages written in PHP.
- D
Development dependencies are automatically included in every production installation.
Correct answer
BTests are generally run during development, so production servers do not need PHPUnit.
Explanation
The test runner is needed for local development and testing, but it is not needed on the production server.
- A
- 4
Which Composer option could be used to mark PHPUnit as a development dependency directly in the command?
- A
--global - B
--autoload - C
--production - D
--dev
Correct answer
D--devExplanation
The setup notes that the development-dependency choice could be made with the
--devflag during installation. - A
- 5
What does the
export-ignoreattribute in a.gitattributesfile do in this project?- A
Prevents Composer from installing the specified files as dependencies.
- B
Prevents specified files from being committed to the repository.
- C
Removes specified files from the developer’s local working directory.
- D
Excludes specified files from exported package archives.
Correct answer
DExcludes specified files from exported package archives.
Explanation
The
export-ignoreattribute excludes listed files or directories from archives, such as package downloads, without being the same as ignoring them in Git. - A
- 6
Which statement best distinguishes
.gitignorefrom.gitattributesentries usingexport-ignore?- A
.gitignorecontrols Git’s treatment of untracked files, whileexport-ignoreexcludes files from exported archives. - B
.gitignoreexcludes files from package archives, whileexport-ignoreprevents files from being committed. - C
Both files control which dependencies Composer installs, but for different environments.
- D
Both files perform the same task, but
.gitattributesapplies only to PHP files.
Correct answer
A.gitignorecontrols Git’s treatment of untracked files, whileexport-ignoreexcludes files from exported archives.Explanation
The setup uses
.gitattributeswithexport-ignoreto keep files out of exported package archives, while.gitignoreis used for files such as generated PHPUnit cache data. - A
- 7
Which set of files or directories is suitable for exclusion from a distributed package archive in the described project?
- A
The tests directory, PHPUnit configuration, and development-only project files
- B
The source directory, Composer autoloader, and runtime application entry point
- C
The database schema, application source, and all package metadata
- D
The production dependencies, application classes, and Composer manifest
Correct answer
AThe tests directory, PHPUnit configuration, and development-only project files
Explanation
The tests directory and PHPUnit configuration are development-oriented files that may be unnecessary in a distributed package archive.
- A
- 8
What should PHPUnit’s
bootstrapsetting load in this setup?- A
The project’s
.gitattributesfile - B
The Composer-generated
vendor/autoload.phpfile - C
The application’s production database dump
- D
A PHPUnit bootstrap file containing every test method
Correct answer
BThe Composer-generated
vendor/autoload.phpfileExplanation
The configuration uses Composer’s
vendor/autoload.phpso project classes and other autoloaded files are available to tests. - A
- 9
Why does the setup point PHPUnit directly to
vendor/autoload.phpinstead of a test bootstrap file?- A
A bootstrap file is used only when tests do not extend
TestCase. - B
PHPUnit cannot load bootstrap files located inside a test directory.
- C
The test directory itself automatically defines every class without autoloading.
- D
No separate test bootstrap file is needed for the setup; Composer’s autoloader provides the required loading.
Correct answer
DNo separate test bootstrap file is needed for the setup; Composer’s autoloader provides the required loading.
Explanation
A test bootstrap file could initialize additional rules or constants, but the described tests only need Composer’s autoloader.
- A
- 10
What is the purpose of PHPUnit’s
colorsconfiguration property?- A
To display test results with colors that help distinguish outcomes.
- B
To select which source files PHPUnit includes in the test suite.
- C
To generate color-coded XML configuration files.
- D
To assign different namespaces to test classes.
Correct answer
ATo display test results with colors that help distinguish outcomes.
Explanation
Enabling colors makes test output easier to read by visually distinguishing results such as passing and failing tests.
- A
- 11
What is the role of the PHPUnit cache directory setting?
- A
It determines which directory contains the test suite’s namespace.
- B
It specifies where Composer stores the application’s source classes.
- C
It specifies where PHPUnit stores its cache data.
- D
It identifies the directory where PHPUnit writes production logs.
Correct answer
CIt specifies where PHPUnit stores its cache data.
Explanation
The configuration names a PHPUnit cache directory, such as
.phpunit.cache, for PHPUnit’s generated cache data. - A
- 12
How is the test suite organized in the described PHPUnit configuration?
- A
A suite named
sourcepoints to thevendordirectory. - B
A suite named
cachepoints to the project root. - C
A suite named
unitpoints to thetestsdirectory. - D
A suite named
testspoints to thesrcdirectory.
Correct answer
CA suite named
unitpoints to thetestsdirectory.Explanation
The configuration defines a unit-test suite and assigns the
testsdirectory as its test location. - A
- 13
Which source directory and file pattern are specified for the project’s source files?
- A
The project root and files matching
*.json - B
The
srcdirectory and files matching*.php - C
The
vendordirectory and files matching*.test - D
The
testsdirectory and files matching*.xml
Correct answer
BThe
srcdirectory and files matching*.phpExplanation
The configuration identifies the project source as
srcand includes files with the PHP extension. - A
- 14
Why might the PHPUnit configuration be kept as
phpunit.xml.distrather than only asphpunit.xml?- A
The
.distextension makes PHPUnit run tests without a test suite. - B
The
.distextension prevents the configuration from being read by PHPUnit. - C
The distribution file can provide a shared default while allowing a local
phpunit.xmlcustomization. - D
PHPUnit requires every project to store its configuration in two separate files.
Correct answer
CThe distribution file can provide a shared default while allowing a local
phpunit.xmlcustomization.Explanation
The setup uses the distribution configuration as a default and notes that PHPUnit uses
phpunit.xmlfirst if it exists, falling back tophpunit.xml.distotherwise. - A
- 15
If both
phpunit.xmlandphpunit.xml.distare present, which configuration does PHPUnit use first?- A
Both files are always merged in alphabetical order
- B
Whichever file was most recently modified
- C
phpunit.xml - D
phpunit.xml.dist
Correct answer
Cphpunit.xmlExplanation
The described behavior is that PHPUnit checks
phpunit.xmlfirst and uses the.distfile as a fallback when the first file is absent. - A
- 16
Why should the PHPUnit cache directory generally be added to
.gitignore?- A
It defines the namespace for every test class.
- B
It is the standard location for production dependencies.
- C
It contains the source code PHPUnit needs to run tests.
- D
It is generated cache data and is not needed as a committed project file.
Correct answer
DIt is generated cache data and is not needed as a committed project file.
Explanation
PHPUnit generates its cache directory automatically, so the generated data does not need to be committed.
- A
- 17
What naming convention does the setup use for a test class that tests the
Userentity?- A
A class named
User - B
A class named
UserTest - C
A class named
TestUserEntity - D
A class named
EntityTestUser
Correct answer
BA class named
UserTestExplanation
The test class name uses the class under test followed by the
Testsuffix:UserTest. - A
- 18
Why is the
UserTestclass declaredfinalin the described testing style?- A
A final class allows its test methods to return values instead of
void. - B
Test classes are generally not intended to be extended through inheritance.
- C
Declaring a class
finalmakes its assertions compare values loosely. - D
PHPUnit requires every test class to be declared
final.
Correct answer
BTest classes are generally not intended to be extended through inheritance.
Explanation
The setup presents
finalas a good practice because inheritance is not common for an individual test class. - A
- 19
What effect does
declare(strict_types=1)have on typed arguments in the test file?- A
It makes all class properties public regardless of their declarations.
- B
It prevents PHP from silently coercing an incompatible value to a declared type.
- C
It automatically converts every argument to the type declared by its function.
- D
It requires every test method to have at least one argument.
Correct answer
BIt prevents PHP from silently coercing an incompatible value to a declared type.
Explanation
Strict types make PHP enforce declared types rather than silently converting an incompatible value in the situations described.
- A
- 20
Which class should a PHPUnit test class extend to use PHPUnit’s test-case features?
- A
PHPUnit\Framework\TestSuite - B
PHPUnit\Framework\TestCase - C
PHPUnit\Framework\Assertion - D
Composer\Autoload\ClassLoader
Correct answer
BPHPUnit\Framework\TestCaseExplanation
The test class extends PHPUnit’s
TestCaseclass to use features such as setup hooks and assertions. - A
- 21
How should a test class override PHPUnit’s setup hook?
- A
Define a static
testSetupmethod and callTestCase::run(). - B
Define a public
constructmethod and callparent::construct(). - C
Define a protected
setUpmethod and callparent::setUp(). - D
Define a private
setUpmethod and omit the parent call.
Correct answer
CDefine a protected
setUpmethod and callparent::setUp().Explanation
The described pattern overrides
setUpas a protected method and calls the parent setup implementation. - A
- 22
What is the main purpose of creating the
Userentity insetUp?- A
To run every test method immediately when the class is loaded.
- B
To replace PHPUnit’s assertion methods with entity getters.
- C
To prepare a fresh entity instance for the test methods to use.
- D
To save the entity to the production database before each test.
Correct answer
CTo prepare a fresh entity instance for the test methods to use.
Explanation
The setup method initializes the entity instance so tests can use it as their test subject.
- A
- 23
Which declaration best matches the described test method convention?
- A
A protected static method whose name begins with
runand has no return type - B
A private method whose name begins with
assertand whose return type isUser - C
A public method whose name begins with
testand whose return type isvoid - D
A public method whose name begins with
setand whose return type isbool
Correct answer
CA public method whose name begins with
testand whose return type isvoidExplanation
The examples use public test methods beginning with
testand specifyvoidas the return type. - A
- 24
How should the test namespace relate to the project’s namespace in the described convention?
- A
It should use the Composer vendor directory as its first namespace segment.
- B
It should use the project namespace followed by a
Testssegment and the relevant subnamespace. - C
It should replace the project namespace entirely with the test class name.
- D
It should omit namespaces because test classes cannot be namespaced.
Correct answer
BIt should use the project namespace followed by a
Testssegment and the relevant subnamespace.Explanation
The setup follows the convention of starting with the main project namespace, then adding
Testsand the relevant area, such asEntity. - A
- 25
What does the sequential ID test verify?
- A
That the entity rejects every integer assigned to its ID setter.
- B
That an ID assigned through the setter is returned by the corresponding getter.
- C
That the sequential ID is generated by the UUID library.
- D
That PHPUnit automatically assigns a database ID to every entity.
Correct answer
BThat an ID assigned through the setter is returned by the corresponding getter.
Explanation
The test assigns a value, such as 55, as the sequential ID and checks that the getter returns that value.
- A
- 26
Why does the setup prefer
assertSamefor checking the sequential ID?- A
It converts both values to strings before comparing them.
- B
It verifies that the ID was written to a database table.
- C
It checks only that both values are truthy.
- D
It checks strict equality, including whether the values have the same type.
Correct answer
DIt checks strict equality, including whether the values have the same type.
Explanation
assertSameuses strict comparison, so it checks both the value and its type rather than allowing type coercion. - A
- 27
How does the described test create a UUID value using the Ramsey UUID library?
- A
Generate a version 4 UUID and convert it to a string.
- B
Use the entity getter to create a UUID without calling the library.
- C
Generate a version 4 UUID and store the object without conversion.
- D
Read a sequential integer ID and convert it to a UUID object.
Correct answer
AGenerate a version 4 UUID and convert it to a string.
Explanation
The example creates a UUID using the library’s version 4 method and calls
toString()to obtain a string value. - A
- 28
What relationship does the UUID setter-and-getter test check?
- A
The entity’s UUID getter generates a different UUID on every call.
- B
The UUID string passed to the setter is returned by the entity’s UUID getter.
- C
The UUID setter converts the UUID string into a sequential integer ID.
- D
The UUID getter returns the Ramsey UUID object rather than the stored string.
Correct answer
BThe UUID string passed to the setter is returned by the entity’s UUID getter.
Explanation
The test assigns the string representation of a UUID and asserts that the entity returns the same value through its getter.
- A
- 29
When testing the entity’s data-population or unserialization behavior, what is passed into it?
- A
An associative array containing the user’s data fields
- B
A database connection containing only the table name
- C
A Composer configuration file containing dependency versions
- D
A PHPUnit test result object containing assertion counts
Correct answer
AAn associative array containing the user’s data fields
Explanation
The example supplies an associative array of user data to the entity’s data-population behavior and then checks the resulting getters.
- A
- 30
What should the first-name assertion in the data-population test verify?
- A
The entity’s first-name getter returns the user’s email address.
- B
The input array’s first-name field is replaced with the user’s UUID.
- C
The entity’s first-name getter returns the first-name value from the input array.
- D
The entity’s first-name getter returns a value generated by PHPUnit.
Correct answer
CThe entity’s first-name getter returns the first-name value from the input array.
Explanation
The test checks that data such as
Pierresupplied under the first-name field can be retrieved through the entity’s first-name getter. - A
- 31
What should the last-name assertion in the data-population test verify?
- A
The entity’s last-name getter returns the value supplied for the phone field.
- B
The entity’s last-name getter creates a new value from the sequential ID.
- C
The entity’s last-name getter returns the last-name value supplied in the array.
- D
The array’s last-name field is converted into the entity’s email field.
Correct answer
CThe entity’s last-name getter returns the last-name value supplied in the array.
Explanation
The test compares the array’s last-name value with the value returned by the entity’s last-name getter.
- A
- 32
What does the email assertion check after user data is populated?
- A
That the email value is used as the entity’s sequential ID.
- B
That the email getter generates a valid email address automatically.
- C
That the email getter returns the email value supplied in the array.
- D
That the input array’s email field is omitted from the entity.
Correct answer
CThat the email getter returns the email value supplied in the array.
Explanation
The test compares the supplied email value with the value returned by the entity’s email getter.
- A
- 33
What does the phone assertion check after user data is populated?
- A
That the phone getter returns the phone value supplied in the array.
- B
That the phone getter formats the value as a UUID.
- C
That the input phone value is stored as a PHPUnit assertion count.
- D
That the phone value replaces the entity’s password.
Correct answer
AThat the phone getter returns the phone value supplied in the array.
Explanation
The test supplies a dummy phone value and verifies it through the phone getter.
- A
- 34
What does the password assertion check in the example data-population test?
- A
That the password getter returns the password value supplied in the array.
- B
That PHPUnit automatically removes the password from the entity.
- C
That the password getter returns the user’s creation date.
- D
That the entity hashes the password using a behavior demonstrated by the test.
Correct answer
AThat the password getter returns the password value supplied in the array.
Explanation
The example places a dummy password value in the input array and checks the entity’s password getter against it; it does not demonstrate password hashing.
- A
- 35
How is the creation-date value for the data-population test chosen in the example?
- A
It generates a new UUID and stores it as the creation date.
- B
It uses the current time in any format accepted by the operating system.
- C
It uses the PHPUnit cache directory as a date string.
- D
It uses a value matching the date format stored for users in the database.
Correct answer
DIt uses a value matching the date format stored for users in the database.
Explanation
The example reuses a creation-date value from the database so the test data matches the format used for stored user records.
- A
- 36
Why does the setup avoid testing the session-token and last-session-time fields at this stage?
- A
PHPUnit cannot assert values that come from database columns.
- B
The fields are already covered automatically by the UUID test.
- C
Those fields have not yet been implemented in the user entity.
- D
Those fields must always be tested only on a production server.
Correct answer
CThose fields have not yet been implemented in the user entity.
Explanation
The example notes that these database columns are not yet retrieved by the user entity, so testing them would not test implemented behavior.
- A
- 37
How can PHPUnit be run when it is installed globally and available in the terminal?
- A
Run
phpunit.xml.distas a shell command. - B
Run
phpunitfrom the terminal. - C
Run
vendor/autoload.phpas a shell command. - D
Run
composer export-ignorefrom the terminal.
Correct answer
BRun
phpunitfrom the terminal.Explanation
If PHPUnit is installed globally, the
phpunitcommand can be invoked directly from the terminal. - A
- 38
How can PHPUnit be run if it is installed locally in the project but not globally?
- A
Run
src/vendor/autoload.php. - B
Run
vendor/bin/phpunit. - C
Run
tests/bin/phpunit. - D
Run
composer/phpunit.xml.
Correct answer
BRun
vendor/bin/phpunit.Explanation
A local Composer installation places the executable in
vendor/bin/phpunit. - A
- 39
How can a developer run only one named test method instead of the full suite?
- A
Rename the test directory to match the method name.
- B
Add the method name to the Composer autoload section.
- C
Use PHPUnit’s filter option with the test class and method name.
- D
Delete the other test methods before running PHPUnit.
Correct answer
CUse PHPUnit’s filter option with the test class and method name.
Explanation
The setup demonstrates using PHPUnit’s filter option to select a specific test, with the class and method identifying the target.
- A
- 40
What does a failing
assertSameresult indicate when the expected first name is changed but the entity still returns the original name?- A
The actual getter value did not strictly match the expected value.
- B
The test passed because the entity returned a nonempty string.
- C
The failure proves that the PHPUnit configuration did not load.
- D
The assertion changed the entity’s stored first name automatically.
Correct answer
AThe actual getter value did not strictly match the expected value.
Explanation
The assertion fails because the expected value and the value returned by the getter differ.
- A
- 41
What can a passing test run establish about the tested entity behavior?
- A
The application is guaranteed to work on every production server.
- B
All database columns are implemented, even if the entity does not retrieve them.
- C
The assertions in the executed tests matched the observed values.
- D
Every possible behavior of the entity is correct, including untested behavior.
Correct answer
CThe assertions in the executed tests matched the observed values.
Explanation
A passing run shows that the executed assertions succeeded; it does not establish the correctness of behavior that was not tested.
- A

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