TicketAvailabilityTest::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 3
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Service\TicketAvailability;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Mockery\Adapter\Phpunit\MockeryTestCase;
7
use ConferenceTools\Tickets\Domain\Finder\TicketCounterInterface;
8
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter;
9
use ConferenceTools\Tickets\Domain\Service\Configuration;
10
use Mockery as m;
11
12
class TicketAvailabilityTest extends MockeryTestCase
13
{
14
    /**
15
     * @var Configuration
16
     */
17
    private $config;
18
19
    /**
20
     * @var ArrayCollection
21
     */
22
    private $ticketCounters;
23
24
    public function __construct($name = null, array $data = array(), $dataName = '')
25
    {
26
        parent::__construct($name, $data, $dataName);
27
        $this->config = Configuration::fromArray([
28
            'tickets' => [
29
                'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75],
30
                'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150],
31
                'free' => ['name' => 'Free', 'cost' => 0, 'available' => 0]
32
            ],
33
            'financial' => [
34
                'taxRate' => 10,
35
                'currency' => 'GBP',
36
                'displayTax' => true
37
            ]
38
        ]);
39
        $ticketCounters['early'] = new TicketCounter(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ticketCounters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ticketCounters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
40
            $this->config->getTicketType('early'),
41
            $this->config->getAvailableTickets('early')
42
        );
43
44
        $ticketCounters['std'] = new TicketCounter(
45
            $this->config->getTicketType('std'),
46
            $this->config->getAvailableTickets('std')
47
        );
48
49
        $ticketCounters['free'] = new TicketCounter(
50
            $this->config->getTicketType('free'),
51
            $this->config->getAvailableTickets('free')
52
        );
53
54
        $this->ticketCounters= new ArrayCollection($ticketCounters);
55
    }
56
57
    public function testFetchAllAvailableTickets()
58
    {
59
        $mockFinder = m::mock(TicketCounterInterface::class);
60
        $mockFinder->shouldReceive('byTicketTypeIdentifiers')
61
            ->with('early', 'std', 'free')
62
            ->andReturn($this->ticketCounters);
63
64
        $sut = new TicketAvailability(new TicketTypeFilter($this->config), $mockFinder);
65
66
        $result = $sut->fetchAllAvailableTickets();
67
68
        self::assertTrue($result->count() === 2, 'The expected number of tickets was not returned');
69
        self::assertFalse($result->contains($this->ticketCounters['free']), 'Free tickets were included in the result');
70
    }
71
72
    /**
73
     * @dataProvider provideIsAvailable
74
     *
75
     * @param $ticketType
76
     * @param $quantity
77
     * @param $expected
78
     */
79
    public function testIsAvailable($ticketType, $quantity, $expected)
80
    {
81
        $mockFinder = m::mock(TicketCounterInterface::class);
82
        $mockFinder->shouldReceive('byTicketTypeIdentifiers')
83
            ->with('early', 'std', 'free')
84
            ->andReturn($this->ticketCounters);
85
86
        $sut = new TicketAvailability(new TicketTypeFilter($this->config), $mockFinder);
87
88
        self::assertEquals($expected, $sut->isAvailable($ticketType, $quantity));
89
    }
90
91
    public function provideIsAvailable()
92
    {
93
        return [
94
            [$this->config->getTicketType('free'), 1, false],
95
            [$this->config->getTicketType('std'), 1, true],
96
            [$this->config->getTicketType('early'), 76, false],
97
        ];
98
    }
99
}
100