Differences Between: [Versions 310 and 402] [Versions 39 and 402]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core_question; 18 19 use question_attempt_step; 20 use testable_question_attempt; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 require_once (__DIR__ . '/../lib.php'); 26 require_once (__DIR__ . '/helpers.php'); 27 28 /** 29 * Unit tests for the {@link question_attempt_step_iterator} class. 30 * 31 * @package core_question 32 * @category test 33 * @copyright 2009 The Open University 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class questionattemptstepiterator_test extends \advanced_testcase { 37 private $qa; 38 private $iterator; 39 40 protected function setUp(): void { 41 $question = \test_question_maker::make_question('description'); 42 $this->qa = new testable_question_attempt($question, 0); 43 for ($i = 0; $i < 3; $i++) { 44 $step = new question_attempt_step(array('i' => $i)); 45 $this->qa->add_step($step); 46 } 47 $this->iterator = $this->qa->get_step_iterator(); 48 } 49 50 protected function tearDown(): void { 51 $this->qa = null; 52 $this->iterator = null; 53 } 54 55 public function test_foreach_loop() { 56 $i = 0; 57 foreach ($this->iterator as $key => $step) { 58 $this->assertEquals($i, $key); 59 $this->assertEquals($i, $step->get_qt_var('i')); 60 $i++; 61 } 62 } 63 64 public function test_foreach_loop_add_step_during() { 65 $i = 0; 66 foreach ($this->iterator as $key => $step) { 67 $this->assertEquals($i, $key); 68 $this->assertEquals($i, $step->get_qt_var('i')); 69 $i++; 70 if ($i == 2) { 71 $step = new question_attempt_step(array('i' => 3)); 72 $this->qa->add_step($step); 73 } 74 } 75 $this->assertEquals(4, $i); 76 } 77 78 public function test_reverse_foreach_loop() { 79 $i = 2; 80 foreach ($this->qa->get_reverse_step_iterator() as $key => $step) { 81 $this->assertEquals($i, $key); 82 $this->assertEquals($i, $step->get_qt_var('i')); 83 $i--; 84 } 85 } 86 87 public function test_offsetExists_before_start() { 88 $this->assertFalse(isset($this->iterator[-1])); 89 } 90 91 public function test_offsetExists_at_start() { 92 $this->assertTrue(isset($this->iterator[0])); 93 } 94 95 public function test_offsetExists_at_endt() { 96 $this->assertTrue(isset($this->iterator[2])); 97 } 98 99 public function test_offsetExists_past_end() { 100 $this->assertFalse(isset($this->iterator[3])); 101 } 102 103 public function test_offsetGet_before_start() { 104 $this->expectException(\moodle_exception::class); 105 $step = $this->iterator[-1]; 106 } 107 108 public function test_offsetGet_at_start() { 109 $step = $this->iterator[0]; 110 $this->assertEquals(0, $step->get_qt_var('i')); 111 } 112 113 public function test_offsetGet_at_end() { 114 $step = $this->iterator[2]; 115 $this->assertEquals(2, $step->get_qt_var('i')); 116 } 117 118 public function test_offsetGet_past_end() { 119 $this->expectException(\moodle_exception::class); 120 $step = $this->iterator[3]; 121 } 122 123 public function test_cannot_set() { 124 $this->expectException(\moodle_exception::class); 125 $this->iterator[0] = null; 126 } 127 128 public function test_cannot_unset() { 129 $this->expectException(\moodle_exception::class); 130 unset($this->iterator[2]); 131 } 132 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body