PHP Quality Control & Assurance 3 — Questions and Answers
Question 1: Which PHPUnit annotation marks a test that is expected to throw a specific exception?
- @expectedError
- @throws
- @expectedException (Correct answer)
- @willThrow
Correct answer: @expectedException
@expectedException (or the method expectException()) tells PHPUnit the test passes only if that exception is thrown.
Question 2: What is a test double in PHPUnit?
- Running a test twice to check reproducibility
- An object that replaces a real dependency for testing (Correct answer)
- A backup copy of a test suite
- A method that calls another test method
Correct answer: An object that replaces a real dependency for testing
A test double is a generic term for stubs, mocks, spies, and fakes used to isolate the unit under test.
Question 3: In PHPStan, what does 'level 8' (max) analysis enforce that lower levels do not?
- Syntax errors only
- Strict checking of mixed types and all implicit conversions (Correct answer)
- PSR-12 style rules
- Test coverage requirements
Correct answer: Strict checking of mixed types and all implicit conversions
PHPStan level 8 adds strict checks on mixed types, requiring explicit handling rather than allowing implicit coercion.
Question 4: Which PHP testing concept verifies that a mock method is called exactly N times?
- expects($this->exactly(N)) (Correct answer)
- assertCalled(N)
- times(N)->verify()
- callCount(N)
Correct answer: expects($this->exactly(N))
PHPUnit's expects($this->exactly(N)) sets an expectation that a mocked method is invoked a precise number of times.
Question 5: What is mutation testing in the context of PHP quality assurance?
- Testing code on multiple PHP versions
- Introducing small code changes to verify tests catch them (Correct answer)
- Randomizing test execution order
- Transforming test data types automatically
Correct answer: Introducing small code changes to verify tests catch them
Mutation testing tools like Infection modify code slightly ('mutants') and confirm that existing tests fail for each change.
Question 6: Which directive in a phpunit.xml file sets the minimum required line coverage percentage?
- <minCoverage>
- <coverageThreshold>
- <require-coverage>
- forceCoversAnnotation inside <coverage> (Correct answer)
Correct answer: forceCoversAnnotation inside <coverage>
PHPUnit uses <coverage> configuration with attributes like forceCoversAnnotation and separate minLines/minMethods settings, not a single tag.
Question 7: What does the PHP function 'assert()' do when zend.assertions is set to -1 in production?
- Throws an AssertionError
- Is completely removed at compile time and has zero cost (Correct answer)
- Logs a warning
- Behaves the same as in development
Correct answer: Is completely removed at compile time and has zero cost
With zend.assertions=-1, assert() calls are compiled out entirely, so they add no runtime overhead in production.
Which PHPUnit annotation marks a test that is expected to throw a specific exception?