AddressHandlerTest.php 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

namespace LQDN\Tests\Functional\Handler;

use LQDN\Command\AddressCreateCommand;
use LQDN\Command\AddressDeleteCommand;
use LQDN\Command\AddressUpdateCommand;
use LQDN\Handler\AddressHandler;
use LQDN\Tests\Functional\FunctionalTest;

class AddressHandlerTest extends FunctionalTest
{
Okhin's avatar
Okhin committed
13
    public function testAddressUsedDelete()
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    {
        $this->assertTrue($this->addressExists(1));

        $this->setExpectedException('LQDN\Exception\AddressUsedException');
        $this->container['command_handler']->handle(new AddressDeleteCommand(1, 1));
    }

    public function testAdressDelete()
    {
        $this->assertTrue($this->addressExists(2));

        $this->container['command_handler']->handle(new AddressDeleteCommand(2, 1));

        $this->assertFalse($this->addressExists(2));
    }

Okhin's avatar
Okhin committed
30
    public function testAddressUpdate()
31 32 33 34 35 36 37 38 39 40 41 42
    {
        $this->assertTrue($this->addressExists(1));
        $this->assertTrue($this->isDefaultAddress(1));
        $this->assertTrue($this->addressExists(2));
        $this->assertFalse($this->isDefaultAddress(2));

        $this->container['command_handler']->handle(new AddressUpdateCommand(2, 1, 'updated', '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF', true));

        $this->assertFalse($this->isDefaultAddress(1));
        $this->assertTrue($this->isDefaultAddress(2));
    }

43
    public function notestAddressCreateWithSpecialChars()
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    {
        $this->assertTrue($this->addressExists(1));
        $this->assertTrue($this->isDefaultAddress(1));

        $this->container['command_handler']->handle(new AddressCreateCommand(1, 'New address', 'updated', '6 rue Ménars', '', 22000, 'Saint Brieuc', 'France', 'Côtes d\'Armor', true));

        $expectedAddress = [
            'nom' => 'New address',
            'adresse' => '6 rue Ménars',
            'adresse2' => '',
            'codepostal' => '22000',
            'ville' => 'Saint Brieuc',
            'etat' => 'Côtes d\'Armor',
            'pays' => 'France',
            'alias' => 'updated',
            'user_id' => '1',
            'defaut' => '1',
        ];

        $latestAddress = $this->getLatestAddress();
        unset($latestAddress['id']);

        $this->assertFalse($this->isDefaultAddress(1));
        $this->assertSame($expectedAddress, $latestAddress);
    }

    /**
     * Check if an address exists in DB.
     *
     * @param int $id
     *
     * @return bool
     */
    private function addressExists($id)
    {
        return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM adresses WHERE id = $id");
    }

    /**
     * Check if an address is marked as default.
     *
     * @param int $id
     *
     * @return bool
     */
    private function isDefaultAddress($id)
    {
        return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM adresses WHERE id = $id and defaut=true");
    }

    /**
     * Retrieve latest address.
     *
     * @return []
     */
    private function getLatestAddress()
    {
        return $this->container['db']->fetchAssoc("SELECT * FROM adresses ORDER BY id DESC LIMIT 1");
    }
}