From dad1a1c61217328c44e59bcc2422d3e2f3b49b48 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 4 Sep 2013 10:09:51 +0200 Subject: [PATCH] add update address action and create tests --- core/lib/Thelia/Action/Address.php | 11 ++ .../Core/Event/AddressCreateOrUpdateEvent.php | 1 + core/lib/Thelia/Tests/Action/AddressTest.php | 145 ++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 core/lib/Thelia/Tests/Action/AddressTest.php diff --git a/core/lib/Thelia/Action/Address.php b/core/lib/Thelia/Action/Address.php index 892547304..e28a98370 100644 --- a/core/lib/Thelia/Action/Address.php +++ b/core/lib/Thelia/Action/Address.php @@ -43,11 +43,22 @@ class Address extends BaseAction implements EventSubscriberInterface $this->createOrUpdate($address, $event); } + public function update(AddressCreateOrUpdateEvent $event) + { + $addressModel = $event->getAddress(); + + $this->createOrUpdate($addressModel, $event); + } + protected function createOrUpdate(AddressModel $addressModel, AddressCreateOrUpdateEvent $event) { $addressModel->setDispatcher($this->getDispatcher()); + if ($addressModel->isNew()) { + $addressModel->setLabel($event->getLabel()); + } + $addressModel ->setTitleId($event->getTitle()) ->setFirstname($event->getFirstname()) diff --git a/core/lib/Thelia/Core/Event/AddressCreateOrUpdateEvent.php b/core/lib/Thelia/Core/Event/AddressCreateOrUpdateEvent.php index 872021429..1eee2acc8 100644 --- a/core/lib/Thelia/Core/Event/AddressCreateOrUpdateEvent.php +++ b/core/lib/Thelia/Core/Event/AddressCreateOrUpdateEvent.php @@ -252,6 +252,7 @@ class AddressCreateOrUpdateEvent extends Event public function setAddress(Address $address) { $this->address = $address; + $this->setCustomer($address->getCustomer()); } /** diff --git a/core/lib/Thelia/Tests/Action/AddressTest.php b/core/lib/Thelia/Tests/Action/AddressTest.php new file mode 100644 index 000000000..68fc97923 --- /dev/null +++ b/core/lib/Thelia/Tests/Action/AddressTest.php @@ -0,0 +1,145 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Action; +use Thelia\Action\Address; +use Thelia\Core\Event\AddressCreateOrUpdateEvent; +use Thelia\Model\Base\CustomerQuery; + + +/** + * + * test address eventListener + * + * Class AddressTest + * @package Thelia\Tests\Action + * @author Manuel Raynaud + */ +class AddressTest extends \PHPUnit_Framework_TestCase +{ + + public function getContainer() + { + $container = new \Symfony\Component\DependencyInjection\ContainerBuilder(); + + $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface"); + + $container->set("event_dispatcher", $dispatcher); + + return $container; + } + + public function testCreatedAddress() + { + $customer = CustomerQuery::create()->findOne(); + + $AddressCreateOrUpdateEvent = new AddressCreateOrUpdateEvent( + "test address", + 1, + "Thelia", + "Thelia", + "5 rue rochon", + "", + "", + "63000", + "clermont-ferrand", + 64, + "0102030405", + "", + "" + ); + $AddressCreateOrUpdateEvent->setCustomer($customer); + + $actionAddress = new Address($this->getContainer()); + $actionAddress->create($AddressCreateOrUpdateEvent); + + $createdAddress = $AddressCreateOrUpdateEvent->getAddress(); + + $this->assertInstanceOf("Thelia\Model\Address", $createdAddress); + $this->assertFalse($createdAddress->isNew()); + $this->assertSame($customer, $createdAddress->getCustomer()); + + $this->assertEquals($AddressCreateOrUpdateEvent->getLabel(), $createdAddress->getLabel()); + $this->assertEquals($AddressCreateOrUpdateEvent->getTitle(), $createdAddress->getTitleId()); + $this->assertEquals($AddressCreateOrUpdateEvent->getFirstname(), $createdAddress->getFirstname()); + $this->assertEquals($AddressCreateOrUpdateEvent->getLastname(), $createdAddress->getLastname()); + $this->assertEquals($AddressCreateOrUpdateEvent->getAddress1(), $createdAddress->getAddress1()); + $this->assertEquals($AddressCreateOrUpdateEvent->getAddress2(), $createdAddress->getAddress2()); + $this->assertEquals($AddressCreateOrUpdateEvent->getAddress3(), $createdAddress->getAddress3()); + $this->assertEquals($AddressCreateOrUpdateEvent->getZipcode(), $createdAddress->getZipcode()); + $this->assertEquals($AddressCreateOrUpdateEvent->getCity(), $createdAddress->getCity()); + $this->assertEquals($AddressCreateOrUpdateEvent->getCountry(), $createdAddress->getCountryId()); + $this->assertEquals($AddressCreateOrUpdateEvent->getPhone(), $createdAddress->getPhone()); + $this->assertEquals($AddressCreateOrUpdateEvent->getCellphone(), $createdAddress->getCellphone()); + $this->assertEquals($AddressCreateOrUpdateEvent->getCompany(), $createdAddress->getCompany()); + } + + public function testUpdatedAddress() + { + + $customer = CustomerQuery::create()->findOne(); + $address = $customer->getAddresses()->getFirst(); + + $addressEvent = new AddressCreateOrUpdateEvent( + "", + 1, + "Thelia modif", + "Thelia modif", + "cour des étoiles", + "rue des miracles", + "", + "63000", + "clermont-ferrand", + 64, + "0102030405", + "", + "" + ); + $addressEvent->setAddress($address); + + $actionAddress = new Address($this->getContainer()); + $actionAddress->update($addressEvent); + + + $updatedAddress = $addressEvent->getAddress(); + $this->assertInstanceOf("Thelia\Model\Address", $updatedAddress); + $this->assertFalse($updatedAddress->isNew()); + $this->assertSame($customer, $updatedAddress->getCustomer()); + + $this->assertEquals($address->getLabel(), $updatedAddress->getLabel()); + $this->assertEquals($addressEvent->getTitle(), $updatedAddress->getTitleId()); + $this->assertEquals($addressEvent->getFirstname(), $updatedAddress->getFirstname()); + $this->assertEquals($addressEvent->getLastname(), $updatedAddress->getLastname()); + $this->assertEquals($addressEvent->getAddress1(), $updatedAddress->getAddress1()); + $this->assertEquals($addressEvent->getAddress2(), $updatedAddress->getAddress2()); + $this->assertEquals($addressEvent->getAddress3(), $updatedAddress->getAddress3()); + $this->assertEquals($addressEvent->getZipcode(), $updatedAddress->getZipcode()); + $this->assertEquals($addressEvent->getCity(), $updatedAddress->getCity()); + $this->assertEquals($addressEvent->getCountry(), $updatedAddress->getCountryId()); + $this->assertEquals($addressEvent->getPhone(), $updatedAddress->getPhone()); + $this->assertEquals($addressEvent->getCellphone(), $updatedAddress->getCellphone()); + $this->assertEquals($addressEvent->getCompany(), $updatedAddress->getCompany()); + + } + +} \ No newline at end of file