Skip to content

Commit

Permalink
Fix assertions in tests (#7831)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored May 24, 2022
1 parent 0bba538 commit a439577
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 29 deletions.
6 changes: 6 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<code>new Datagrid($this-&gt;query, $this-&gt;columns, $pager, $this-&gt;formBuilder, [])</code>
</InvalidArgument>
</file>
<!-- https://github.com/vimeo/psalm/issues/7896 -->
<file src="src/Menu/Provider/GroupMenuProvider.php">
<PossiblyUndefinedArrayOffset occurrences="1">
<code>$item['route']</code>
</PossiblyUndefinedArrayOffset>
</file>
</files>
8 changes: 4 additions & 4 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,11 @@ public function historyAction(Request $request): Response
{
$object = $this->assertObjectExists($request, true);
\assert(null !== $object);
$this->admin->checkAccess('history', $object);

$objectId = $this->admin->getNormalizedIdentifier($object);
\assert(null !== $objectId);

$this->admin->checkAccess('history', $object);

$manager = $this->container->get('sonata.admin.audit.manager');
\assert($manager instanceof AuditManagerInterface);

Expand Down Expand Up @@ -719,11 +719,11 @@ public function historyViewRevisionAction(Request $request, string $revision): R
{
$object = $this->assertObjectExists($request, true);
\assert(null !== $object);
$this->admin->checkAccess('historyViewRevision', $object);

$objectId = $this->admin->getNormalizedIdentifier($object);
\assert(null !== $objectId);

$this->admin->checkAccess('historyViewRevision', $object);

$manager = $this->container->get('sonata.admin.audit.manager');
\assert($manager instanceof AuditManagerInterface);

Expand Down
27 changes: 15 additions & 12 deletions src/Filter/Model/FilterData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class FilterData
{
private ?int $type = null;
private ?int $type;

/**
* @var mixed
Expand All @@ -27,9 +27,17 @@ final class FilterData

private bool $hasValue;

private function __construct()
/**
* @param mixed $value
*/
private function __construct(?int $type, bool $hasValue, $value = null)
{
$this->hasValue = false;
$this->type = $type;
$this->hasValue = $hasValue;

if ($hasValue) {
$this->value = $value;
}
}

/**
Expand All @@ -39,8 +47,6 @@ private function __construct()
*/
public static function fromArray(array $data): self
{
$filterData = new self();

if (isset($data['type'])) {
if (!\is_int($data['type']) && (!\is_string($data['type']) || !is_numeric($data['type']))) {
throw new \InvalidArgumentException(sprintf(
Expand All @@ -49,15 +55,12 @@ public static function fromArray(array $data): self
));
}

$filterData->type = (int) $data['type'];
}

if (\array_key_exists('value', $data)) {
$filterData->value = $data['value'];
$filterData->hasValue = true;
$type = (int) $data['type'];
} else {
$type = null;
}

return $filterData;
return new self($type, \array_key_exists('value', $data), $data['value'] ?? null);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Menu/Provider/GroupMenuProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ private function generateMenuItem(array $item, array $group): ItemInterface
}

\assert(isset($item['label']));
\assert(isset($item['route']));

return $this->menuFactory->createItem($item['label'], [
'route' => $item['route'],
Expand Down
28 changes: 20 additions & 8 deletions tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2199,22 +2199,34 @@ public function testGetCurrentLeafChildAdmin(): void

// Workaround for static analysis
$postAdminChildAdmin = $postAdmin->getCurrentLeafChildAdmin();
$commentAdminChildAdmin = $commentAdmin->getCurrentLeafChildAdmin();
$commentVoteAdminChildAdmin = $commentVoteAdmin->getCurrentLeafChildAdmin();

static::assertNull($postAdminChildAdmin);
static::assertNull($commentAdmin->getCurrentLeafChildAdmin());
static::assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
static::assertNull($commentAdminChildAdmin);
static::assertNull($commentVoteAdminChildAdmin);

$commentAdmin->setCurrentChild(true);

static::assertSame($commentAdmin, $postAdmin->getCurrentLeafChildAdmin());
static::assertNull($commentAdmin->getCurrentLeafChildAdmin());
static::assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
// Workaround for static analysis
$postAdminChildAdmin = $postAdmin->getCurrentLeafChildAdmin();
$commentAdminChildAdmin = $commentAdmin->getCurrentLeafChildAdmin();
$commentVoteAdminChildAdmin = $commentVoteAdmin->getCurrentLeafChildAdmin();

static::assertSame($commentAdmin, $postAdminChildAdmin);
static::assertNull($commentAdminChildAdmin);
static::assertNull($commentVoteAdminChildAdmin);

$commentVoteAdmin->setCurrentChild(true);

static::assertSame($commentVoteAdmin, $postAdmin->getCurrentLeafChildAdmin());
static::assertSame($commentVoteAdmin, $commentAdmin->getCurrentLeafChildAdmin());
static::assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
// Workaround for static analysis
$postAdminChildAdmin = $postAdmin->getCurrentLeafChildAdmin();
$commentAdminChildAdmin = $commentAdmin->getCurrentLeafChildAdmin();
$commentVoteAdminChildAdmin = $commentVoteAdmin->getCurrentLeafChildAdmin();

static::assertSame($commentVoteAdmin, $postAdminChildAdmin);
static::assertSame($commentVoteAdmin, $commentAdminChildAdmin);
static::assertNull($commentVoteAdminChildAdmin);
}

public function testAdminAvoidInfiniteLoop(): void
Expand Down
63 changes: 59 additions & 4 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,11 @@ public function testEditAction(): void
->method('getForm')
->willReturn($form);

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$form
Expand All @@ -1453,7 +1458,7 @@ public function testEditAction(): void
'action' => 'edit',
'form' => $formView,
'object' => $object,
'objectId' => null,
'objectId' => 'foo_normalized',
]);

static::assertInstanceOf(Response::class, $this->controller->editAction($this->request));
Expand Down Expand Up @@ -1491,6 +1496,11 @@ public function testEditActionSuccess(string $expectedToStringValue, string $toS
->with(static::equalTo('edit'))
->willReturn(true);

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$form->expects(static::once())
Expand Down Expand Up @@ -1542,6 +1552,11 @@ public function testEditActionError(string $expectedToStringValue, string $toStr
->method('checkAccess')
->with(static::equalTo('edit'));

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$this->admin->expects(static::once())
Expand Down Expand Up @@ -1580,7 +1595,7 @@ public function testEditActionError(string $expectedToStringValue, string $toStr
'action' => 'edit',
'form' => $formView,
'object' => $object,
'objectId' => null,
'objectId' => 'foo_normalized',
]);

static::assertInstanceOf(Response::class, $this->controller->editAction($this->request));
Expand Down Expand Up @@ -1658,6 +1673,11 @@ public function testEditActionAjaxError(): void
->method('checkAccess')
->with(static::equalTo('edit'));

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$this->admin->expects(static::once())
Expand Down Expand Up @@ -1706,6 +1726,11 @@ public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('checkAccess')
->with(static::equalTo('edit'));

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$this->admin->expects(static::once())
Expand Down Expand Up @@ -1753,6 +1778,11 @@ public function testEditActionWithModelManagerException(string $expectedToString
->method('getClass')
->willReturn(\stdClass::class);

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$this->admin->expects(static::once())
Expand Down Expand Up @@ -1796,7 +1826,7 @@ public function testEditActionWithModelManagerException(string $expectedToString
'action' => 'edit',
'form' => $formView,
'object' => $object,
'objectId' => null,
'objectId' => 'foo_normalized',
]);

static::assertInstanceOf(Response::class, $this->controller->editAction($this->request));
Expand All @@ -1817,6 +1847,11 @@ public function testEditActionWithPreview(): void
->method('checkAccess')
->with(static::equalTo('edit'));

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$this->admin->expects(static::once())
Expand Down Expand Up @@ -1855,7 +1890,7 @@ public function testEditActionWithPreview(): void
'action' => 'edit',
'form' => $formView,
'object' => $object,
'objectId' => null,
'objectId' => 'foo_normalized',
]);

static::assertInstanceOf(Response::class, $this->controller->editAction($this->request));
Expand All @@ -1881,6 +1916,11 @@ public function testEditActionWithLockException(): void
->method('getClass')
->willReturn($class);

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

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

$form
Expand Down Expand Up @@ -2606,6 +2646,11 @@ public function testHistoryActionNoReader(): void
->method('getClass')
->willReturn('Foo');

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

$this->auditManager->expects(static::once())
->method('hasReader')
->with(static::equalTo('Foo'))
Expand Down Expand Up @@ -3041,6 +3086,11 @@ public function testHistoryViewRevisionActionNoReader(): void
->method('getClass')
->willReturn('Foo');

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

$this->auditManager->expects(static::once())
->method('hasReader')
->with(static::equalTo('Foo'))
Expand Down Expand Up @@ -3221,6 +3271,11 @@ public function testHistoryCompareRevisionsActionNoReader(): void
->method('getClass')
->willReturn('Foo');

$this->admin
->method('getNormalizedIdentifier')
->with(static::equalTo($object))
->willReturn('foo_normalized');

$this->auditManager->expects(static::once())
->method('hasReader')
->with(static::equalTo('Foo'))
Expand Down

0 comments on commit a439577

Please sign in to comment.