Completed
Push — master ( 22512f...2e0539 )
by Kamil
24:53
created

ReportContext::thereIsReport()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 37
rs 8.8571
cc 3
eloc 27
nc 4
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
namespace Sylius\Bundle\ReportBundle\Behat;
13
14
use Behat\Gherkin\Node\TableNode;
15
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext;
16
17
/**
18
 * ReportContext for ReportBundle scenarios.
19
 *
20
 * @author Mateusz Zalewski <[email protected]>
21
 * @author Fernando Caraballo Ortiz <[email protected]>
22
 */
23
class ReportContext extends DefaultContext
24
{
25
    /**
26
     * @Given there are following reports configured:
27
     * @And there are following reports configured:
28
     */
29
    public function thereAreReports(TableNode $table)
30
    {
31
        $manager = $this->getEntityManager();
32
33
        foreach ($table->getHash() as $data) {
34
            $this->thereIsReport(
35
                $data['name'],
36
                $data['description'],
37
                $data["code"],
38
                $data['renderer'],
39
                $data["renderer_configuration"],
40
                $data["data_fetcher"],
41
                $data["data_fetcher_configuration"],
42
                false
43
            );
44
        }
45
46
        $manager->flush();
47
    }
48
49
    /**
50
     * Create a report.
51
     *
52
     * @param string $name
53
     * @param string $description
54
     * @param string $code
55
     * @param string $rendererType
56
     * @param mixed  $rendererConfiguration
57
     * @param string $dataFetcherType
58
     * @param mixed  $dataFetcherConfiguration
59
     * @param bool   $flush
60
     *
61
     * @return mixed
62
     */
63
    public function thereIsReport(
64
        $name,
65
        $description,
66
        $code,
67
        $rendererType,
68
        $rendererConfiguration,
69
        $dataFetcherType,
70
        $dataFetcherConfiguration,
71
        $flush = true
72
    ) {
73
        $factory = $this->getFactory('report');
74
75
        $report = $factory->createNew();
76
        $report->setName($name);
77
        $report->setDescription($description);
78
        $report->setCode($code);
79
80
        $report->setRenderer($rendererType);
81
        $report->setRendererConfiguration($this->getConfiguration($rendererConfiguration));
82
83
        $dataFetcherConfiguration = $this->getConfiguration($dataFetcherConfiguration);
84
        $dataFetcherConfiguration["start"] = new \DateTime($dataFetcherConfiguration["start"]);
85
        $dataFetcherConfiguration["end"] = new \DateTime($dataFetcherConfiguration["end"]);
86
        $dataFetcherConfiguration["empty_records"] = isset($dataFetcherConfiguration["empty_records"]) ? false : true;
87
88
        $report->setDataFetcher($dataFetcherType);
89
        $report->setDataFetcherConfiguration($dataFetcherConfiguration);
90
91
        $menager = $this->getEntityManager();
92
        $menager->persist($report);
93
94
        if ($flush) {
95
            $menager->flush();
96
        }
97
98
        return $report;
99
    }
100
101
    /**
102
     * @Then the report row for date :date will have a total amount of :total
103
     */
104
    public function theReportRowForDateWillHaveATotalAmountOf($date, $total)
105
    {
106
        $page = $this->getSession()->getPage();
107
        $rows = $page->findAll('css', 'table tbody tr');
108
        foreach ($rows as $row) {
109
            $columns = $row->findAll('css', 'td');
110
            if ($columns[1]->getText() === $date) {
111
                \PHPUnit_Framework_Assert::assertEquals($columns[2]->getText(), $total);
112
            }
113
        }
114
    }
115
116
    /**
117
     * @Given order #:orderNumber will be completed on :date
118
     */
119
    public function orderWillBeCompletedOn($orderNumber, $date)
120
    {
121
        $orderRepository = $this->getContainer()->get('sylius.repository.cart');
122
        $orderManager = $this->getContainer()->get('sylius.manager.cart');
123
124
        $order = $orderRepository->findOneBy(['number' => $orderNumber]);
125
        $order->setCompletedAt(new \DateTime($date));
126
127
        $orderManager->flush();
128
    }
129
}
130