MDL-52333: qtype_calculated: Adding division by zero test
authorDavid Monllao <davidm@moodle.com>
Thu, 10 Dec 2015 08:37:48 +0000 (16:37 +0800)
committerDavid Monllao <davidm@moodle.com>
Thu, 10 Dec 2015 08:38:57 +0000 (16:38 +0800)
question/type/calculated/tests/variablesubstituter_test.php

index f9be863..b670306 100644 (file)
@@ -65,6 +65,17 @@ class qtype_calculated_variable_substituter_test extends advanced_testcase {
         $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12.
     }
 
+    public function test_division_by_zero_expression() {
+
+        if (intval(PHP_VERSION) < 7) {
+            $this->markTestSkipped('Division by zero triggers a PHP warning before PHP 7.');
+        }
+
+        $this->setExpectedException('moodle_exception');
+        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 0), '.');
+        $vs->calculate('{a} / {b}');
+    }
+
     public function test_replace_expressions_in_text_simple_var() {
         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
         $this->assertEquals('1 + 2', $vs->replace_expressions_in_text('{a} + {b}'));