| 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  |