Skip to content

Commit

Permalink
Added fixes to Category after writing form-based tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Apr 28, 2020
1 parent b824699 commit 1182bf8
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public function __construct()

public function getName(): string
{
return $this->name;
return (string) $this->name;
}

public function setName(string $name): void
public function setName(?string $name): void
{
$this->name = $name;
}
Expand All @@ -111,37 +111,37 @@ public function getDescription(): ?string
return $this->description;
}

public function setDescription(string $description = null): void
public function setDescription(?string $description): void
{
$this->description = $description;
}

public function getSlug(): string
{
return $this->slug;
return (string) $this->slug;
}

public function setSlug(string $slug = null): void
public function setSlug(?string $slug): void
{
$this->slug = $slug;
$this->slug = (string) $slug;
}

public function isEnabled(): bool
{
return $this->enabled;
}

public function setEnabled(bool $enabled = false): void
public function setEnabled(?bool $enabled = false): void
{
$this->enabled = $enabled;
$this->enabled = (bool) $enabled;
}

public function getParent(): ?Category
{
return $this->parent;
}

public function setParent(Category $parent = null): void
public function setParent(?Category $parent): void
{
if ($parent === $this) {
// Refuse the category to have itself as parent.
Expand Down
39 changes: 39 additions & 0 deletions Tests/Entity/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
use Orbitale\Bundle\CmsBundle\Tests\AbstractTestCase;
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Category;
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;

class CategoryTest extends AbstractTestCase
{
Expand Down Expand Up @@ -161,4 +167,37 @@ public function testCategorySlugIsNotTransliteratedIfEmpty()

static::assertEquals(null, $category->getSlug());
}

public function testSuccessfulFormSubmissionWithEmptyData()
{
static::bootKernel();

$category = new Category();

/** @var FormBuilderInterface $builder */
$builder = static::$container->get(FormFactoryInterface::class)->createBuilder(FormType::class, $category);
$builder
->add('name')
->add('slug')
->add('description', TextareaType::class)
->add('parent', EntityType::class, ['class' => Category::class])
->add('enabled', CheckboxType::class)
;

$form = $builder->getForm();

$form->submit([
'name' => null,
'slug' => null,
'description' => null,
'parent' => null,
'enabled' => null,
]);

static::assertSame('', $category->getName());
static::assertSame('', $category->getSlug());
static::assertNull($category->getDescription());
static::assertNull($category->getParent());
static::assertFalse($category->isEnabled());
}
}
62 changes: 62 additions & 0 deletions Tests/Entity/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
use Doctrine\ORM\EntityManager;
use Orbitale\Bundle\CmsBundle\Tests\AbstractTestCase;
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;

class PageTest extends AbstractTestCase
{
Expand Down Expand Up @@ -141,4 +147,60 @@ public function testPageSlugIsTransliterated()

static::assertEquals('default-page', $page->getSlug());
}

public function testSuccessfulFormSubmissionWithEmptyData()
{
static::bootKernel();

$page = new Page();

/** @var FormBuilderInterface $builder */
$builder = static::$container->get(FormFactoryInterface::class)->createBuilder(FormType::class, $page);
$builder
->add('title')
->add('slug')
->add('metaDescription')
->add('metaTitle')
->add('metaKeywords')
->add('host')
->add('content', TextareaType::class)
->add('css', TextareaType::class)
->add('js', TextareaType::class)
->add('parent', EntityType::class, ['class' => Page::class])
->add('homepage', CheckboxType::class)
->add('enabled', CheckboxType::class)
;

$form = $builder->getForm();

$form->submit([
'title' => null,
'slug' => null,
'metaDescription' => null,
'metaTitle' => null,
'metaKeywords' => null,
'host' => null,
'content' => null,
'css' => null,
'js' => null,
'category' => null,
'parent' => null,
'homepage' => null,
'enabled' => null,
]);

static::assertSame('', $page->getTitle());
static::assertSame('', $page->getSlug());
static::assertNull($page->getMetaDescription());
static::assertNull($page->getMetaTitle());
static::assertNull($page->getMetaKeywords());
static::assertNull($page->getHost());
static::assertNull($page->getContent());
static::assertNull($page->getCss());
static::assertNull($page->getJs());
static::assertNull($page->getCategory());
static::assertNull($page->getParent());
static::assertFalse($page->isHomepage());
static::assertFalse($page->isEnabled());
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"symfony/browser-kit": "^5.0",
"symfony/css-selector": "^5.0",
"symfony/dom-crawler": "^5.0",
"symfony/form": "^5.0",
"symfony/phpunit-bridge": "^5.0",
"symfony/yaml": "^5.0"
},
Expand Down

0 comments on commit 1182bf8

Please sign in to comment.