| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Tests\GBProd\ElasticaSpecification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Elastica\QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Elastica\Query\AbstractQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Elastica\Query\BoolQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GBProd\ElasticaSpecification\ExpressionBuilder\NotBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GBProd\ElasticaSpecification\ExpressionBuilder\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GBProd\ElasticaSpecification\Registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use GBProd\Specification\Not; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GBProd\Specification\Specification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class NotBuilderTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function testConstruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $builder = new NotBuilder(new Registry()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->assertInstanceOf(NotBuilder::class, $builder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 23 |  | View Code Duplication |     public function testBuildReturnsNotExpression() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $not = $this->createNot(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $registry = $this->createRegistry($not); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $builder = new NotBuilder($registry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $query = $builder->build($not, new QueryBuilder()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assertInstanceOf(BoolQuery::class, $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertArrayHasKey('bool', $query->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertArrayHasKey('must_not', $query->toArray()['bool']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertCount(1, $query->toArray()['bool']['must_not']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return Not | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     private function createNot() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         return new Not( | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             $this->createMock(Specification::class) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param Not $not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return Registry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function createRegistry($not) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $builder = $this->createMock(Builder::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ->method('build') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ->willReturn($this->createMock(AbstractQuery::class)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $registry = new Registry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $registry->register(get_class($not->getWrappedSpecification()), $builder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 71 |  | View Code Duplication |     public function testBuildThrowExceptionIfNotNotSpecification() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $spec = $this->createMock(Specification::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $registry = new Registry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $builder = new NotBuilder($registry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->setExpectedException(\InvalidArgumentException::class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $expr = $builder->build($spec, new QueryBuilder()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                        
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.