Skip to content

Commit 1182bf8

Browse files
committed
Added fixes to Category after writing form-based tests
1 parent b824699 commit 1182bf8

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed

Entity/Category.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ public function __construct()
9898

9999
public function getName(): string
100100
{
101-
return $this->name;
101+
return (string) $this->name;
102102
}
103103

104-
public function setName(string $name): void
104+
public function setName(?string $name): void
105105
{
106106
$this->name = $name;
107107
}
@@ -111,37 +111,37 @@ public function getDescription(): ?string
111111
return $this->description;
112112
}
113113

114-
public function setDescription(string $description = null): void
114+
public function setDescription(?string $description): void
115115
{
116116
$this->description = $description;
117117
}
118118

119119
public function getSlug(): string
120120
{
121-
return $this->slug;
121+
return (string) $this->slug;
122122
}
123123

124-
public function setSlug(string $slug = null): void
124+
public function setSlug(?string $slug): void
125125
{
126-
$this->slug = $slug;
126+
$this->slug = (string) $slug;
127127
}
128128

129129
public function isEnabled(): bool
130130
{
131131
return $this->enabled;
132132
}
133133

134-
public function setEnabled(bool $enabled = false): void
134+
public function setEnabled(?bool $enabled = false): void
135135
{
136-
$this->enabled = $enabled;
136+
$this->enabled = (bool) $enabled;
137137
}
138138

139139
public function getParent(): ?Category
140140
{
141141
return $this->parent;
142142
}
143143

144-
public function setParent(Category $parent = null): void
144+
public function setParent(?Category $parent): void
145145
{
146146
if ($parent === $this) {
147147
// Refuse the category to have itself as parent.

Tests/Entity/CategoryTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
use Orbitale\Bundle\CmsBundle\Tests\AbstractTestCase;
1616
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Category;
1717
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
18+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
19+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
20+
use Symfony\Component\Form\Extension\Core\Type\FormType;
21+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
22+
use Symfony\Component\Form\FormBuilderInterface;
23+
use Symfony\Component\Form\FormFactoryInterface;
1824

1925
class CategoryTest extends AbstractTestCase
2026
{
@@ -161,4 +167,37 @@ public function testCategorySlugIsNotTransliteratedIfEmpty()
161167

162168
static::assertEquals(null, $category->getSlug());
163169
}
170+
171+
public function testSuccessfulFormSubmissionWithEmptyData()
172+
{
173+
static::bootKernel();
174+
175+
$category = new Category();
176+
177+
/** @var FormBuilderInterface $builder */
178+
$builder = static::$container->get(FormFactoryInterface::class)->createBuilder(FormType::class, $category);
179+
$builder
180+
->add('name')
181+
->add('slug')
182+
->add('description', TextareaType::class)
183+
->add('parent', EntityType::class, ['class' => Category::class])
184+
->add('enabled', CheckboxType::class)
185+
;
186+
187+
$form = $builder->getForm();
188+
189+
$form->submit([
190+
'name' => null,
191+
'slug' => null,
192+
'description' => null,
193+
'parent' => null,
194+
'enabled' => null,
195+
]);
196+
197+
static::assertSame('', $category->getName());
198+
static::assertSame('', $category->getSlug());
199+
static::assertNull($category->getDescription());
200+
static::assertNull($category->getParent());
201+
static::assertFalse($category->isEnabled());
202+
}
164203
}

Tests/Entity/PageTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
use Doctrine\ORM\EntityManager;
1515
use Orbitale\Bundle\CmsBundle\Tests\AbstractTestCase;
1616
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
17+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
18+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
19+
use Symfony\Component\Form\Extension\Core\Type\FormType;
20+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
21+
use Symfony\Component\Form\FormBuilderInterface;
22+
use Symfony\Component\Form\FormFactoryInterface;
1723

1824
class PageTest extends AbstractTestCase
1925
{
@@ -141,4 +147,60 @@ public function testPageSlugIsTransliterated()
141147

142148
static::assertEquals('default-page', $page->getSlug());
143149
}
150+
151+
public function testSuccessfulFormSubmissionWithEmptyData()
152+
{
153+
static::bootKernel();
154+
155+
$page = new Page();
156+
157+
/** @var FormBuilderInterface $builder */
158+
$builder = static::$container->get(FormFactoryInterface::class)->createBuilder(FormType::class, $page);
159+
$builder
160+
->add('title')
161+
->add('slug')
162+
->add('metaDescription')
163+
->add('metaTitle')
164+
->add('metaKeywords')
165+
->add('host')
166+
->add('content', TextareaType::class)
167+
->add('css', TextareaType::class)
168+
->add('js', TextareaType::class)
169+
->add('parent', EntityType::class, ['class' => Page::class])
170+
->add('homepage', CheckboxType::class)
171+
->add('enabled', CheckboxType::class)
172+
;
173+
174+
$form = $builder->getForm();
175+
176+
$form->submit([
177+
'title' => null,
178+
'slug' => null,
179+
'metaDescription' => null,
180+
'metaTitle' => null,
181+
'metaKeywords' => null,
182+
'host' => null,
183+
'content' => null,
184+
'css' => null,
185+
'js' => null,
186+
'category' => null,
187+
'parent' => null,
188+
'homepage' => null,
189+
'enabled' => null,
190+
]);
191+
192+
static::assertSame('', $page->getTitle());
193+
static::assertSame('', $page->getSlug());
194+
static::assertNull($page->getMetaDescription());
195+
static::assertNull($page->getMetaTitle());
196+
static::assertNull($page->getMetaKeywords());
197+
static::assertNull($page->getHost());
198+
static::assertNull($page->getContent());
199+
static::assertNull($page->getCss());
200+
static::assertNull($page->getJs());
201+
static::assertNull($page->getCategory());
202+
static::assertNull($page->getParent());
203+
static::assertFalse($page->isHomepage());
204+
static::assertFalse($page->isEnabled());
205+
}
144206
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"symfony/browser-kit": "^5.0",
2727
"symfony/css-selector": "^5.0",
2828
"symfony/dom-crawler": "^5.0",
29+
"symfony/form": "^5.0",
2930
"symfony/phpunit-bridge": "^5.0",
3031
"symfony/yaml": "^5.0"
3132
},

0 commit comments

Comments
 (0)