summaryrefslogtreecommitdiff
path: root/tests/unit/VendorTest.php
blob: de581103154aa3af9d55d89fbaafe58f255379a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace App\Unit;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use App\Model\Vendor;

/**
 * Tet the vedor class
 *
 * @author Phil Burton <phil@pgburton.com>
 */
class VendorTest extends TestCase
{
    /**
     * Test the good constructing of vendor
     *
     * @dataProvider getGoodConstructorData
     * @author Phil Burton <phil@pgburton.com>
     * @param string $name
     * @param string $postcode
     * @param int maxCovers
     */
    public function testGoodConstructor($name, $postcode, $maxCovers)
    {
        // create new vendor
        $vendor = new Vendor($name, $postcode, $maxCovers);

        // use reflection to get values of protected properties
        $reflectionClass = new ReflectionClass(Vendor::class);

        // Check name has been set correctly
        $nameProperty = $reflectionClass->getProperty('name');
        $nameProperty->setAccessible(true);
        $nameValue = $nameProperty->getValue($vendor);

        $this->assertEquals($nameValue, $name);

        // Check postcode has been set correctly
        $postcodeProperty = $reflectionClass->getProperty('postcode');
        $postcodeProperty->setAccessible(true);
        $postcodeValue = $postcodeProperty->getValue($vendor);

        $this->assertEquals($postcodeValue, $postcode);

        // Check maxCovers has been set correctly
        $maxCoversProperty = $reflectionClass->getProperty('maxCovers');
        $maxCoversProperty->setAccessible(true);
        $maxCoversValue = $maxCoversProperty->getValue($vendor);

        $this->assertEquals($maxCoversValue, $maxCovers);

        // Check the menus array is initalised to an empty array
        $mensuProperty = $reflectionClass->getProperty('menus');
        $mensuProperty->setAccessible(true);
        $menusValue = $mensuProperty->getValue($vendor);
        $this->assertEquals($menusValue, []);
    }

    /**
     * Data for testing good constcutor
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return mixed[]
     */
    public function getGoodConstructorData()
    {
        return [
            ['a', 'b', 1],
            ['1', '2', 3],
            ['A very long name with spaces in it', 'SW111TH', 10000000],
        ];
    }

    /**
     * Test the check location
     *
     * @dataProvider getLocationData
     * @author Phil Burton <phil@pgburton.com>
     */
    public function testCheckLocation($postcode, $postcodePart, $expectedResult)
    {
        $vendor = new Vendor('name', $postcode, 1);

        $this->assertEquals($vendor->checkLocation($postcodePart), $expectedResult);
    }

    /**
     * Return the location data for test
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return mixed[]
     */
    public function getLocationData()
    {
        return [
            ['SW111TH', 'SW', true],
            ['SW11 1TH', 'SW', true],
            ['sw111th', 'SW', true],
            ['sW111TH', 'SW', true],
            ['SW11 1TH', 'sw', true],
            ['sw11 1th', 'sw', true],
            ['SW', 'SW', true],
            ['SE111TH', 'SW', false],
            ['SW111TH', 'se', false],
            ['0000SW', 'SW', false],
        ];
    }
}