Lemon\RestBundle\Tests\Controller\ResourceControllerTest::testPutActionWithNestedCollectionAndRemoveAllExistingItems PHP Method

testPutActionWithNestedCollectionAndRemoveAllExistingItems() public method

    public function testPutActionWithNestedCollectionAndRemoveAllExistingItems()
    {
        $person = new Person();
        $person->name = "Stan Lemon";
        $car1 = new Car();
        $car1->name = 'Honda';
        $car1->year = 2006;
        $car1->person = $person;
        $person->cars->add($car1);
        $car2 = new Car();
        $car2->name = 'Mercedes Benz';
        $car2->year = 2013;
        $car2->person = $person;
        $person->cars->add($car2);
        $this->em->persist($person);
        $this->em->flush($person);
        $this->em->clear();
        $refresh = $this->em->getRepository('Lemon\\RestBundle\\Tests\\Fixtures\\Person')->findOneBy(array('id' => $person->id));
        $this->assertCount(2, $refresh->cars);
        $this->em->clear();
        $request = $this->makeRequest('PUT', '/person/' . $person->id, json_encode(array('name' => $person->name, 'cars' => array())));
        /** @var \Symfony\Component\HttpFoundation\Response $response */
        $response = $this->controller->putAction($request, 'person', $person->id);
        $data = json_decode($response->getContent());
        $refresh = $this->em->getRepository('Lemon\\RestBundle\\Tests\\Fixtures\\Person')->findOneBy(array('id' => $data->id));
        $this->assertNotNull($refresh);
        $this->assertEquals($person->id, $refresh->id);
        $this->assertEquals($person->name, $refresh->name);
        $this->assertCount(0, $refresh->cars);
    }