Passed
Push — master ( 9be0c8...8c1bed )
by Chris
02:56
created

FactoryTest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Service\Basket;
4
5
use Carnage\Cqrs\Aggregate\Identity\GeneratorInterface;
6
use ConferenceTools\Tickets\Domain\Service\Configuration;
7
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode;
8
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\Percentage;
9
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservation;
10
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservationRequest;
11
use Mockery\Adapter\Phpunit\MockeryTestCase;
12
use PHPUnit\Framework\TestCase;
13
use Mockery as m;
14
15
class FactoryTest extends MockeryTestCase
16
{
17
    /**
18
     * @var Configuration
19
     */
20
    private $config;
21
22 View Code Duplication
    public function __construct($name = null, array $data = array(), $dataName = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        parent::__construct($name, $data, $dataName);
25
        $this->config = Configuration::fromArray([
26
            'tickets' => [
27
                'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75],
28
                'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150],
29
                'free' => ['name' => 'Free', 'cost' => 0, 'available' => 100, 'metadata' => ['private' => true]]
30
            ],
31
            'financial' => [
32
                'taxRate' => 10,
33
                'currency' => 'GBP',
34
                'displayTax' => true
35
            ]
36
        ]);
37
    }
38
39
    public function testBasket()
40
    {
41
        $generator = m::mock(GeneratorInterface::class);
42
        $ticketIds = ['id1', 'id2'];
43
        $generator->shouldReceive('generateIdentity')->andReturnValues($ticketIds);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'generateIdentity'. ( Ignorable by Annotation )

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

43
        $generator->/** @scrutinizer ignore-call */ 
44
                    shouldReceive('generateIdentity')->andReturnValues($ticketIds);

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...
44
45
        $validator = m::mock(BasketValidator::class);
46
        $validator->shouldReceive('validate');
47
48
        $sut = new Factory(
49
            $generator,
0 ignored issues
show
Bug introduced by
$generator of type Mockery\MockInterface is incompatible with the type Carnage\Cqrs\Aggregate\Identity\GeneratorInterface expected by parameter $ticketIdGenerator of ConferenceTools\Tickets\...\Factory::__construct(). ( Ignorable by Annotation )

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

49
            /** @scrutinizer ignore-type */ $generator,
Loading history...
50
            $this->config,
51
            $validator
0 ignored issues
show
Bug introduced by
$validator of type Mockery\MockInterface is incompatible with the type ConferenceTools\Tickets\...\Basket\BasketValidator expected by parameter $basketValidator of ConferenceTools\Tickets\...\Factory::__construct(). ( Ignorable by Annotation )

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

51
            /** @scrutinizer ignore-type */ $validator
Loading history...
52
        );
53
54
        $basket = $sut->basket(
55
            new TicketReservationRequest(
56
                $this->config->getTicketType('std'),
57
                2
58
            )
59
        );
60
61
        $ticketReservations = $basket->getTickets();
62
        $ticketIdsFromBasket = array_map(
63
            function (TicketReservation $item) {
64
                return $item->getReservationId();
65
            },
66
            $ticketReservations
67
        );
68
69
        self::assertEquals(2, count($ticketReservations));
70
        self::assertEquals($ticketIds, $ticketIdsFromBasket);
71
        self::assertFalse($basket->hasDiscountCode());
72
    }
73
74
    public function testBasketWithDiscount()
75
    {
76
        $generator = m::mock(GeneratorInterface::class);
77
        $ticketIds = ['id1', 'id2'];
78
        $generator->shouldReceive('generateIdentity')->andReturnValues($ticketIds);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'generateIdentity'. ( Ignorable by Annotation )

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

78
        $generator->/** @scrutinizer ignore-call */ 
79
                    shouldReceive('generateIdentity')->andReturnValues($ticketIds);

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...
79
80
        $validator = m::mock(BasketValidator::class);
81
        $validator->shouldReceive('validate');
82
83
        $sut = new Factory(
84
            $generator,
0 ignored issues
show
Bug introduced by
$generator of type Mockery\MockInterface is incompatible with the type Carnage\Cqrs\Aggregate\Identity\GeneratorInterface expected by parameter $ticketIdGenerator of ConferenceTools\Tickets\...\Factory::__construct(). ( Ignorable by Annotation )

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

84
            /** @scrutinizer ignore-type */ $generator,
Loading history...
85
            $this->config,
86
            $validator
0 ignored issues
show
Bug introduced by
$validator of type Mockery\MockInterface is incompatible with the type ConferenceTools\Tickets\...\Basket\BasketValidator expected by parameter $basketValidator of ConferenceTools\Tickets\...\Factory::__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
            /** @scrutinizer ignore-type */ $validator
Loading history...
87
        );
88
89
        $discountCode = new DiscountCode(
90
            'discount',
91
            'Discount',
92
            new Percentage(15)
93
        );
94
        $basket = $sut->basketWithDiscount(
95
            $discountCode,
96
            new TicketReservationRequest(
97
                $this->config->getTicketType('std'),
98
                2
99
            )
100
        );
101
102
        $ticketReservations = $basket->getTickets();
103
104
        self::assertEquals(2, count($ticketReservations));
105
        self::assertTrue($basket->hasDiscountCode());
106
        self::assertSame($discountCode, $basket->getDiscountCode());
107
    }
108
}
109