1. Home
  2. Explore
  3. Computer Science
  4. Installing PHPUnit with Composer

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Installing PHPUnit with Composer

  • 41Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Installation and Packaging

Which Composer command installs PHPUnit as a project dependency?

41 questions

  1. 1

    Which Composer command installs PHPUnit as a project dependency?

    1. A

      composer install phpunit/phpunit

    2. B

      composer create-project phpunit/phpunit

    3. C

      composer update --autoload phpunit/phpunit

    4. D

      composer require phpunit/phpunit

    Correct answer
    D

    composer require phpunit/phpunit

    Explanation

    The command composer require phpunit/phpunit adds PHPUnit to the project. During installation, Composer can prompt whether to add it as a development dependency.

  2. 2

    What PHPUnit version does the setup describe installing?

    1. A

      Version 8.2.3

    2. B

      Version 11.2.3

    3. C

      Version 10.2.3

    4. D

      Version 9.2.3

    Correct answer
    C

    Version 10.2.3

    Explanation

    The setup identifies version 10.2.3 as the latest version being installed at that point.

  3. 3

    Why should PHPUnit be installed as a development dependency rather than a production dependency?

    1. A

      PHPUnit can only run when the application has no production dependencies.

    2. B

      Tests are generally run during development, so production servers do not need PHPUnit.

    3. C

      Production dependencies cannot include packages written in PHP.

    4. D

      Development dependencies are automatically included in every production installation.

    Correct answer
    B

    Tests 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.

  4. 4

    Which Composer option could be used to mark PHPUnit as a development dependency directly in the command?

    1. A

      --global

    2. B

      --autoload

    3. C

      --production

    4. D

      --dev

    Correct answer
    D

    --dev

    Explanation

    The setup notes that the development-dependency choice could be made with the --dev flag during installation.

  5. 5

    What does the export-ignore attribute in a .gitattributes file do in this project?

    1. A

      Prevents Composer from installing the specified files as dependencies.

    2. B

      Prevents specified files from being committed to the repository.

    3. C

      Removes specified files from the developer’s local working directory.

    4. D

      Excludes specified files from exported package archives.

    Correct answer
    D

    Excludes specified files from exported package archives.

    Explanation

    The export-ignore attribute excludes listed files or directories from archives, such as package downloads, without being the same as ignoring them in Git.

  6. 6

    Which statement best distinguishes .gitignore from .gitattributes entries using export-ignore?

    1. A

      .gitignore controls Git’s treatment of untracked files, while export-ignore excludes files from exported archives.

    2. B

      .gitignore excludes files from package archives, while export-ignore prevents files from being committed.

    3. C

      Both files control which dependencies Composer installs, but for different environments.

    4. D

      Both files perform the same task, but .gitattributes applies only to PHP files.

    Correct answer
    A

    .gitignore controls Git’s treatment of untracked files, while export-ignore excludes files from exported archives.

    Explanation

    The setup uses .gitattributes with export-ignore to keep files out of exported package archives, while .gitignore is used for files such as generated PHPUnit cache data.

  7. 7

    Which set of files or directories is suitable for exclusion from a distributed package archive in the described project?

    1. A

      The tests directory, PHPUnit configuration, and development-only project files

    2. B

      The source directory, Composer autoloader, and runtime application entry point

    3. C

      The database schema, application source, and all package metadata

    4. D

      The production dependencies, application classes, and Composer manifest

    Correct answer
    A

    The 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.

  8. 8

    What should PHPUnit’s bootstrap setting load in this setup?

    1. A

      The project’s .gitattributes file

    2. B

      The Composer-generated vendor/autoload.php file

    3. C

      The application’s production database dump

    4. D

      A PHPUnit bootstrap file containing every test method

    Correct answer
    B

    The Composer-generated vendor/autoload.php file

    Explanation

    The configuration uses Composer’s vendor/autoload.php so project classes and other autoloaded files are available to tests.

  9. 9

    Why does the setup point PHPUnit directly to vendor/autoload.php instead of a test bootstrap file?

    1. A

      A bootstrap file is used only when tests do not extend TestCase.

    2. B

      PHPUnit cannot load bootstrap files located inside a test directory.

    3. C

      The test directory itself automatically defines every class without autoloading.

    4. D

      No separate test bootstrap file is needed for the setup; Composer’s autoloader provides the required loading.

    Correct answer
    D

    No 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.

  10. 10

    What is the purpose of PHPUnit’s colors configuration property?

    1. A

      To display test results with colors that help distinguish outcomes.

    2. B

      To select which source files PHPUnit includes in the test suite.

    3. C

      To generate color-coded XML configuration files.

    4. D

      To assign different namespaces to test classes.

    Correct answer
    A

    To 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.

  11. 11

    What is the role of the PHPUnit cache directory setting?

    1. A

      It determines which directory contains the test suite’s namespace.

    2. B

      It specifies where Composer stores the application’s source classes.

    3. C

      It specifies where PHPUnit stores its cache data.

    4. D

      It identifies the directory where PHPUnit writes production logs.

    Correct answer
    C

    It 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.

  12. 12

    How is the test suite organized in the described PHPUnit configuration?

    1. A

      A suite named source points to the vendor directory.

    2. B

      A suite named cache points to the project root.

    3. C

      A suite named unit points to the tests directory.

    4. D

      A suite named tests points to the src directory.

    Correct answer
    C

    A suite named unit points to the tests directory.

    Explanation

    The configuration defines a unit-test suite and assigns the tests directory as its test location.

  13. 13

    Which source directory and file pattern are specified for the project’s source files?

    1. A

      The project root and files matching *.json

    2. B

      The src directory and files matching *.php

    3. C

      The vendor directory and files matching *.test

    4. D

      The tests directory and files matching *.xml

    Correct answer
    B

    The src directory and files matching *.php

    Explanation

    The configuration identifies the project source as src and includes files with the PHP extension.

  14. 14

    Why might the PHPUnit configuration be kept as phpunit.xml.dist rather than only as phpunit.xml?

    1. A

      The .dist extension makes PHPUnit run tests without a test suite.

    2. B

      The .dist extension prevents the configuration from being read by PHPUnit.

    3. C

      The distribution file can provide a shared default while allowing a local phpunit.xml customization.

    4. D

      PHPUnit requires every project to store its configuration in two separate files.

    Correct answer
    C

    The distribution file can provide a shared default while allowing a local phpunit.xml customization.

    Explanation

    The setup uses the distribution configuration as a default and notes that PHPUnit uses phpunit.xml first if it exists, falling back to phpunit.xml.dist otherwise.

  15. 15

    If both phpunit.xml and phpunit.xml.dist are present, which configuration does PHPUnit use first?

    1. A

      Both files are always merged in alphabetical order

    2. B

      Whichever file was most recently modified

    3. C

      phpunit.xml

    4. D

      phpunit.xml.dist

    Correct answer
    C

    phpunit.xml

    Explanation

    The described behavior is that PHPUnit checks phpunit.xml first and uses the .dist file as a fallback when the first file is absent.

  16. 16

    Why should the PHPUnit cache directory generally be added to .gitignore?

    1. A

      It defines the namespace for every test class.

    2. B

      It is the standard location for production dependencies.

    3. C

      It contains the source code PHPUnit needs to run tests.

    4. D

      It is generated cache data and is not needed as a committed project file.

    Correct answer
    D

    It 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.

  17. 17

    What naming convention does the setup use for a test class that tests the User entity?

    1. A

      A class named User

    2. B

      A class named UserTest

    3. C

      A class named TestUserEntity

    4. D

      A class named EntityTestUser

    Correct answer
    B

    A class named UserTest

    Explanation

    The test class name uses the class under test followed by the Test suffix: UserTest.

  18. 18

    Why is the UserTest class declared final in the described testing style?

    1. A

      A final class allows its test methods to return values instead of void.

    2. B

      Test classes are generally not intended to be extended through inheritance.

    3. C

      Declaring a class final makes its assertions compare values loosely.

    4. D

      PHPUnit requires every test class to be declared final.

    Correct answer
    B

    Test classes are generally not intended to be extended through inheritance.

    Explanation

    The setup presents final as a good practice because inheritance is not common for an individual test class.

  19. 19

    What effect does declare(strict_types=1) have on typed arguments in the test file?

    1. A

      It makes all class properties public regardless of their declarations.

    2. B

      It prevents PHP from silently coercing an incompatible value to a declared type.

    3. C

      It automatically converts every argument to the type declared by its function.

    4. D

      It requires every test method to have at least one argument.

    Correct answer
    B

    It 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.

  20. 20

    Which class should a PHPUnit test class extend to use PHPUnit’s test-case features?

    1. A

      PHPUnit\Framework\TestSuite

    2. B

      PHPUnit\Framework\TestCase

    3. C

      PHPUnit\Framework\Assertion

    4. D

      Composer\Autoload\ClassLoader

    Correct answer
    B

    PHPUnit\Framework\TestCase

    Explanation

    The test class extends PHPUnit’s TestCase class to use features such as setup hooks and assertions.

  21. 21

    How should a test class override PHPUnit’s setup hook?

    1. A

      Define a static testSetup method and call TestCase::run().

    2. B

      Define a public construct method and call parent::construct().

    3. C

      Define a protected setUp method and call parent::setUp().

    4. D

      Define a private setUp method and omit the parent call.

    Correct answer
    C

    Define a protected setUp method and call parent::setUp().

    Explanation

    The described pattern overrides setUp as a protected method and calls the parent setup implementation.

  22. 22

    What is the main purpose of creating the User entity in setUp?

    1. A

      To run every test method immediately when the class is loaded.

    2. B

      To replace PHPUnit’s assertion methods with entity getters.

    3. C

      To prepare a fresh entity instance for the test methods to use.

    4. D

      To save the entity to the production database before each test.

    Correct answer
    C

    To 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.

  23. 23

    Which declaration best matches the described test method convention?

    1. A

      A protected static method whose name begins with run and has no return type

    2. B

      A private method whose name begins with assert and whose return type is User

    3. C

      A public method whose name begins with test and whose return type is void

    4. D

      A public method whose name begins with set and whose return type is bool

    Correct answer
    C

    A public method whose name begins with test and whose return type is void

    Explanation

    The examples use public test methods beginning with test and specify void as the return type.

  24. 24

    How should the test namespace relate to the project’s namespace in the described convention?

    1. A

      It should use the Composer vendor directory as its first namespace segment.

    2. B

      It should use the project namespace followed by a Tests segment and the relevant subnamespace.

    3. C

      It should replace the project namespace entirely with the test class name.

    4. D

      It should omit namespaces because test classes cannot be namespaced.

    Correct answer
    B

    It should use the project namespace followed by a Tests segment and the relevant subnamespace.

    Explanation

    The setup follows the convention of starting with the main project namespace, then adding Tests and the relevant area, such as Entity.

  25. 25

    What does the sequential ID test verify?

    1. A

      That the entity rejects every integer assigned to its ID setter.

    2. B

      That an ID assigned through the setter is returned by the corresponding getter.

    3. C

      That the sequential ID is generated by the UUID library.

    4. D

      That PHPUnit automatically assigns a database ID to every entity.

    Correct answer
    B

    That 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.

  26. 26

    Why does the setup prefer assertSame for checking the sequential ID?

    1. A

      It converts both values to strings before comparing them.

    2. B

      It verifies that the ID was written to a database table.

    3. C

      It checks only that both values are truthy.

    4. D

      It checks strict equality, including whether the values have the same type.

    Correct answer
    D

    It checks strict equality, including whether the values have the same type.

    Explanation

    assertSame uses strict comparison, so it checks both the value and its type rather than allowing type coercion.

  27. 27

    How does the described test create a UUID value using the Ramsey UUID library?

    1. A

      Generate a version 4 UUID and convert it to a string.

    2. B

      Use the entity getter to create a UUID without calling the library.

    3. C

      Generate a version 4 UUID and store the object without conversion.

    4. D

      Read a sequential integer ID and convert it to a UUID object.

    Correct answer
    A

    Generate 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.

  28. 28

    What relationship does the UUID setter-and-getter test check?

    1. A

      The entity’s UUID getter generates a different UUID on every call.

    2. B

      The UUID string passed to the setter is returned by the entity’s UUID getter.

    3. C

      The UUID setter converts the UUID string into a sequential integer ID.

    4. D

      The UUID getter returns the Ramsey UUID object rather than the stored string.

    Correct answer
    B

    The 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.

  29. 29

    When testing the entity’s data-population or unserialization behavior, what is passed into it?

    1. A

      An associative array containing the user’s data fields

    2. B

      A database connection containing only the table name

    3. C

      A Composer configuration file containing dependency versions

    4. D

      A PHPUnit test result object containing assertion counts

    Correct answer
    A

    An 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.

  30. 30

    What should the first-name assertion in the data-population test verify?

    1. A

      The entity’s first-name getter returns the user’s email address.

    2. B

      The input array’s first-name field is replaced with the user’s UUID.

    3. C

      The entity’s first-name getter returns the first-name value from the input array.

    4. D

      The entity’s first-name getter returns a value generated by PHPUnit.

    Correct answer
    C

    The entity’s first-name getter returns the first-name value from the input array.

    Explanation

    The test checks that data such as Pierre supplied under the first-name field can be retrieved through the entity’s first-name getter.

  31. 31

    What should the last-name assertion in the data-population test verify?

    1. A

      The entity’s last-name getter returns the value supplied for the phone field.

    2. B

      The entity’s last-name getter creates a new value from the sequential ID.

    3. C

      The entity’s last-name getter returns the last-name value supplied in the array.

    4. D

      The array’s last-name field is converted into the entity’s email field.

    Correct answer
    C

    The 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.

  32. 32

    What does the email assertion check after user data is populated?

    1. A

      That the email value is used as the entity’s sequential ID.

    2. B

      That the email getter generates a valid email address automatically.

    3. C

      That the email getter returns the email value supplied in the array.

    4. D

      That the input array’s email field is omitted from the entity.

    Correct answer
    C

    That 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.

  33. 33

    What does the phone assertion check after user data is populated?

    1. A

      That the phone getter returns the phone value supplied in the array.

    2. B

      That the phone getter formats the value as a UUID.

    3. C

      That the input phone value is stored as a PHPUnit assertion count.

    4. D

      That the phone value replaces the entity’s password.

    Correct answer
    A

    That 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.

  34. 34

    What does the password assertion check in the example data-population test?

    1. A

      That the password getter returns the password value supplied in the array.

    2. B

      That PHPUnit automatically removes the password from the entity.

    3. C

      That the password getter returns the user’s creation date.

    4. D

      That the entity hashes the password using a behavior demonstrated by the test.

    Correct answer
    A

    That 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.

  35. 35

    How is the creation-date value for the data-population test chosen in the example?

    1. A

      It generates a new UUID and stores it as the creation date.

    2. B

      It uses the current time in any format accepted by the operating system.

    3. C

      It uses the PHPUnit cache directory as a date string.

    4. D

      It uses a value matching the date format stored for users in the database.

    Correct answer
    D

    It 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.

  36. 36

    Why does the setup avoid testing the session-token and last-session-time fields at this stage?

    1. A

      PHPUnit cannot assert values that come from database columns.

    2. B

      The fields are already covered automatically by the UUID test.

    3. C

      Those fields have not yet been implemented in the user entity.

    4. D

      Those fields must always be tested only on a production server.

    Correct answer
    C

    Those 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.

  37. 37

    How can PHPUnit be run when it is installed globally and available in the terminal?

    1. A

      Run phpunit.xml.dist as a shell command.

    2. B

      Run phpunit from the terminal.

    3. C

      Run vendor/autoload.php as a shell command.

    4. D

      Run composer export-ignore from the terminal.

    Correct answer
    B

    Run phpunit from the terminal.

    Explanation

    If PHPUnit is installed globally, the phpunit command can be invoked directly from the terminal.

  38. 38

    How can PHPUnit be run if it is installed locally in the project but not globally?

    1. A

      Run src/vendor/autoload.php.

    2. B

      Run vendor/bin/phpunit.

    3. C

      Run tests/bin/phpunit.

    4. D

      Run composer/phpunit.xml.

    Correct answer
    B

    Run vendor/bin/phpunit.

    Explanation

    A local Composer installation places the executable in vendor/bin/phpunit.

  39. 39

    How can a developer run only one named test method instead of the full suite?

    1. A

      Rename the test directory to match the method name.

    2. B

      Add the method name to the Composer autoload section.

    3. C

      Use PHPUnit’s filter option with the test class and method name.

    4. D

      Delete the other test methods before running PHPUnit.

    Correct answer
    C

    Use 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.

  40. 40

    What does a failing assertSame result indicate when the expected first name is changed but the entity still returns the original name?

    1. A

      The actual getter value did not strictly match the expected value.

    2. B

      The test passed because the entity returned a nonempty string.

    3. C

      The failure proves that the PHPUnit configuration did not load.

    4. D

      The assertion changed the entity’s stored first name automatically.

    Correct answer
    A

    The 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.

  41. 41

    What can a passing test run establish about the tested entity behavior?

    1. A

      The application is guaranteed to work on every production server.

    2. B

      All database columns are implemented, even if the entity does not retrieve them.

    3. C

      The assertions in the executed tests matched the observed values.

    4. D

      Every possible behavior of the entity is correct, including untested behavior.

    Correct answer
    C

    The 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.

Question 1 of 41
Question 1 of 41

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent