Skip to content

Commit

Permalink
Improved the workshop constructor to support unit testing
Browse files Browse the repository at this point in the history
Thanks to Tim Hunt for the hint.
  • Loading branch information
mudrd8mz committed Jan 4, 2010
1 parent 8a1ba8a commit 4efd7b5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
7 changes: 4 additions & 3 deletions mod/workshop/allocation/random/simpletest/testallocator.php
Expand Up @@ -68,10 +68,11 @@ class workshop_allocation_random_test extends UnitTestCase {
protected $allocator;

public function setUp() {
$cm = (object)array('id' => 3);
$course = (object)array('id' => 11);
$cm = new stdClass();
$course = new stdClass();
$context = new stdClass();
$workshop = (object)array('id' => 42);
$this->workshop = new workshop($workshop, $cm, $course);
$this->workshop = new workshop($workshop, $cm, $course, $context);
$this->allocator = new testable_workshop_random_allocator($this->workshop);
}

Expand Down
7 changes: 4 additions & 3 deletions mod/workshop/form/accumulative/simpletest/teststrategy.php
Expand Up @@ -67,10 +67,11 @@ public function setUp() {
$this->realDB = $DB;
$DB = new mockDB();

$cm = (object)array('id' => 3);
$course = (object)array('id' => 11);
$cm = new stdClass();
$course = new stdClass();
$context = new stdClass();
$workshop = (object)array('id' => 42, 'strategy' => 'accumulative');
$this->workshop = new workshop($workshop, $cm, $course);
$this->workshop = new workshop($workshop, $cm, $course, $context);
$this->strategy = new testable_workshop_accumulative_strategy($this->workshop);
}

Expand Down
7 changes: 4 additions & 3 deletions mod/workshop/form/numerrors/simpletest/teststrategy.php
Expand Up @@ -70,10 +70,11 @@ public function setUp() {
$this->realDB = $DB;
$DB = new mockDB();

$cm = (object)array('id' => 3);
$course = (object)array('id' => 11);
$cm = new stdClass();
$course = new stdClass();
$context = new stdClass();
$workshop = (object)array('id' => 42, 'strategy' => 'numerrors');
$this->workshop = new workshop($workshop, $cm, $course);
$this->workshop = new workshop($workshop, $cm, $course, $context);
$this->strategy = new testable_workshop_numerrors_strategy($this->workshop);
}

Expand Down
14 changes: 9 additions & 5 deletions mod/workshop/locallib.php
Expand Up @@ -80,18 +80,22 @@ class workshop {
* @param stdClass $dbrecord Workshop instance data from {workshop} table
* @param stdClass $cm Course module record as returned by {@link get_coursemodule_from_id()}
* @param stdClass $course Course record from {course} table
* @param stdClass $context The context of the workshop instance
*/
public function __construct(stdClass $dbrecord, stdClass $cm, stdClass $course) {
public function __construct(stdClass $dbrecord, stdClass $cm, stdClass $course, stdClass $context=null) {
foreach ($dbrecord as $field => $value) {
$this->{$field} = $value;
}
$this->evaluation = 'best'; // todo make this configurable
$this->cm = $cm;
$this->course = $course; // beware - this replaces the standard course field in the instance table
// this is intentional - IMO there should be no such field as it violates
// 3rd normal form with no real performance gain. This way I try to
// demonstrate how backwards compatibility could be achieved --mudrd8mz
$this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
// 3rd normal form with no real performance gain
if (is_null($context)) {
$this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
} else {
$this->context = $context;
}
$this->evaluation = 'best'; // todo make this configurable although we have no alternatives yet
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
File renamed without changes.

0 comments on commit 4efd7b5

Please sign in to comment.