public function test_update_and_delete() { $x = new Weapon(); $x->characterId = $this->char->characterId; $wpns = array('long sword', 'short sword', 'testing'); $createdList = array(); foreach ($wpns as $n) { $xData = array('character_id' => $x->characterId, 'weapon_name' => $n); $id = $x->create($this->dbObj, $xData); $this->assertTrue(is_numeric($id)); $this->assertTrue($id > 0); $this->assertFalse(isset($createdList[$id])); $data = $x->load($this->dbObj); $this->assertTrue(is_array($data)); $this->assertTrue(count($data) > count($xData)); $createdList[$id] = $data; } $allWpns = $x->get_all($this->dbObj, $x->characterId); $this->assertTrue(is_array($allWpns)); $this->assertEquals(count($createdList), count($allWpns)); foreach ($createdList as $id => $data) { $x->id = $id; $newData = $data; $newData['weapon_name'] .= __METHOD__; $this->assertNotEquals($data, $newData); $this->assertNull($x->mass_update($newData)); $this->assertEquals(1, $x->save($this->dbObj)); $this->assertEquals($newData, $x->load($this->dbObj)); $this->assertEquals($id, $x->id); $this->assertEquals(1, $x->delete($this->dbObj)); } $this->assertEquals(array(), $x->get_all($this->dbObj, $x->characterId)); }
public function test_get_total_weight() { $x = new CharacterSheet($this->dbObj, __METHOD__, 1); $this->assertTrue(is_numeric($x->characterId), ToolBox::debug_print($x)); $this->assertEquals(0, $x->get_total_weight(false)); $this->assertEquals($x->get_total_weight(true), $x->get_total_weight(false)); //now create some gear. $manualWeight = 0; $itemList = array(); $createThis = array(array('torches', 1, 10), array('lead nuggets', 10, 10), array('misc', 4, 200)); $g = new Gear(); $g->characterId = $x->characterId; foreach ($createThis as $data) { $manualWeight += round($data[1] * $data[2], 1); $xData = array('character_id' => $x->characterId, 'gear_name' => $data[0], 'weight' => $data[1], 'quantity' => $data[2]); $id = $g->create($this->dbObj, $xData); $itemList[$id] = $xData; } $this->assertEquals($manualWeight, Gear::calculate_list_weight($itemList)); //now, at first, this should be 0 because we haven't re-loaded the sheet. $this->assertEquals(0, $x->get_total_weight(false)); $this->assertEquals(0, $x->get_total_weight(true)); $x->load(); $this->assertEquals($manualWeight, $x->get_total_weight(false)); $this->assertEquals($manualWeight, $x->get_total_weight(true)); $withWeapons = $manualWeight; $w = new Weapon(); $w->characterId = $x->characterId; $wpns = array('great_sword' => 5, 'long_sword' => 2, 'short_sword' => 1); foreach ($wpns as $name => $wgt) { $xData = array('character_id' => $x->characterId, 'weapon_name' => $name, 'weight' => $wgt); $id = $w->create($this->dbObj, $xData); $itemList[$id] = $xData; $withWeapons += $wgt; } $x->load(); $this->assertNotEquals($manualWeight, $withWeapons); $this->assertTrue($withWeapons > $manualWeight); $this->assertEquals($manualWeight, $x->get_total_weight(false)); $this->assertEquals($x->get_total_weight(), $x->get_total_weight(false)); $this->assertEquals($withWeapons, $x->get_total_weight(true)); $withArmor = $withWeapons; $a = new Armor(); $rmr = array('big' => 10, 'small' => 1); foreach ($rmr as $name => $wgt) { $xData = array('character_id' => $x->characterId, 'armor_name' => $name, 'weight' => $wgt); $id = $a->create($this->dbObj, $xData); $itemList[$id] = $xData; $withArmor += $wgt; } $x->load(); $this->assertNotEquals($manualWeight, $withArmor); $this->assertTrue($withArmor > $withWeapons); $this->assertEquals($manualWeight, $x->get_total_weight(false)); $this->assertEquals($x->get_total_weight(), $x->get_total_weight(false)); $this->assertEquals($withArmor, $x->get_total_weight(true)); }
public function handle_new_record($type, $name, array $extraData = null) { if (is_null($extraData) || !is_array($extraData)) { $extraData = array(); } $extraData['character_id'] = $this->characterId; $log = new cs_webdblogger($this->dbObj, "Character"); switch ($type) { case Weapon::sheetIdPrefix: $x = new Weapon(); $extraData['weapon_name'] = $name; $result = $x->create($this->dbObj, $extraData); break; case Armor::sheetIdPrefix: $x = new Armor(); $extraData['armor_name'] = $name; $result = $x->create($this->dbObj, $extraData); break; case SpecialAbility::sheetIdPrefix: $x = new SpecialAbility(); $extraData['special_ability_name'] = $name; $result = $x->create($this->dbObj, $extraData); break; case Gear::sheetIdPrefix: $x = new Gear(); $extraData['gear_name'] = $name; $result = $x->create($this->dbObj, $extraData); break; case Skill::sheetIdPrefix: $x = new Skill(); $extraData['skill_name'] = $name; $result = $x->create($this->dbObj, $extraData); break; default: $details = __METHOD__ . ": invalid type (" . $type . ")"; $log->log_by_class($details, "exception in code"); throw new InvalidArgumentException($details); } $details = "New record, type=(" . $type . "), name=(" . $name . ")"; if (is_array($details) && count($extraData) > 0) { $details .= implode(", ", $extraData); } $log->log_by_class($details, "create"); return $result; }