AddressHandlerTest.php 2.26 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));
    }

30
    public function testAddressCreate()
31
32
33
    {
        $this->assertTrue($this->addressExists(1));

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

Okhin's avatar
Okhin committed
37
    public function testAddressCreateWithSpecialChars()
38
39
40
    {
        $this->assertTrue($this->addressExists(1));

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

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