From 643099c1b6bc3b7f95b73c650f54cc24388d9da9 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 3 Feb 2014 18:03:09 +0100 Subject: [PATCH] create test suite for category listener --- core/lib/Thelia/Tests/Action/CategoryTest.php | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 core/lib/Thelia/Tests/Action/CategoryTest.php diff --git a/core/lib/Thelia/Tests/Action/CategoryTest.php b/core/lib/Thelia/Tests/Action/CategoryTest.php new file mode 100644 index 000000000..480c6f437 --- /dev/null +++ b/core/lib/Thelia/Tests/Action/CategoryTest.php @@ -0,0 +1,208 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Action; + +use Thelia\Action\Category; +use Thelia\Core\Event\Category\CategoryDeleteEvent; +use Thelia\Core\Event\Category\CategoryUpdateEvent; +use Thelia\Core\Event\UpdateSeoEvent; +use Thelia\Model\Category as CategoryModel; +use Thelia\Core\Event\Category\CategoryCreateEvent; +use Thelia\Model\CategoryQuery; +use Thelia\Tools\URL; + + +/** + * Class CategoryTest + * @package Thelia\Tests\Action + * @author Manuel Raynaud + */ +class CategoryTest extends \PHPUnit_Framework_TestCase +{ + use RewrittenUrlTestTrait; + + /** + * @var EventDispatcherInterface + */ + protected $dispatcher; + + public function setUp() + { + $this->dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface"); + } + + protected function getRandomCategory() + { + $category = CategoryQuery::create() + ->addAscendingOrderByColumn('RAND()') + ->findOne(); + + if (null === $category) { + $this->fail('use fixtures before launching test, there is no category in database'); + } + + return $category; + } + + public function getUpdateEvent(&$category) + { + if (!$category instanceof \Thelia\Model\Category) { + $category = $this->getRandomCategory(); + } + + $event = new CategoryUpdateEvent($category->getId()); + + $event + ->setLocale('en_US') + ->setTitle('bar') + ->setDescription('bar description') + ->setChapo('bar chapo') + ->setPostscriptum('bar postscriptum') + ->setVisible(0) + ->setParent(0) + ->setDispatcher($this->dispatcher) + ; + } + + public function getUpdateSeoEvent(&$category) + { + if (!$category instanceof \Thelia\Model\Category) { + $category = $this->getRandomCategory(); + } + + $event = new UpdateSeoEvent($category->getId()); + $event->setDispatcher($this->dispatcher); + $event + ->setLocale($category->getLocale()) + ->setMetaTitle($category->getMetaTitle()) + ->setMetaDescription($category->getMetaDescription()) + ->setMetaKeywords($category->getMetaKeywords()); + + return $event; + } + + public function processUpdateAction($event) + { + $action = new Category(); + $action->update($event); + + return $event->getCategory(); + } + + public function processUpdateSeoAction($event) + { + $action = new Category(); + + return $action->updateSeo($event); + + } + + public function testCreate() + { + $event = new CategoryCreateEvent(); + + $event + ->setLocale('en_US') + ->setParent(0) + ->setTitle('foo') + ->setVisible(1) + ->setDispatcher($this->dispatcher); + + $action = new Category(); + $action->create($event); + + $createdCategory = $event->getCategory(); + + $this->assertInstanceOf('Thelia\Model\Category', $createdCategory); + + $this->assertFalse($createdCategory->isNew()); + + $this->assertEquals('en_US', $createdCategory->getLocale()); + $this->assertEquals('foo', $createdCategory->getTitle()); + $this->assertEquals(1, $createdCategory->getVisible()); + $this->assertEquals(0, $createdCategory->getParent()); + $this->assertNull($createdCategory->getDescription()); + $this->assertNull($createdCategory->getChapo()); + $this->assertNull($createdCategory->getPostscriptum()); + + return $createdCategory; + } + + /** + * @param CategoryModel $category + * @depends testCreate + */ + public function testUpdate(CategoryModel $category) + { + $event = new CategoryUpdateEvent($category->getId()); + + $event + ->setLocale('en_US') + ->setTitle('bar') + ->setDescription('bar description') + ->setChapo('bar chapo') + ->setPostscriptum('bar postscriptum') + ->setVisible(0) + ->setParent(0) + ->setDispatcher($this->dispatcher) + ; + + $action = new Category(); + $action->update($event); + + $updatedCategory = $event->getCategory(); + + $this->assertInstanceOf('Thelia\Model\Category', $updatedCategory); + + $this->assertEquals('en_US', $updatedCategory->getLocale()); + $this->assertEquals('bar', $updatedCategory->getTitle()); + $this->assertEquals('bar description', $updatedCategory->getDescription()); + $this->assertEquals('bar chapo', $updatedCategory->getChapo()); + $this->assertEquals('bar postscriptum', $updatedCategory->getPostscriptum()); + $this->assertEquals(0, $updatedCategory->getVisible()); + $this->assertEquals(0, $updatedCategory->getParent()); + + return $updatedCategory; + } + + /** + * @depends testUpdate + */ + public function testDelete(CategoryModel $category) + { + $event = new CategoryDeleteEvent($category->getId()); + $event->setDispatcher($this->dispatcher); + + $action = new Category(); + $action->delete($event); + + $deletedCategory = $event->getCategory(); + + $this->assertInstanceOf('Thelia\Model\Category', $deletedCategory); + $this->assertTrue($deletedCategory->isDeleted()); + + + } + +} \ No newline at end of file