AddressHandlerTest.php 2.4 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
    {
        $this->assertTrue($this->addressExists(1));

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

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

        $this->assertFalse($this->addressExists(2));
23
24
25
26
27
28
29
30
31
32
33
    }

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

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

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

34
    public function testAddressCreate()
35
36
37
    {
        $this->assertTrue($this->addressExists(1));

38
        $this->container['command_handler']->handle(new AddressCreateCommand(1, 1, '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF'));
39
40
    }

Okhin's avatar
Okhin committed
41
    public function testAddressCreateWithSpecialChars()
42
43
44
    {
        $this->assertTrue($this->addressExists(1));

45
        $this->container['command_handler']->handle(new AddressCreateCommand(1, 'New address', '6 rue Ménars', '', 22000, 'Saint Brieuc', 'France', 'Côtes d\'Armor'));
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

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

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

        $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");
    }

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