|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once(dirname(__FILE__) . '/../lib.php'); 00030 require_once(dirname(__FILE__) . '/helpers.php'); 00031 00032 00039 class question_attempt_step_iterator_test extends UnitTestCase { 00040 private $qa; 00041 private $iterator; 00042 00043 public function setUp() { 00044 $question = test_question_maker::make_question('description'); 00045 $this->qa = new testable_question_attempt($question, 0); 00046 for ($i = 0; $i < 3; $i++) { 00047 $step = new question_attempt_step(array('i' => $i)); 00048 $this->qa->add_step($step); 00049 } 00050 $this->iterator = $this->qa->get_step_iterator(); 00051 } 00052 00053 public function tearDown() { 00054 $this->qa = null; 00055 $this->iterator = null; 00056 } 00057 00058 public function test_foreach_loop() { 00059 $i = 0; 00060 foreach ($this->iterator as $key => $step) { 00061 $this->assertEqual($i, $key); 00062 $this->assertEqual($i, $step->get_qt_var('i')); 00063 $i++; 00064 } 00065 } 00066 00067 public function test_foreach_loop_add_step_during() { 00068 $i = 0; 00069 foreach ($this->iterator as $key => $step) { 00070 $this->assertEqual($i, $key); 00071 $this->assertEqual($i, $step->get_qt_var('i')); 00072 $i++; 00073 if ($i == 2) { 00074 $step = new question_attempt_step(array('i' => 3)); 00075 $this->qa->add_step($step); 00076 } 00077 } 00078 $this->assertEqual(4, $i); 00079 } 00080 00081 public function test_reverse_foreach_loop() { 00082 $i = 2; 00083 foreach ($this->qa->get_reverse_step_iterator() as $key => $step) { 00084 $this->assertEqual($i, $key); 00085 $this->assertEqual($i, $step->get_qt_var('i')); 00086 $i--; 00087 } 00088 } 00089 00090 public function test_offsetExists_before_start() { 00091 $this->assertFalse(isset($this->iterator[-1])); 00092 } 00093 00094 public function test_offsetExists_at_start() { 00095 $this->assertTrue(isset($this->iterator[0])); 00096 } 00097 00098 public function test_offsetExists_at_endt() { 00099 $this->assertTrue(isset($this->iterator[2])); 00100 } 00101 00102 public function test_offsetExists_past_end() { 00103 $this->assertFalse(isset($this->iterator[3])); 00104 } 00105 00106 public function test_offsetGet_before_start() { 00107 $this->expectException(); 00108 $step = $this->iterator[-1]; 00109 } 00110 00111 public function test_offsetGet_at_start() { 00112 $step = $this->iterator[0]; 00113 $this->assertEqual(0, $step->get_qt_var('i')); 00114 } 00115 00116 public function test_offsetGet_at_end() { 00117 $step = $this->iterator[2]; 00118 $this->assertEqual(2, $step->get_qt_var('i')); 00119 } 00120 00121 public function test_offsetGet_past_end() { 00122 $this->expectException(); 00123 $step = $this->iterator[3]; 00124 } 00125 00126 public function test_cannot_set() { 00127 $this->expectException(); 00128 $this->iterator[0] = null; 00129 } 00130 00131 public function test_cannot_unset() { 00132 $this->expectException(); 00133 unset($this->iterator[2]); 00134 } 00135 }