private function getRepo() { $backend = new ArrayRepositoryBackend(new ModelConfig('Actor'), array(array('name' => 'Roger Moore'), array('name' => 'Arnold Schwarzenegger'))); $repo = new Repository(); $repo->registerBackend($backend); return $repo; }
/** * Elke test_* met een schone database beginnen. */ public function fillDatabase($db) { $db->import(dirname(__FILE__) . '/rebuild_test_database.' . $db->getAttribute(PDO::ATTR_DRIVER_NAME) . '.sql', $error); $repo = new Repository(); Repository::$instances[__CLASS__] = $repo; $backend = new DatabaseRepositoryBackend(array($this->dbLink)); foreach ($backend->configs as $config) { $config->class = SimpleRecord::class; } $repo->registerBackend($backend); }
public function test_many_to_many_relation_with_mapped_fields() { $repo = new Repository(); $backend = new DatabaseRepositoryBackend(array($this->dbLink)); $repo->registerBackend($backend); $backend->configs['Customer']->hasMany['ratings']['fields']['rating'] = 'groupRating'; $bob = $repo->getCustomer(1); // Reading $this->assertCount(1, $bob->ratings); $groupRating = $bob->ratings[0]; $this->assertEquals('Hacker', $groupRating->title); // Access normal property $this->assertEquals('5', $groupRating->groupRating); // Access additional property $this->assertInstanceOf(Junction::class, $groupRating); // Updating $this->assertCount(1, $bob->ratings); $groupRating->groupRating = '4'; // Using a string because an int would change detection when saving the $group $repo->saveCustomer($bob); $this->assertLastQuery("UPDATE ratings SET rating = '4' WHERE customer_id = 1 AND group_id = 1"); $group = $repo->getGroup($groupRating->id); $this->assertCount(2, $group->ratings->toArray()); $this->assertLastQuery('SELECT * FROM customers WHERE id IN (1, 2)'); // The many to many for the group was't yet loaded. $this->assertEquals('Bob Fanger', $group->ratings[0]->name, 'Sanity check'); $this->assertEquals(4, $group->ratings[0]->rating); $this->assertRelativeQueryCount(6, 'Sanity check'); $repo->saveGroup($group); $this->assertRelativeQueryCount(6, '0 changes, 0 queries.'); $group->ratings[0]->rating = 10; $repo->saveGroup($group); $this->assertQuery('UPDATE ratings SET rating = 10 WHERE customer_id = 1 AND group_id = 1'); $this->assertRelativeQueryCount(7); $this->assertEquals(10, $bob->ratings[0]->groupRating, 'The many-to-many relation should be updated on both ends'); // Deleting unset($group->ratings[0]); $repo->saveGroup($group); $this->assertLastQuery('DELETE FROM ratings WHERE customer_id = 1 AND group_id = 1'); $this->assertRelativeQueryCount(8); $this->assertCount(0, $bob->ratings, 'The many-to-many relation should be updated on both ends'); }
/** * Get a Customer instance where all the properties are still placeholders * (Slow/Expensive operation, initializes a new Repository on every call). * * @param string $id * * @return stdClass */ private function getDirtyCustomer($id) { $repo = new Repository(); $repo->registerBackend(new DatabaseRepositoryBackend($this->dbLink)); return $repo->getCustomer($id); }