public function testPostActionWithNestedCollection()
{
$tag = new Tag();
$tag->name = 'baz';
$this->em->persist($tag);
$this->em->flush($tag);
$this->em->clear();
$request = $this->makeRequest('POST', '/person', json_encode(array('name' => 'Stan Lemon', 'cars' => array(array('name' => 'Honda', 'year' => 2006)), 'tags' => array(array('name' => 'foo'), array('name' => 'bar'), array('id' => $tag->id, 'name' => 'baz')))));
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $this->controller->postAction($request, 'person');
$this->em->clear();
$data = json_decode($response->getContent());
$this->assertEquals($data->name, "Stan Lemon");
$refresh = $this->em->getRepository('Lemon\\RestBundle\\Tests\\Fixtures\\Person')->findOneBy(array('id' => $data->id));
$this->assertNotNull($refresh);
$this->assertEquals("Stan Lemon", $refresh->name);
$this->assertCount(1, $refresh->cars);
$this->assertEquals("Honda", $refresh->cars[0]->name);
$this->assertEquals(2006, $refresh->cars[0]->year);
$this->assertCount(3, $refresh->tags);
$foundExisting = false;
foreach ($refresh->tags as $t) {
if ($t->id == $tag->id) {
$foundExisting = true;
}
}
$this->assertTrue($foundExisting, "Found existing tag on refreshed entity");
}