Skip to content

Commit

Permalink
Test save callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead committed Jan 12, 2022
1 parent 873e440 commit d960754
Showing 1 changed file with 76 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,11 @@ public function testAddRootPagesToFieldConfig(): void
$requestStack = new RequestStack();
$requestStack->push($request);

$result = $this->createMock(Result::class);
$result
->expects($this->once())
->method('fetchAllAssociative')
->willReturn([
['id' => 1, 'title' => 'title-1', 'language' => 'language-1'],
['id' => 2, 'title' => 'title-2', 'language' => 'language-2'],
['id' => 3, 'title' => 'title-3', 'language' => 'language-3'],
])
;

$statement = $this->createMock(Statement::class);
$statement
->expects($this->once())
->method('executeQuery')
->willReturn($result)
;

$connection = $this->createMock(Connection::class);
$connection
->expects($this->once())
->method('prepare')
->willReturn($statement)
;
$connection = $this->mockGetRootPages([
['id' => 1, 'title' => 'title-1', 'language' => 'language-1'],
['id' => 2, 'title' => 'title-2', 'language' => 'language-2'],
['id' => 3, 'title' => 'title-3', 'language' => 'language-3'],
]);

$listener = new RootPageDependentSelectListener(
$connection,
Expand Down Expand Up @@ -247,6 +228,51 @@ public function testAddWizardToSelectWhenModuleIsSet(): void
$this->assertCount(3, unserialize($listener->wizardCallback($dataContainer)));
}

public function testDoesNotSaveUnserializableData(): void
{
$dataContainer = $this->mockClassWithProperties(DataContainer::class);

$listener = new RootPageDependentSelectListener(
$this->createMock(Connection::class),
$this->createMock(TranslatorInterface::class),
$this->createMock(ScopeMatcher::class),
$this->createMock(RequestStack::class),
$this->createMock(CsrfTokenManagerInterface::class),
'contao_csrf_token'
);

$this->assertSame('foobar', $listener->saveCallback('foobar', $dataContainer));
}

public function testSavesValuesRelatedToRootPage(): void
{
$dataContainer = $this->mockClassWithProperties(DataContainer::class);

$connection = $this->mockGetRootPages([
['id' => 1, 'title' => 'title-1', 'language' => 'language-1'],
['id' => 2, 'title' => 'title-2', 'language' => 'language-2'],
['id' => 3, 'title' => 'title-3', 'language' => 'language-3'],
]);

$listener = new RootPageDependentSelectListener(
$connection,
$this->createMock(TranslatorInterface::class),
$this->createMock(ScopeMatcher::class),
$this->createMock(RequestStack::class),
$this->createMock(CsrfTokenManagerInterface::class),
'contao_csrf_token'
);

$this->assertSame(
serialize([
1 => 10,
2 => 20,
3 => 30,
]),
$listener->saveCallback(serialize([10, 20, 30]), $dataContainer)
);
}

private function populateGlobalsArray(array $data): void
{
$GLOBALS['TL_DCA']['tl_module']['fields'] = $data;
Expand Down Expand Up @@ -277,4 +303,30 @@ private function mockScopeMatcher(bool $hasBackendUser, Request $request): Scope

return $scopeMatcher;
}

private function mockGetRootPages(array $data): Connection
{
$result = $this->createMock(Result::class);
$result
->expects($this->once())
->method('fetchAllAssociative')
->willReturn($data)
;

$statement = $this->createMock(Statement::class);
$statement
->expects($this->once())
->method('executeQuery')
->willReturn($result)
;

$connection = $this->createMock(Connection::class);
$connection
->expects($this->once())
->method('prepare')
->willReturn($statement)
;

return $connection;
}
}

0 comments on commit d960754

Please sign in to comment.