/** * Tests association mapping, using various relationship types. * * @dataProvider sampleAssociationData * * @param string $association Association name. * @param string $entityType Entity type of the association. * @param mixed $entityIds ID(s) to populate the association with */ public function testAssociationMapping($association, $entityType, $entityIds) { $collectionAssociation = is_array($entityIds); // establish the expected result // NOTE: We do this here, rather than in the data provider, as we rely on the mock entities having // been populated in the createMockEntities() call, which is executed after the data provider. if ($collectionAssociation) { $expected = array(); foreach ($entityIds as $individualId) { $expected[] = self::getMockEntity($entityType, $individualId); } } else { $expected = self::getMockEntity($entityType, $entityIds); } // get the actual result $data = array($association => $entityIds); $resource = new EmployeeEntity(); $this->mapper->map($resource, $data); $mapped = $resource->{$association}; // check the mapping was correct $this->assertEquals(gettype($expected), gettype($mapped)); if ($collectionAssociation) { foreach ($mapped as $index => $item) { $this->assertEntityEquals($expected[$index], $item); } } else { $this->assertEntityEquals($expected, $mapped); } }
/** * Updates a single resource entity. The supplied resource can either be * a Doctrine entity object or an identifier reference. * * @param mixed $resource Either an entity or an identifier. * @param array $data If $resource is not an entity, contains the new data to populate. * * @throws \BedRest\Model\Doctrine\Exception * @return object */ public function update($resource, array $data = array()) { if (!$resource instanceof $this->resourceClassName) { $resource = $this->get($resource); $this->dataMapper->map($resource, $data); } if (!$this->validate($resource)) { throw new Exception("Invalid data supplied."); } $this->entityManager->persist($resource); $this->entityManager->flush(); return $resource; }