public function testEdit()
{
$client = $this->createClient();
$library = $this->dataLibrary->createEmpty();
$library->set('name', 'lib a');
$this->dataLibrary->create($library);
$entityBook = $this->dataBook->createEmpty();
$entityBook->set('title', 'titleA');
$entityBook->set('author', 'authorA');
$entityBook->set('pages', 111);
$entityBook->set('release', "2014-08-31");
$entityBook->set('library', $library->get('id'));
$this->dataBook->create($entityBook);
$crawler = $client->request('GET', '/crud/foo/' . $entityBook->get('id') . '/edit');
$this->assertTrue($client->getResponse()->isNotFound());
$this->assertCount(1, $crawler->filter('html:contains("Entity not found")'));
$crawler = $client->request('GET', '/crud/book/666/edit');
$this->assertTrue($client->getResponse()->isNotFound());
$this->assertCount(1, $crawler->filter('html:contains("Instance not found")'));
$crawler = $client->request('GET', '/crud/book/' . $entityBook->get('id') . '/edit');
$this->assertTrue($client->getResponse()->isOk());
$this->assertRegExp('/titleA/', $client->getResponse()->getContent());
$this->assertCount(1, $crawler->filter('html:contains("Submit")'));
$this->assertCount(1, $crawler->filter('html:contains("Author")'));
$this->assertCount(1, $crawler->filter('html:contains("Pages")'));
$crawler = $client->request('POST', '/crud/book/' . $entityBook->get('id') . '/edit');
$this->assertTrue($client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('html:contains("Could not edit, see the red marked fields.")'));
$this->assertRegExp('/has-error/', $client->getResponse()->getContent());
$file = __DIR__ . '/../test1.xml';
$crawler = $client->request('POST', '/crud/book/' . $entityBook->get('id') . '/edit', ['version' => 0, 'title' => 'titleEdited', 'author' => 'author', 'pages' => 111, 'price' => 3.99, 'library' => $library->get('id')], ['cover' => new UploadedFile($file, 'test1.xml', 'application/xml', filesize($file), null, true)]);
$this->assertTrue($client->getResponse()->isRedirect('/crud/book/' . $entityBook->get('id')));
$crawler = $client->followRedirect();
$this->assertCount(1, $crawler->filter('html:contains("Book edited with id ' . $entityBook->get('id') . '")'));
$bookEdited = $this->dataBook->get($entityBook->get('id'));
$this->assertSame($bookEdited->get('title'), 'titleEdited');
$this->fileProcessorHandle->createFile->never()->called();
$this->fileProcessorHandle->updateFile->once()->called();
$this->fileProcessorHandle->deleteFile->never()->called();
$this->fileProcessorHandle->renderFile->never()->called();
// Optimistic locking
$client->request('POST', '/crud/book/' . $entityBook->get('id') . '/edit', ['version' => 0, 'title' => 'titleEdited2', 'author' => 'author', 'pages' => 111, 'price' => 3.99, 'library' => $library->get('id')], ['cover' => new UploadedFile($file, 'test1.xml', 'application/xml', filesize($file), null, true)]);
$this->assertTrue($client->getResponse()->isOk());
$this->assertRegExp('/There was a more up to date version of the data available\\./', $client->getResponse()->getContent());
// Canceling events
$before = function (Entity $entity) {
return false;
};
$this->dataBook->pushEvent('before', 'update', $before);
$client->request('POST', '/crud/book/' . $entityBook->get('id') . '/edit', ['version' => 1, 'title' => 'titleEdited', 'author' => 'author', 'pages' => 111, 'price' => 3.99, 'library' => $library->get('id')], ['cover' => new UploadedFile($file, 'test1.xml', 'application/xml', filesize($file), null, true)]);
$this->assertTrue($client->getResponse()->isOk());
$this->assertRegExp('/Could not edit\\./', $client->getResponse()->getContent());
$this->dataBook->popEvent('before', 'update');
$this->dataBook->pushEvent('before', 'updateFiles', $before);
$client->request('POST', '/crud/book/' . $entityBook->get('id') . '/edit', ['version' => 1, 'title' => 'titleEdited', 'author' => 'author', 'pages' => 111, 'price' => 3.99, 'library' => $library->get('id')], ['cover' => new UploadedFile($file, 'test1.xml', 'application/xml', filesize($file), null, true)]);
$this->assertTrue($client->getResponse()->isOk());
$this->assertRegExp('/Could not edit\\./', $client->getResponse()->getContent());
$this->dataBook->popEvent('before', 'updateFiles');
}