Discover the advanced autograding options available for PHP assignments
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:
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:
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 directorycp $UPLOADED_FILES/*.mv~/composer.json.# wrap a bash script around phpunit (needed to handle the exit code returned by phpunit)cat<<EOF>script.shphpunit --log-junit report.xml FibonacciTests.php cg junitxml report.xmlexit 0EOF# run the script that calls phpunitchmod+xscript.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.shphpcs --report=checkstyle --standard=PSR12 ./Fibonacci.php > report.xmlcg checkstyle parse report.xmlexit 0EOF# run the script that calls phpcschmod+xscript.sh./script.sh
Running the Code Quality Test will produce inline comments within the student submission, as shown below: