Completed
Pull Request — develop (#586)
by
unknown
136:55 queued 71:54
created

AnalyticsManagerTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 78
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testBuildPipeline() 0 69 1
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
     * @covers AnalyticsManager::parseObjectDates()
21
     * @covers AnalyticsManager::buildPipeline()
22
     * @covers JsonMapper::map()
23
     * @return void
24
     */
25
    public function testBuildPipeline()
26
    {
27
        $analyticsManager = $this->getContainer()->get('graviton.analytics.analytics_manager');
28
        $buildPipeline = $this->getPrivateClassMethod(get_class($analyticsManager), 'buildPipeline');
29
        $mapper = new JsonMapper();
30
        $date = new \DateTime('-4 years');
31
        $year = $date->format('Y');
32
        $expect = '[{"$match":{"created_year":{"$gte":'.$year.'}}},{"$group":{"_id":"app-count","count":{"$sum":1}}}]';
33
34
        $definitionA = json_decode(
35
            '{
36
              "collection": "App",
37
              "route": "app",
38
              "type": "object",
39
              "aggregate": {
40
                "$match": {
41
                  "created_year": {
42
                    "$gte": "PARSE_DATE(-4 years|Y)"
43
                  }
44
                },
45
                "$group": {
46
                  "_id": "app-count",
47
                  "count": {
48
                    "$sum": 1
49
                  }
50
                }
51
              },
52
              "schema": {}
53
            }
54
        '
55
        );
56
57
        $modelA = $mapper->map($definitionA, new AnalyticModel());
58
        $resultA = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelA]));
59
        $this->assertEquals($expect, $resultA);
60
61
        // Pipeline
62
        $definitionB = json_decode(
63
            '{
64
              "collection": "App",
65
              "route": "app",
66
              "type": "object",
67
              "pipeline": [
68
                {
69
                  "$match": {
70
                    "created_year": {
71
                      "$gte": "PARSE_DATE(-4 years|Y)"
72
                    }
73
                  }
74
                },
75
                {
76
                  "$group": {
77
                    "_id": "app-count",
78
                    "count": {
79
                      "$sum": 1
80
                    }
81
                  }
82
                }
83
              ],
84
              "schema": {}
85
            }
86
        '
87
        );
88
89
        $modelB = $mapper->map($definitionB, new AnalyticModel());
90
91
        $resultB = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelB]));
92
        $this->assertEquals($expect, $resultB);
93
    }
94
}
95