PHP Quality Control & Assurance 4 — Questions and Answers
Question 1: Which tool generates human-readable HTML code coverage reports from PHPUnit's coverage data?
- phpdoc
- phpunit --coverage-html (Correct answer)
- phpstan --report
- phpcbf --html
Correct answer: phpunit --coverage-html
Running PHPUnit with --coverage-html <dir> generates an HTML report showing line-by-line coverage.
Question 2: In a PHP CI/CD pipeline, what is the primary role of a linter like PHP-CS-Fixer?
- Execute unit tests automatically
- Enforce consistent code style across the codebase (Correct answer)
- Detect security vulnerabilities
- Measure application performance
Correct answer: Enforce consistent code style across the codebase
PHP-CS-Fixer checks and automatically corrects code formatting to match a configured coding standard.
Question 3: What PHPUnit feature allows setUp() and tearDown() to run once per test class rather than per test method?
- setUpBeforeClass() and tearDownAfterClass() (Correct answer)
- classSetUp() and classTearDown()
- beforeAll() and afterAll()
- staticSetUp() and staticTearDown()
Correct answer: setUpBeforeClass() and tearDownAfterClass()
setUpBeforeClass() and tearDownAfterClass() are static methods that run once per class, unlike setUp()/tearDown() which run per test.
Question 4: What is a 'flaky test' in PHP QA terminology?
- A test with no assertions
- A test that passes and fails non-deterministically (Correct answer)
- A test that is skipped by default
- A test with too many mocks
Correct answer: A test that passes and fails non-deterministically
A flaky test produces inconsistent results across runs due to timing, environment, or shared state issues.
Question 5: Which PHP extension must be enabled to collect code coverage data in PHPUnit?
- xdebug or pcov (Correct answer)
- opcache
- apcu
- sodium
Correct answer: xdebug or pcov
PHPUnit requires either Xdebug or pcov to instrument PHP code and collect line/branch coverage information.
Question 6: What does 'contract testing' verify in a PHP microservices architecture?
- That all services share the same database schema
- That a service honors the interface contract expected by its consumers (Correct answer)
- That HTTP response times meet SLA targets
- That all services use the same PHP version
Correct answer: That a service honors the interface contract expected by its consumers
Contract testing ensures each service's API matches the expectations (contract) defined by the services that consume it.
Question 7: Which PHPMD rule category identifies methods or classes that are overly long and should be refactored?
- Controversial
- Design
- Code Size (Correct answer)
- Naming
Correct answer: Code Size
PHPMD's Code Size ruleset flags excessively long methods, classes, and high cyclomatic complexity.
Which tool generates human-readable HTML code coverage reports from PHPUnit's coverage data?