Skip to content

Commit f7568f4

Browse files
Test create initial category (#907)
1 parent 8e73abc commit f7568f4

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/Document/CategoryManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public function getAllRootCategories(bool $loadChildren = true): array
102102
->execute();
103103
\assert(\is_array($rootCategories));
104104

105+
if ([] === $rootCategories) {
106+
return $this->getRootCategoriesForContext(null);
107+
}
108+
105109
$categories = [];
106110

107111
foreach ($rootCategories as $category) {

src/Entity/CategoryManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function getAllRootCategories(bool $loadChildren = true): array
103103
->getQuery()
104104
->getResult();
105105

106+
if ([] === $rootCategories) {
107+
return $this->getRootCategoriesForContext(null);
108+
}
109+
106110
$categories = [];
107111

108112
foreach ($rootCategories as $category) {

tests/Functional/Admin/CategoryAdminTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ public static function provideFormUrlsCases(): iterable
9292
yield 'Remove Category' => ['/admin/tests/app/category/1/delete', [], 'btn_delete'];
9393
}
9494

95+
public function testCreateFirstCategory(): void
96+
{
97+
$client = self::createClient();
98+
99+
$client->request('GET', '/admin/tests/app/category/create', [
100+
'uniqid' => 'category',
101+
]);
102+
103+
static::assertSame(1, $this->countCategories());
104+
105+
$client->submitForm('btn_create_and_list', [
106+
'category[name]' => 'Name',
107+
]);
108+
$client->followRedirect();
109+
110+
self::assertResponseIsSuccessful();
111+
112+
$client->request('GET', '/admin/tests/app/category/tree');
113+
114+
self::assertResponseIsSuccessful();
115+
static::assertSame(2, $this->countCategories());
116+
}
117+
95118
/**
96119
* @psalm-suppress UndefinedPropertyFetch
97120
*/
@@ -116,4 +139,18 @@ private function prepareData(): void
116139

117140
$manager->flush();
118141
}
142+
143+
/**
144+
* @psalm-suppress UndefinedPropertyFetch
145+
*/
146+
private function countCategories(): int
147+
{
148+
// TODO: Simplify this when dropping support for Symfony 4.
149+
// @phpstan-ignore-next-line
150+
$container = method_exists(static::class, 'getContainer') ? static::getContainer() : static::$container;
151+
$manager = $container->get('doctrine.orm.entity_manager');
152+
\assert($manager instanceof EntityManagerInterface);
153+
154+
return $manager->getRepository(Category::class)->count([]);
155+
}
119156
}

0 commit comments

Comments
 (0)