Passed
Pull Request — master (#69)
by Chris
17:32 queued 08:49
created

TicketAvailabilityTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A provideIsAvailable() 0 6 1
A testIsAvailable() 0 10 1
B __construct() 0 31 1
A testFetchAllAvailableTickets() 0 13 1
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
Comprehensibility Best Practice introduced by
$ticketCounters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ticketCounters = array(); before regardless.
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')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'byTicketTypeIdentifiers'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $mockFinder->/** @scrutinizer ignore-call */ 
61
                     shouldReceive('byTicketTypeIdentifiers')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
61
            ->with('early', 'std', 'free')
62
            ->andReturn($this->ticketCounters);
63
64
        $sut = new TicketAvailability(new TicketTypeFilter($this->config), $mockFinder);
0 ignored issues
show
Bug introduced by
$mockFinder of type Mockery\MockInterface is incompatible with the type ConferenceTools\Tickets\...\TicketCounterInterface expected by parameter $finder of ConferenceTools\Tickets\...lability::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $sut = new TicketAvailability(new TicketTypeFilter($this->config), /** @scrutinizer ignore-type */ $mockFinder);
Loading history...
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')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'byTicketTypeIdentifiers'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $mockFinder->/** @scrutinizer ignore-call */ 
83
                     shouldReceive('byTicketTypeIdentifiers')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
            ->with('early', 'std', 'free')
84
            ->andReturn($this->ticketCounters);
85
86
        $sut = new TicketAvailability(new TicketTypeFilter($this->config), $mockFinder);
0 ignored issues
show
Bug introduced by
$mockFinder of type Mockery\MockInterface is incompatible with the type ConferenceTools\Tickets\...\TicketCounterInterface expected by parameter $finder of ConferenceTools\Tickets\...lability::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $sut = new TicketAvailability(new TicketTypeFilter($this->config), /** @scrutinizer ignore-type */ $mockFinder);
Loading history...
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