weekly on-sync release 3.2dev
[moodle.git] / question / engine / tests / questionattemptstepiterator_test.php
CommitLineData
603bd001
PS
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/**
18 * This file contains tests for the question_attempt_step_iterator class.
19 *
20 * @package moodlecore
21 * @subpackage questionengine
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once(dirname(__FILE__) . '/../lib.php');
31require_once(dirname(__FILE__) . '/helpers.php');
32
33
34/**
35 * Unit tests for the {@link question_attempt_step_iterator} class.
36 *
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class question_attempt_step_iterator_test extends advanced_testcase {
41 private $qa;
42 private $iterator;
43
44 protected function setUp() {
45 $question = test_question_maker::make_question('description');
46 $this->qa = new testable_question_attempt($question, 0);
47 for ($i = 0; $i < 3; $i++) {
48 $step = new question_attempt_step(array('i' => $i));
49 $this->qa->add_step($step);
50 }
51 $this->iterator = $this->qa->get_step_iterator();
52 }
53
54 protected function tearDown() {
55 $this->qa = null;
56 $this->iterator = null;
57 }
58
59 public function test_foreach_loop() {
60 $i = 0;
61 foreach ($this->iterator as $key => $step) {
62 $this->assertEquals($i, $key);
63 $this->assertEquals($i, $step->get_qt_var('i'));
64 $i++;
65 }
66 }
67
68 public function test_foreach_loop_add_step_during() {
69 $i = 0;
70 foreach ($this->iterator as $key => $step) {
71 $this->assertEquals($i, $key);
72 $this->assertEquals($i, $step->get_qt_var('i'));
73 $i++;
74 if ($i == 2) {
75 $step = new question_attempt_step(array('i' => 3));
76 $this->qa->add_step($step);
77 }
78 }
79 $this->assertEquals(4, $i);
80 }
81
82 public function test_reverse_foreach_loop() {
83 $i = 2;
84 foreach ($this->qa->get_reverse_step_iterator() as $key => $step) {
85 $this->assertEquals($i, $key);
86 $this->assertEquals($i, $step->get_qt_var('i'));
87 $i--;
88 }
89 }
90
91 public function test_offsetExists_before_start() {
92 $this->assertFalse(isset($this->iterator[-1]));
93 }
94
95 public function test_offsetExists_at_start() {
96 $this->assertTrue(isset($this->iterator[0]));
97 }
98
99 public function test_offsetExists_at_endt() {
100 $this->assertTrue(isset($this->iterator[2]));
101 }
102
103 public function test_offsetExists_past_end() {
104 $this->assertFalse(isset($this->iterator[3]));
105 }
106
107 public function test_offsetGet_before_start() {
108 $this->setExpectedException('moodle_exception');
109 $step = $this->iterator[-1];
110 }
111
112 public function test_offsetGet_at_start() {
113 $step = $this->iterator[0];
114 $this->assertEquals(0, $step->get_qt_var('i'));
115 }
116
117 public function test_offsetGet_at_end() {
118 $step = $this->iterator[2];
119 $this->assertEquals(2, $step->get_qt_var('i'));
120 }
121
122 public function test_offsetGet_past_end() {
123 $this->setExpectedException('moodle_exception');
124 $step = $this->iterator[3];
125 }
126
127 public function test_cannot_set() {
128 $this->setExpectedException('moodle_exception');
129 $this->iterator[0] = null;
130 }
131
132 public function test_cannot_unset() {
133 $this->setExpectedException('moodle_exception');
134 unset($this->iterator[2]);
135 }
136}