diff --git a/core/lib/Thelia/Action/Area.php b/core/lib/Thelia/Action/Area.php index 51d9ef04d..19037c9e4 100644 --- a/core/lib/Thelia/Action/Area.php +++ b/core/lib/Thelia/Action/Area.php @@ -56,6 +56,8 @@ class Area extends BaseAction implements EventSubscriberInterface public function removeCountry(AreaRemoveCountryEvent $event) { if (null !== $country = CountryQuery::create()->findPk($event->getCountryId())) { + $event->setArea($country->getArea()); + $country->setDispatcher($event->getDispatcher()); $country->setAreaId(null) ->save(); diff --git a/core/lib/Thelia/Tests/Action/AreaTest.php b/core/lib/Thelia/Tests/Action/AreaTest.php index a67ebc205..bfc756e5b 100644 --- a/core/lib/Thelia/Tests/Action/AreaTest.php +++ b/core/lib/Thelia/Tests/Action/AreaTest.php @@ -24,7 +24,13 @@ namespace Thelia\Tests\Action; use Thelia\Action\Area; +use Thelia\Core\Event\Area\AreaAddCountryEvent; use Thelia\Core\Event\Area\AreaCreateEvent; +use Thelia\Core\Event\Area\AreaDeleteEvent; +use Thelia\Core\Event\Area\AreaRemoveCountryEvent; +use Thelia\Core\Event\Area\AreaUpdatePostageEvent; +use Thelia\Model\Area as AreaModel; +use Thelia\Model\CountryQuery; /** @@ -52,6 +58,95 @@ class AreaTest extends \PHPUnit_Framework_TestCase $this->assertTrue($event->hasArea()); $this->assertEquals('foo', $createdArea->getName()); + + return $createdArea; + } + + /** + * @param AreaModel $area + * @depends testCreate + */ + public function testUpdatePostage(AreaModel $area) + { + $event = new AreaUpdatePostageEvent($area->getId()); + $event + ->setPostage(20) + ->setDispatcher($this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface")); + + $areaAction = new Area(); + $areaAction->updatePostage($event); + + $updatedArea = $event->getArea(); + + $this->assertInstanceOf('Thelia\Model\Area', $updatedArea); + $this->assertEquals(20, $updatedArea->getPostage()); + + return $updatedArea; + + } + + /** + * @param AreaModel $area + * @depends testUpdatePostage + */ + public function testAddCountry(AreaModel $area) + { + $country = CountryQuery::create()->findOne(); + + $event = new AreaAddCountryEvent($area->getId(), $country->getId()); + $event->setDispatcher($this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface")); + + $areaAction = new Area(); + $areaAction->addCountry($event); + + $updatedArea = $event->getArea(); + + $updatedCountry = CountryQuery::create()->findOneByAreaId($updatedArea->getId()); + + $this->assertInstanceOf('Thelia\Model\Area', $updatedArea); + $this->assertEquals($country->getId(), $updatedCountry->getId()); + + return $updatedArea; + } + + /** + * @param AreaModel $area + * @depends testAddCountry + */ + public function testRemoveCountry(AreaModel $area) + { + $country = CountryQuery::create()->filterByArea($area)->find()->getFirst(); + + $event = new AreaRemoveCountryEvent($area->getId(), $country->getId()); + $event->setDispatcher($this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface")); + + $areaAction = new Area(); + $areaAction->removeCountry($event); + + $updatedCountry = CountryQuery::create()->findPk($country->getId()); + $updatedArea = $event->getArea(); + + $this->assertInstanceOf('Thelia\Model\Area', $updatedArea); + $this->assertNull($updatedCountry->getAreaId()); + + return $event->getArea(); + } + + /** + * @depends testRemoveCountry + */ + public function testDelete(AreaModel $area) + { + $event = new AreaDeleteEvent($area->getId()); + $event->setDispatcher($this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface")); + + $areaAction = new Area(); + $areaAction->delete($event); + + $deletedArea = $event->getArea(); + + $this->assertInstanceOf('Thelia\Model\Area', $deletedArea); + $this->assertTrue($deletedArea->isDeleted()); } } \ No newline at end of file