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 Configuration

What Composer command installs PHPUnit as a development dependency?

41 questions

  1. 1

    What Composer command installs PHPUnit as a development dependency?

    Model answer

    Run composer require --dev phpunit/phpunit.

    Explanation

    The --dev option records PHPUnit under Composer's development requirements.

  2. 2

    Why is PHPUnit installed as a development dependency rather than a production dependency?

    Model answer

    Unit tests are run during development, so the production server generally does not need PHPUnit.

    Explanation

    Keeping testing tools in development requirements avoids including them as production dependencies.

  3. 3

    Which PHPUnit version was installed in the demonstration?

    Model answer

    The installed version was PHPUnit 10.2.3.

    Explanation

    The demonstration selected version 10.2.3 as the latest version shown at the time.

  4. 4

    What is the role of phpunit.xml.dist in a PHPUnit project?

    Model answer

    It provides the project's distributed default PHPUnit configuration.

    Explanation

    PHPUnit reads this configuration to determine settings such as test suites and bootstrap files.

  5. 5

    Why does the PHPUnit configuration bootstrap vendor/autoload.php?

    Model answer

    It loads Composer's autoloader so project classes and other autoloaded files are available to tests.

    Explanation

    This gives tests the same Composer-based class loading used by the application.

  6. 6

    How was the unit test suite configured to locate tests?

    Model answer

    The suite was named unit and pointed to the tests directory.

    Explanation

    This configuration tells PHPUnit where to discover the project's unit tests.

  7. 7

    Which project source directory and file type were specified in the PHPUnit configuration?

    Model answer

    The configuration included PHP files from the src directory.

    Explanation

    The source entry identifies the codebase files that PHPUnit should treat as project source.

  8. 8

    What do PHPUnit's color setting and cache directory contribute?

    Model answer

    Colors make test results easier to read, while the cache directory stores PHPUnit's cache data.

    Explanation

    The demonstrated cache directory was .phpunit.cache.

  9. 9

    Why should a PHPUnit test class commonly be declared final?

    Model answer

    Declaring it final prevents other classes from extending it, which is usually unnecessary for an individual test class.

    Explanation

    The demonstration presents inheritance between individual test classes as uncommon.

  10. 10

    What does declare(strict_types=1) require when PHP types are declared?

    Model answer

    It makes type checking strict, so PHP does not silently coerce values of the wrong type at typed calls.

    Explanation

    The demonstration contrasts strict type checking with PHP attempting to cast a value to the declared type.

  11. 11

    Which PHPUnit class should a test class extend to use PHPUnit's testing features?

    Model answer

    It should extend PHPUnit\Framework\TestCase.

    Explanation

    The test case base class provides test lifecycle behavior and assertion methods.

  12. 12

    What is the purpose of the setUp method in a PHPUnit test class?

    Model answer

    It prepares shared test state before each test runs, such as constructing the entity being tested.

    Explanation

    The demonstration initializes a new user entity in setUp so test methods can reuse it.

  13. 13

    Why should an overridden setUp method call parent::setUp()?

    Model answer

    It allows the parent test case's setup behavior to run as well as the subclass's setup.

    Explanation

    The demonstration follows this practice when overriding PHPUnit's setup method.

  14. 14

    How should a test class name and its test methods be named for PHPUnit discovery?

    Model answer

    The class name should end in Test, and test methods should be public and named with a test prefix.

    Explanation

    The example uses a UserTest class with public methods such as testSequentialID.

  15. 15

    Why can a test method declare a void return type?

    Model answer

    A test method performs assertions rather than returning a result value.

    Explanation

    The demonstrated test methods use void as their return type.

  16. 16

    What is the benefit of storing the entity under test in a private, typed property?

    Model answer

    The property makes the entity available to the test class's methods while declaring that it must be a user entity.

    Explanation

    The demonstration initializes this property with a new user entity in setUp.

  17. 17

    How does PHPUnit's assertSame differ from a non-strict equality assertion?

    Model answer

    assertSame checks both value and type, corresponding to PHP's strict === comparison.

    Explanation

    This helps a test catch cases where the value matches but its type does not.

  18. 18

    How can a test verify that a user entity stores and returns its sequential ID?

    Model answer

    Set a sequential ID on the entity, then use assertSame to compare that expected ID with the value returned by its getter.

    Explanation

    The demonstration sets the ID to a known value and checks getSequentialID().

  19. 19

    How does the demonstrated test generate a UUID for the user entity?

    Model answer

    It calls Ramsey UUID's uuid4() method and converts the result to a string with toString().

    Explanation

    The library returns a UUID object, while the entity is given the string representation.

  20. 20

    How can a test check that the user entity returns the UUID that was assigned to it?

    Model answer

    Assign the generated UUID string to the entity, then compare it with the result of the UUID getter using assertSame.

    Explanation

    This verifies that the setter and getter preserve the UUID string.

  21. 21

    What happens to a test when its expected value differs from the entity's actual getter value?

    Model answer

    The assertion fails, and PHPUnit reports the mismatch between the expected and actual values.

    Explanation

    The demonstration changes an expected first name and observes the test fail because the getter returns a different name.

  22. 22

    What is the purpose of testing a user entity's unserialization behavior?

    Model answer

    It checks that the entity can accept serialized user data and expose the corresponding values through its getters.

    Explanation

    The demonstration passes an expected data array to the entity's unserialization method and checks the resulting properties.

  23. 23

    How should a test verify the sequential ID after unserializing user data?

    Model answer

    Include an ID in the input data and use assertSame to compare it with the entity's sequential ID getter.

    Explanation

    This checks that unserialization maps the input ID to the entity correctly.

  24. 24

    How should a test verify the UUID after unserializing user data?

    Model answer

    Include a UUID string in the input data and compare it with the entity's UUID getter using assertSame.

    Explanation

    This checks the UUID field's mapping from the input data to the entity.

  25. 25

    How should a test verify the first name after unserializing user data?

    Model answer

    Include a first name in the input data and compare it with the entity's first-name getter using assertSame.

    Explanation

    The example uses a known name so the test can confirm the field is mapped correctly.

  26. 26

    How should a test verify the last name after unserializing user data?

    Model answer

    Include a last name in the input data and compare it with the entity's last-name getter using assertSame.

    Explanation

    This confirms that the last-name value is transferred from the input data to the entity.

  27. 27

    How should a test verify the email after unserializing user data?

    Model answer

    Include an email in the input data and compare it with the entity's email getter using assertSame.

    Explanation

    The assertion checks that the entity retains the supplied email value.

  28. 28

    How should a test verify the phone number after unserializing user data?

    Model answer

    Include a phone number in the input data and compare it with the entity's phone getter using assertSame.

    Explanation

    This checks that the phone field is correctly populated from the input data.

  29. 29

    How should a test verify the password after unserializing user data?

    Model answer

    Include a password value in the input data and compare it with the entity's password getter using assertSame.

    Explanation

    The demonstration checks this field along with the entity's other serialized user data.

  30. 30

    How should a test verify the creation date after unserializing user data?

    Model answer

    Include the creation date in the input data and compare it with the entity's creation-date getter using assertSame.

    Explanation

    The example uses a creation date in the format stored by the application.

  31. 31

    Why were session token and last session time not included in the entity tests?

    Model answer

    Those fields had not yet been implemented in the user entity, so there was no entity behavior for the tests to verify.

    Explanation

    The fields existed in the database, but the demonstration notes that the entity did not yet retrieve them.

  32. 32

    How can PHPUnit be run when it is installed globally?

    Model answer

    Run phpunit from the terminal.

    Explanation

    A global installation makes the PHPUnit command available from different project directories.

  33. 33

    How can PHPUnit be run when it is installed only in the project?

    Model answer

    Run the project's executable at vendor/bin/phpunit.

    Explanation

    Composer installs project-local executable binaries in the vendor/bin directory.

  34. 34

    How can a developer run only one PHPUnit test method?

    Model answer

    Use PHPUnit's --filter option with the test class and method, such as UserTest::testOnSerialize.

    Explanation

    Filtering is useful when the developer wants to run a specific test rather than the full suite.

  35. 35

    Which PHPUnit configuration file takes precedence when both phpunit.xml and phpunit.xml.dist exist?

    Model answer

    phpunit.xml takes precedence; PHPUnit falls back to phpunit.xml.dist if the first file is absent.

    Explanation

    The local configuration can therefore override the distributed default.

  36. 36

    Why might a developer avoid committing a customized phpunit.xml file?

    Model answer

    It can contain local PHPUnit settings that should not replace the shared distributed configuration for everyone.

    Explanation

    The demonstration recommends ignoring the customized file while retaining the distribution configuration.

  37. 37

    What is the purpose of ignoring .phpunit.cache in Git?

    Model answer

    It prevents PHPUnit-generated cache data from being committed as project source.

    Explanation

    PHPUnit generates this cache directory while running tests.

  38. 38

    What does the export-ignore attribute in .gitattributes do?

    Model answer

    It excludes specified files or directories from Git archive exports.

    Explanation

    This can keep development-only contents out of a package archive without removing them from the repository.

  39. 39

    Why might a package repository mark its tests directory as export-ignore?

    Model answer

    Tests may be useful in the repository but unnecessary in the archive distributed to package users.

    Explanation

    The demonstration describes excluding test files from the archive created for a package release.

  40. 40

    What is the distinction between .gitignore and .gitattributes export-ignore in this setup?

    Model answer

    .gitignore prevents files from being tracked by Git, while export-ignore excludes tracked files from exported archives.

    Explanation

    The demonstration uses Git ignoring for PHPUnit's generated cache and archive exclusion for package contents.

  41. 41

    Why should the PHPUnit cache be excluded even when the test files are committed?

    Model answer

    The test files are project code, but the cache is generated data that PHPUnit can recreate.

    Explanation

    Keeping these roles separate lets the project retain its tests without committing temporary cache contents.

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