Completed
Pull Request — develop (#586)
by
unknown
62:11
created

AnalyticsManagerTest::testBuildPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 71
rs 9.1369
cc 1
eloc 19
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Testing case
4
 */
5
namespace Graviton\AnalyticsBundle\Tests\Manager;
6
7
use Graviton\AnalyticsBundle\Helper\JsonMapper;
8
use Graviton\AnalyticsBundle\Manager\AnalyticsManager;
9
use Graviton\AnalyticsBundle\Model\AnalyticModel;
10
use Graviton\TestBundle\Test\RestTestCase;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class AnalyticsManagerTest extends RestTestCase
18
{
19
    /**
20
     * Testing AnalyticsManager::parseObjectDates()
21
     * Testing AnalyticsManager::buildPipeline()
22
     * Testing JsonMapper::map()
23
     *
24
     * @return void
25
     */
26
    public function testBuildPipeline()
27
    {
28
        $analyticsManager = $this->getMockBuilder(AnalyticsManager::class)
29
            ->disableOriginalConstructor()
30
            ->getMock();
31
        $buildPipeline = $this->getPrivateClassMethod(AnalyticsManager::class, 'buildPipeline');
32
        $mapper = new JsonMapper();
33
        $date = new \DateTime('-4 years');
34
        $year = $date->format('Y');
35
        $expect = '[{"$match":{"created_year":{"$gte":'.$year.'}}},{"$group":{"_id":"app-count","count":{"$sum":1}}}]';
36
37
        $definitionA = json_decode(
38
            '{
39
              "collection": "App",
40
              "route": "app",
41
              "type": "object",
42
              "aggregate": {
43
                "$match": {
44
                  "created_year": {
45
                    "$gte": "PARSE_DATE(-4 years|Y)"
46
                  }
47
                },
48
                "$group": {
49
                  "_id": "app-count",
50
                  "count": {
51
                    "$sum": 1
52
                  }
53
                }
54
              },
55
              "schema": {}
56
            }
57
        '
58
        );
59
60
        $modelA = $mapper->map($definitionA, new AnalyticModel());
61
        $resultA = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelA]));
62
        $this->assertEquals($expect, $resultA);
63
64
        // Pipeline
65
        $definitionB = json_decode(
66
            '{
67
              "collection": "App",
68
              "route": "app",
69
              "type": "object",
70
              "pipeline": [
71
                {
72
                  "$match": {
73
                    "created_year": {
74
                      "$gte": "PARSE_DATE(-4 years|Y)"
75
                    }
76
                  }
77
                },
78
                {
79
                  "$group": {
80
                    "_id": "app-count",
81
                    "count": {
82
                      "$sum": 1
83
                    }
84
                  }
85
                }
86
              ],
87
              "schema": {}
88
            }
89
        '
90
        );
91
92
        $modelB = $mapper->map($definitionB, new AnalyticModel());
93
94
        $resultB = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelB]));
95
        $this->assertEquals($expect, $resultB);
96
    }
97
}
98