Completed
Push — master ( 554f9d...0d589d )
by
unknown
13:57
created

AnalyticModelTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

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