PHP

We use phpunit as a testing framework for PHP coding exercises.

Hello World

Let's start with a Hello World exercise to explain the basics of evaluation in PHP. Following is a sample solution file named hello_world.php:

<?php
echo "Hello World!";

And this is an example of what you could write to evaluate the student's code.

<?php
use PHPUnit\Framework\TestCase;

class Evaluate extends TestCase {

  public function testHelloWorld() {
    ob_start();
    require "hello_world.php";
    $output = ob_get_clean();
    $this->assertEquals("Hello World!", $output, "You must print Hello World");
  }
}

Let's examine the above evaluation code.

First, we must import the testing framework, "PHPUnit\Framework\TestCase". Then we declare our test class which must extend TestCase.

Inside our test class, we define a test case named testHelloWorld. This test case basically

  • Starts catching STDOUT with ob_start()

  • Runs exercise.php by including it.

  • ob_get_clean() returns the output written to STDOUT since the ob_start() called.

  • Asserts the output

Function Calls

Now we will change the Hello World example a little bit and move the echo statement into a function. The task is still to print "Hello World" but only when the helloWorld() function is called.

Following is the sample solution code in hello_world.php file:

<?php
function helloWorld() {
  echo "Hello World!";
}

To check whether the student's function is working properly, we need to include the student file and call the function.

The code below is a sample evaluator for helloWorld() function.

<?php
use PHPUnit\Framework\TestCase;

require_once "hello_world.php";

class Evaluate extends TestCase {

  public function testHelloWorld() {
    ob_start();
    helloWorld();
    $output = ob_get_clean();
    $this->assertEquals("Hello World!", $output, "You must print Hello World");
  }
}

The only difference of this evaluation code from the original is that we explicitly called helloWorld() function since including the file is not enough to run the code anymore.

Variable Declarations

In your evaluator code, you can check whether a variable is defined in the student's code and check its value. Consider the exercise is to declare two variables, namely name and age and assign "Jon Snow" and 29 respectively. A sample solution file (exercise.php) is below:

<?php

$name = "John Snow"
$age = 29

In the test code, we will first check if the variables exist and then compare their values with expected ones. Here is an example test code:

<?php
use PHPUnit\Framework\TestCase;

require_once "exercise.php";

class Evaluate extends TestCase {

  public function testName() {
    $this->assertTrue(isset($name), "You must declare name");
    $this->assertEquals("John Snow", $name, "name must be John Snow");
  }

  public function testAge() {
    $this->assertTrue(isset($age), "You must declare age");
    $this->assertEquals(29, $name, "age must be 29");
  }
}

Class Declarations

You can test student's object oriented knowledge. Let's say the exercise is asking students to declare a class named Dog and implement a function inside named bark() . The function will return "woof woof" when it's called.

An example solution is given below:

<?php

class Dog {
  public function bark() {
    return "woof woof";
  }
}

And evaluation code can be implemented as:

<?php
use PHPUnit\Framework\TestCase;

require_once "Dog.php";

class Evaluate extends TestCase {

  public function testBark() {
    $this->assertTrue(class_exists("Dog"), "You must declare Dog class");
    $dog = new Dog();
    $this->assertEquals("woof woof", $dog->bark(), "bark() must return 'woof woof'");
  }
}

Last updated