Completed
Push — docs-installation-setup ( 464016 )
by Kamil
05:18
created

SalesDataProviderTest::getExpectedMonths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Tests\Functional;
15
16
use Fidry\AliceDataFixtures\LoaderInterface;
17
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
18
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
19
use Sylius\Component\Core\Dashboard\Interval;
20
use Sylius\Component\Core\Dashboard\SalesDataProviderInterface;
21
use Sylius\Component\Core\Dashboard\SalesSummary;
22
use Sylius\Component\Core\Dashboard\SalesSummaryInterface;
23
use Sylius\Component\Core\Model\ChannelInterface;
24
use Sylius\Component\Core\Model\OrderInterface;
25
use Sylius\Component\Order\Processor\OrderProcessorInterface;
26
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27
use Symfony\Component\BrowserKit\Client;
28
29
final class SalesDataProviderTest extends WebTestCase
30
{
31
    /** @var Client */
32
    private static $client;
33
34
    protected function setUp(): void
35
    {
36
        self::$client = static::createClient();
37
        self::$client->followRedirects(true);
38
        self::$container = self::$client->getContainer();
39
40
        /** @var LoaderInterface $fixtureLoader */
41
        $fixtureLoader = self::$container->get('fidry_alice_data_fixtures.loader.doctrine');
42
43
        $fixtureLoader->load([__DIR__ . '/../DataFixtures/ORM/resources/year_sales.yml'], [], [], PurgeMode::createDeleteMode());
44
45
        /** @var OrderInterface[] $orders */
46
        $orders = self::$container->get('sylius.repository.order')->findAll();
47
        /** @var OrderProcessorInterface $orderProcessor */
48
        $orderProcessor = self::$container->get('sylius.order_processing.order_processor');
49
50
        foreach ($orders as $order) {
51
            $orderProcessor->process($order);
52
        }
53
54
        self::$container->get('sylius.manager.order')->flush();
55
    }
56
57
    /** @test */
58
    public function it_provides_year_sales_summary_grouped_by_year(): void
59
    {
60
        $startDate = new \DateTime('2019-01-01 00:00:01');
61
        $endDate = new \DateTime('2020-12-31 23:59:59');
62
63
        $salesSummary = $this->getSummaryForChannel($startDate, $endDate, Interval::year(), 'CHANNEL');
64
65
        $expectedPeriods = $this->getExpectedPeriods($startDate, $endDate, '1 year', 'Y');
66
67
        $this->assertInstanceOf(SalesSummary::class, $salesSummary);
68
        $this->assertSame($expectedPeriods, $salesSummary->getIntervals());
69
        $this->assertSame(['20.00', '110.00'], $salesSummary->getSales());
70
    }
71
72
    /** @test */
73
    public function it_provides_year_sales_summary_in_chosen_year(): void
74
    {
75
        $startDate = new \DateTime('2020-01-01 00:00:01');
76
        $endDate = new \DateTime('2020-12-31 23:59:59');
77
78
        $salesSummary = $this->getSummaryForChannel($startDate, $endDate, Interval::year(), 'CHANNEL');
79
80
        $expectedPeriods = $this->getExpectedPeriods($startDate, $endDate, '1 year', 'Y');
81
82
        $this->assertInstanceOf(SalesSummary::class, $salesSummary);
83
        $this->assertSame($expectedPeriods, $salesSummary->getIntervals());
84
        $this->assertSame(['110.00'], $salesSummary->getSales());
85
    }
86
87
    /** @test */
88
    public function it_provides_year_sales_summary_grouped_per_month(): void
89
    {
90
        $startDate = new \DateTime('2020-01-01 00:00:01');
91
        $endDate = new \DateTime('2020-12-31 23:59:59');
92
93
        $salesSummary = $this->getSummaryForChannel($startDate, $endDate, Interval::month(), 'CHANNEL');
94
        $expectedPeriods = $this->getExpectedPeriods($startDate, $endDate, '1 month', 'n.Y');
95
96
        $this->assertInstanceOf(SalesSummary::class, $salesSummary);
97
        $this->assertSame($expectedPeriods, $salesSummary->getIntervals());
98
        $this->assertSame(
99
            ['70.00', '40.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00'],
100
            $salesSummary->getSales()
101
        );
102
    }
103
104
    /** @test */
105
    public function it_provides_years_sales_summary_grouped_per_month(): void
106
    {
107
        $startDate = new \DateTime('2019-01-01 00:00:01');
108
        $endDate = new \DateTime('2020-12-31 23:59:59');
109
110
        $salesSummary = $this->getSummaryForChannel($startDate, $endDate, Interval::month(), 'CHANNEL');
111
        $expectedPeriods = $this->getExpectedPeriods($startDate, $endDate, '1 month', 'n.Y');
112
113
        $this->assertInstanceOf(SalesSummary::class, $salesSummary);
114
        $this->assertSame($expectedPeriods, $salesSummary->getIntervals());
115
        $this->assertSame(
116
            [
117
                '0.00', '0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'20.00',
118
                '70.00', '40.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00' ,'0.00'
119
            ],
120
            $salesSummary->getSales()
121
        );
122
    }
123
124
    /** @test */
125
    public function it_provides_different_data_for_each_channel_and_only_paid_orders(): void
126
    {
127
        $startDate = new \DateTime('2019-01-01 00:00:01');
128
        $endDate = new \DateTime('2019-12-31 23:59:59');
129
130
        $salesSummary = $this->getSummaryForChannel($startDate, $endDate, Interval::year(), 'EXPENSIVE_CHANNEL');
131
        $expectedMonths = $this->getExpectedPeriods($startDate, $endDate, '1 year', 'Y');
132
133
        $this->assertInstanceOf(SalesSummary::class, $salesSummary);
134
        $this->assertSame($expectedMonths, $salesSummary->getIntervals());
135
        $this->assertSame(
136
            ['330.00'],
137
            $salesSummary->getSales()
138
        );
139
    }
140
141
    private function getSummaryForChannel(\DateTimeInterface $startDate, \DateTimeInterface $endDate, Interval $interval, string $channelCode): SalesSummaryInterface
142
    {
143
        /** @var ChannelRepositoryInterface $channelRepository */
144
        $channelRepository = self::$container->get('sylius.repository.channel');
145
        /** @var ChannelInterface $channel */
146
        $channel = $channelRepository->findOneByCode($channelCode);
147
148
        /** @var SalesDataProviderInterface $salesDataProvider */
149
        $salesDataProvider = self::$container->get(SalesDataProviderInterface::class);
150
151
        return $salesDataProvider->getSalesSummary($channel, $startDate, $endDate, $interval);
152
    }
153
154
    private function getExpectedPeriods(\DateTimeInterface $startDate, \DateTimeInterface $endDate, string $interval, string $dateFormat): array
155
    {
156
        $expectedPeriods = [];
157
        $interval = new \DatePeriod(
158
            $startDate,
159
            \DateInterval::createFromDateString($interval),
160
            $endDate
161
        );
162
163
        /** @var \DateTimeInterface $date */
164
        foreach ($interval as $date) {
165
            $expectedPeriods[$date->format($dateFormat)] = 0;
166
        }
167
168
        return array_keys($expectedPeriods);
169
    }
170
}
171