Skip to content

Commit

Permalink
Add runtime exception (#5198)
Browse files Browse the repository at this point in the history
Fixed #5195
  • Loading branch information
sir-kain authored and greg0ire committed Sep 9, 2018
1 parent 0971174 commit 746d068
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 10 deletions.
31 changes: 28 additions & 3 deletions src/Controller/CRUDController.php
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Exception\LockException;
use Sonata\AdminBundle\Exception\ModelManagerException;
Expand Down Expand Up @@ -332,13 +333,15 @@ public function editAction($id = null)
$this->admin->setSubject($existingObject);
$objectId = $this->admin->getNormalizedIdentifier($existingObject);

if (!\is_array($fields = $this->admin->getForm()->all()) || 0 === \count($fields)) {
$form = $this->admin->getForm();
\assert($form instanceof Form);

if (!\is_array($fields = $form->all()) || 0 === \count($fields)) {
throw new \RuntimeException(
'No editable field defined. Did you forget to implement the "configureFormFields" method?'
);
}

$form = $this->admin->getForm();
$form->setData($existingObject);
$form->handleRequest($request);

Expand Down Expand Up @@ -557,6 +560,7 @@ public function batchAction()
* Create action.
*
* @throws AccessDeniedException If access is not granted
* @throws \RuntimeException If no editable field is defined
*
* @return Response
*/
Expand Down Expand Up @@ -592,6 +596,14 @@ public function createAction()
$this->admin->setSubject($newObject);

$form = $this->admin->getForm();
\assert($form instanceof Form);

if (!\is_array($fields = $form->all()) || 0 === \count($fields)) {
throw new \RuntimeException(
'No editable field defined. Did you forget to implement the "configureFormFields" method?'
);
}

$form->setData($newObject);
$form->handleRequest($request);

Expand Down Expand Up @@ -699,14 +711,27 @@ public function showAction($id = null)

$this->admin->setSubject($object);

$fields = $this->admin->getShow();
\assert($fields instanceof FieldDescriptionCollection);

// NEXT_MAJOR: replace deprecation with exception
if (!\is_array($fields->getElements()) || 0 === $fields->count()) {
@trigger_error(
'Calling this method without implementing "configureShowFields"'
.' is not supported since 3.x'
.' and will no longer be possible in 4.0',
E_USER_DEPRECATED
);
}

// NEXT_MAJOR: Remove this line and use commented line below it instead
$template = $this->admin->getTemplate('show');
//$template = $this->templateRegistry->getTemplate('show');

return $this->renderWithExtraParams($template, [
'action' => 'show',
'object' => $object,
'elements' => $this->admin->getShow(),
'elements' => $fields,
], null);
}

Expand Down
114 changes: 107 additions & 7 deletions tests/Controller/CRUDControllerTest.php
Expand Up @@ -833,6 +833,40 @@ public function testShowActionAccessDenied()
$this->controller->showAction(null, $this->request);
}

/**
* @group legacy
* @expectedDeprecation Calling this method without implementing "configureShowFields" is not supported since 3.x and will no longer be possible in 4.0
*/
public function testShowActionDeprecation()
{
$object = new \stdClass();

$this->admin->expects($this->once())
->method('getObject')
->will($this->returnValue($object));

$this->admin->expects($this->once())
->method('checkAccess')
->with($this->equalTo('show'))
->will($this->returnValue(true));

$show = $this->createMock(FieldDescriptionCollection::class);

$this->admin->expects($this->once())
->method('getShow')
->will($this->returnValue($show));

$show->expects($this->once())
->method('getElements')
->willReturn([]);

$show->expects($this->once())
->method('count')
->willReturn(0);

$this->controller->showAction(null, $this->request);
}

public function testPreShow()
{
$object = new \stdClass();
Expand Down Expand Up @@ -874,6 +908,10 @@ public function testShowAction()
->method('getShow')
->will($this->returnValue($show));

$show->expects($this->once())
->method('getElements')
->willReturn(['field' => 'fielddata']);

$this->assertInstanceOf(Response::class, $this->controller->showAction(null, $this->request));

$this->assertSame($this->admin, $this->parameters['admin']);
Expand Down Expand Up @@ -1510,7 +1548,7 @@ public function testEditAction()

$form = $this->createMock(Form::class);

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1572,7 +1610,7 @@ public function testEditActionSuccess($expectedToStringValue, $toStringValue)
->method('getData')
->will($this->returnValue($object));

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1622,7 +1660,7 @@ public function testEditActionError($expectedToStringValue, $toStringValue)

$form = $this->createMock(Form::class);

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1686,7 +1724,7 @@ public function testEditActionAjaxSuccess()

$form = $this->createMock(Form::class);

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1740,7 +1778,7 @@ public function testEditActionAjaxError()

$form = $this->createMock(Form::class);

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1801,7 +1839,7 @@ public function testEditActionWithModelManagerException($expectedToStringValue,

$form = $this->createMock(Form::class);

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1865,7 +1903,7 @@ public function testEditActionWithPreview()

$form = $this->createMock(Form::class);

$this->admin->expects($this->exactly(2))
$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

Expand Down Expand Up @@ -1985,6 +2023,36 @@ public function testCreateActionAccessDenied()
$this->controller->createAction($this->request);
}

public function testCreateActionRuntimeException()
{
$this->expectException(\RuntimeException::class);

$this->admin->expects($this->once())
->method('checkAccess')
->with($this->equalTo('create'))
->will($this->returnValue(true));

$this->admin->expects($this->any())
->method('getClass')
->will($this->returnValue('stdClass'));

$this->admin->expects($this->once())
->method('getNewInstance')
->will($this->returnValue(new \stdClass()));

$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn([]);

$this->controller->createAction($this->request);
}

public function testPreCreate()
{
$object = new \stdClass();
Expand Down Expand Up @@ -2034,6 +2102,10 @@ public function testCreateAction()
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$formView = $this->createMock(FormView::class);

$form->expects($this->any())
Expand Down Expand Up @@ -2107,6 +2179,10 @@ public function testCreateActionSuccess($expectedToStringValue, $toStringValue)
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$form->expects($this->once())
->method('isSubmitted')
->will($this->returnValue(true));
Expand Down Expand Up @@ -2168,6 +2244,10 @@ public function testCreateActionAccessDenied2()
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$form->expects($this->once())
->method('isSubmitted')
->will($this->returnValue(true));
Expand Down Expand Up @@ -2211,6 +2291,10 @@ public function testCreateActionError($expectedToStringValue, $toStringValue)
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$form->expects($this->once())
->method('isSubmitted')
->will($this->returnValue(true));
Expand Down Expand Up @@ -2274,6 +2358,10 @@ public function testCreateActionWithModelManagerException($expectedToStringValue
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$form->expects($this->once())
->method('isValid')
->will($this->returnValue(true));
Expand Down Expand Up @@ -2349,6 +2437,10 @@ public function testCreateActionAjaxSuccess()
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$form->expects($this->once())
->method('isSubmitted')
->will($this->returnValue(true));
Expand Down Expand Up @@ -2403,6 +2495,10 @@ public function testCreateActionAjaxError()
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$form->expects($this->once())
->method('isSubmitted')
->will($this->returnValue(true));
Expand Down Expand Up @@ -2457,6 +2553,10 @@ public function testCreateActionWithPreview()
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->admin->expects($this->once())
->method('supportsPreviewMode')
->will($this->returnValue(true));
Expand Down

0 comments on commit 746d068

Please sign in to comment.