PHP Quality Control & Assurance 2 — Questions and Answers
Question 1: Which PHPUnit method asserts that two arrays have identical key-value pairs regardless of order?
- assertArrayEquals()
- assertSameArray()
- assertEqualsCanonicalizing() (Correct answer)
- assertArrayIdentical()
Correct answer: assertEqualsCanonicalizing()
assertEqualsCanonicalizing() sorts arrays before comparing so order does not matter.
Question 2: What is the purpose of a code coverage threshold in a CI pipeline?
- To measure execution speed
- To fail the build when coverage drops below a set percentage (Correct answer)
- To count the number of unit tests
- To enforce PSR coding standards
Correct answer: To fail the build when coverage drops below a set percentage
A coverage threshold causes the CI build to fail if the percentage of covered code falls below the configured minimum.
Question 3: Which Composer script command is commonly used to run PHPStan static analysis?
- composer test
- composer analyse (Correct answer)
- composer lint
- composer check
Correct answer: composer analyse
By convention, projects add a 'analyse' script in composer.json that runs PHPStan, though the name is configurable.
Question 4: In PHP, which function verifies that a value is exactly equal to another value AND of the same type?
- assertEqual()
- assertSame() uses === (Correct answer)
- assertIdentical()
- assertStrictEquals()
Correct answer: assertSame() uses ===
PHPUnit's assertSame() uses strict comparison (===), checking both value and type.
Question 5: What does PHP_CodeSniffer's 'phpcbf' tool do?
- Analyzes code for bugs
- Automatically fixes coding standard violations (Correct answer)
- Runs unit tests
- Generates API documentation
Correct answer: Automatically fixes coding standard violations
phpcbf (PHP Code Beautifier and Fixer) automatically corrects many coding standard violations detected by phpcs.
Question 6: Which PHPUnit feature allows you to run the same test with multiple sets of input data?
- Test suites
- Data providers (Correct answer)
- Mock factories
- Fixtures
Correct answer: Data providers
Data providers are methods annotated with @dataProvider that supply arrays of argument sets to a test method.
Question 7: What is Rector used for in PHP projects?
- Generating test coverage reports
- Automated code refactoring and upgrades (Correct answer)
- Managing Composer dependencies
- Running integration tests
Correct answer: Automated code refactoring and upgrades
Rector is an automated refactoring tool that applies configurable rules to upgrade or modernize PHP code.
Which PHPUnit method asserts that two arrays have identical key-value pairs regardless of order?