In this guide, we explore the advanced grading options available for PHP assignments. For more information about setting up a PHP assignment from scratch, see:
Unit tests with PHPUnit
PHPUnit is an industry-standard unit testing framework for PHP. It is particularly useful for grading assignments that require students to write functions and classes. Using PHPUnit unit tests offers several advantages over conventional IO tests, including the ability to use assertions, parametrize test cases, and provide better feedback for students. For further documentation about PHPUnit we recommend this online resource.
Consider the example submission Fibonacci.php file below:
Our goal is to unit test the ComputeFibonacci function. Our tests are defined in the following FibonacciTests.php file:
<?php
use PHPUnit\Framework\TestCase;
include('./Fibonacci.php');
class FibonacciTests extends TestCase
{
public function testFibonacciZero()
{
$this->assertEquals(0, ComputeFibonacci(0));
}
public function testFibonacciOne()
{
$this->assertEquals(1, ComputeFibonacci(5));
}
public function testFibonacciTwo()
{
$this->assertEquals(1, ComputeFibonacci(2));
}
public function testFibonacciThree()
{
$this->assertEquals(2, ComputeFibonacci(3));
}
public function testFibonacciTen()
{
$this->assertEquals(55, ComputeFibonacci(10));
}
public function testFibonacciTwenty()
{
$this->assertEquals(6765, ComputeFibonacci(20));
}
}
Setup: Install PHPUnit
After installing PHP as described in the previous guide, we can use Script Block to install Composer, a PHP dependency management framework, and then PHPUnit.
In the Install php dependecies Script block above, we run the following Bash script:
Once we are done with the Setup Phase in AutoTest, we can move on to the next Tests phase. Here, we first need to upload our testing file FibonacciTests.php using an Upload Files Block.
We can then run our unit tests using a Custom Test Block as shown below:
The Custom Test Block runs the following commands:
set -e
# move the necessary files to the current directory
cp $UPLOADED_FILES/* .
mv ~/composer.json .
# wrap a bash script around phpunit (needed to handle the exit code returned by phpunit)
cat <<EOF > script.sh
phpunit --log-junit report.xml FibonacciTests.php
cg junitxml report.xml
exit 0
EOF
# run the script that calls phpunit
chmod +x script.sh
./script.sh
Notice that you can hide these commands from the students using a Hide Block.
The results of the unit tests will be shown to the student as below:
Code Quality Tests with PHP_CodeSniffer
PHP_CodeSniffer is an industry-standard linter for PHP that allows you to enforce rules from various coding standards. It is a useful tool for enforcing code styling best practices for beginner programmers. To read more about PHP_CodeSniffer, we recommend this online resource.
Setup: Install php_codesniffer
We can repeat the same steps described in the section above for installing PHPUnit, just now the installing script is slightly different:
We can run PHP_Codesniffer on a student file, Fibonacci.php` in the example below, through a Custom Test Block:
Within the Custom Test Block, we run the following script:
set -e
# wrap a bash script around phpcs (needed to handle the exit code returned by phpcs)
cat <<EOF > script.sh
phpcs --report=checkstyle --standard=PSR12 ./Fibonacci.php > report.xml
cg checkstyle parse report.xml
exit 0
EOF
# run the script that calls phpcs
chmod +x script.sh
./script.sh
Running the Code Quality Test will produce inline comments within the student submission, as shown below: