Completed
Pull Request — master (#22)
by Sergey
17:44 queued 02:42
created

RelationshipTest::testAssociate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace Isswp101\Persimmon\Test;
4
5
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
6
use Elasticsearch\Common\Exceptions\Missing404Exception;
7
use Isswp101\Persimmon\QueryBuilder\Filters\InnerHitsFilter;
8
use Isswp101\Persimmon\QueryBuilder\QueryBuilder;
9
use Isswp101\Persimmon\Test\Models\PurchaseOrder;
10
use Isswp101\Persimmon\Test\Models\PurchaseOrderLine;
11
12
class RelationshipTest extends BaseTestCase
13
{
14
    public static function setUpBeforeClass()
15
    {
16
        $hash = time() . rand(1, 1000);
17
        PurchaseOrder::$_index = 'travis_ci_test_parent_child_rel_' . $hash;
18
        PurchaseOrderLine::$_index = 'travis_ci_test_parent_child_rel_' . $hash;
19
    }
20
21
    public function testPrepareIndex()
22
    {
23
        $index = PurchaseOrderLine::getIndex();
24
25
        try {
26
            $this->es->indices()->delete(['index' => $index]);
27
        } catch (Missing404Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
28
        }
29
30
        $this->sleep(3);
31
32
        $settings = file_get_contents(__DIR__ . '/index.json');
33
        $this->es->indices()->create(['index' => $index, 'body' => $settings]);
34
    }
35
36
    public function testSaveWithBadRequest400Exception()
37
    {
38
        $po = new PurchaseOrder();
39
        $po->id = 1;
40
        $po->name = 'PO1';
41
42
        $line = new PurchaseOrderLine();
43
        $line->id = 1;
44
        $line->name = 'Line1';
45
46
        $this->expectException(BadRequest400Exception::class);
47
48
        $po->save();
49
        $line->save();
50
    }
51
52 View Code Duplication
    public function testSave()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $po = new PurchaseOrder();
55
        $po->id = 1;
56
        $po->name = 'PO1';
57
58
        $line = new PurchaseOrderLine();
59
        $line->id = 1;
60
        $line->name = 'Line1';
61
62
        $po->save();
63
        $po->lines()->save($line);
64
65
        $poPath = $po->getPath()->toArray();
66
        $linePath = $line->getPath()->toArray();
67
68
        $res = $this->es->get($poPath);
69
        $this->assertEquals(1, $res['_id']);
70
        $this->assertEquals('PO1', $res['_source']['name']);
71
72
        $res = $this->es->get($linePath);
73
        $this->assertEquals(1, $res['_id']);
74
        $this->assertEquals('Line1', $res['_source']['name']);
75
    }
76
77 View Code Duplication
    public function testAssociate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $po = new PurchaseOrder();
80
        $po->id = 1;
81
        $po->name = 'PO1';
82
83
        $line = new PurchaseOrderLine();
84
        $line->id = 1;
85
        $line->name = 'Line1';
86
87
        $po->save();
88
        $line->po()->associate($po);
89
        $line->save();
90
91
        $poPath = $po->getPath()->toArray();
92
        $linePath = $line->getPath()->toArray();
93
94
        $res = $this->es->get($poPath);
95
        $this->assertEquals(1, $res['_id']);
96
        $this->assertEquals('PO1', $res['_source']['name']);
97
98
        $res = $this->es->get($linePath);
99
        $this->assertEquals(1, $res['_id']);
100
        $this->assertEquals('Line1', $res['_source']['name']);
101
    }
102
103
    public function testGetParent()
104
    {
105
        $line = PurchaseOrderLine::findWithParentId(1, 1);
106
        $po = $line->po()->getOrFail();
107
        $this->assertEquals(1, $line->getParentId());
108
        $this->assertEquals(1, $po->getId());
109
    }
110
111
    public function testGetChildren()
112
    {
113
        $po = PurchaseOrder::findOrFail(1);
114
115
        $line = $po->lines()->find(1);
116
        $this->assertEquals(1, $line->getId());
117
        $this->assertEquals(1, $line->getParentId());
118
        $this->assertSame($po, $line->getParent());
119
120
        $lines = $po->lines()->get();
121
        $this->assertEquals(1, $lines->count());
122
        // $this->assertEquals($line->toArray(), $lines->first()->toArray());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
        $this->assertEquals($line->getParent()->toArray(), $lines->first()->getParent()->toArray());
124
    }
125
126
    public function testInnerHits()
127
    {
128
        $query = new QueryBuilder();
129
        $query->filter(new InnerHitsFilter(PurchaseOrderLine::getParentType()));
130
        $lines = PurchaseOrderLine::search($query);
131
        $line = $lines->first();
132
        $this->assertEquals(1, $line->po()->getOrFail()->getId());
133
    }
134
135
    public function testTearDown()
136
    {
137
        $this->deleteIndex(PurchaseOrderLine::$_index);
138
    }
139
}
140