| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spiral\Tests\Statistics\Sources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Spiral\Statistics\Database\Statistics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Spiral\Statistics\Extract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Spiral\Tests\BaseTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testSamePeriodSourceFindExtract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @see Spiral\Tests\Statistics\Extract\Intervals\AbstractInterval::testAnotherPeriodSourceFindExtract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class StatisticsSourceTest extends BaseTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 15 |  | View Code Duplication |     public function testFindExtractWithEvents() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $track = $this->getTrack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->assertCount(0, $this->orm->source(Statistics::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $datetime = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $track->events(['some-event' => 1.23, 'some-event2' => 2.34], $datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->assertCount(2, $this->orm->source(Statistics::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->assertCount(2, $this->getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->findExtract(new \DateTime('-1 day'), new \DateTime('+1 day'), [])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertCount(1, $this->getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->findExtract(new \DateTime('-1 day'), new \DateTime('+1 day'), ['some-event'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assertCount(0, $this->getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ->findExtract(new \DateTime('-1 day'), new \DateTime('+1 day'), ['some-event3'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 36 |  | View Code Duplication |     public function testFindExtractWithDateRange() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $track = $this->getTrack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertCount(0, $this->orm->source(Statistics::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $datetime = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $track->events(['some-event' => 1.23, 'some-event2' => 2.34], $datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertCount(2, $this->orm->source(Statistics::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertCount(0, $this->getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->findExtract(new \DateTime('-3 day'), new \DateTime('-1 day'), [])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->assertCount(0, $this->getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->findExtract(new \DateTime('-3 day'), new \DateTime('-1 day'), ['some-event'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertCount(0, $this->getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->findExtract(new \DateTime('-3 day'), new \DateTime('-1 day'), ['some-event3'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function testFindByEventNameAndDatetime() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $track = $this->getTrack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertCount(0, $this->orm->source(Statistics::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $datetime = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $track->events(['some-event' => 1.23, 'some-event2' => 2.34], $datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertCount(2, $this->orm->source(Statistics::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertEmpty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->getSource()->findByEventNameAndDatetime('some-event3', $datetime)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertEmpty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->getSource()->findByEventNameAndDatetime('some-event', new \DateTime('+1 day'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $record = $this->getSource()->findByEventNameAndDatetime('some-event', $datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertNotEmpty($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->assertEquals($record->name, 'some-event'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->assertEquals($record->value, 1.23); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $record->timestamp->getTimestamp(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $datetime->getTimestamp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     public function testDataIntegrity() | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |         $track = $this->getTrack(); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         $this->assertCount(0, $this->orm->source(Statistics::class)); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         $datetime = new \DateTime(); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         $track->event('some-event', 1.23, $datetime); | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         $this->assertCount(1, $this->orm->source(Statistics::class)); | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         /** @var Statistics $record */ | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         $record = $this->orm->source(Statistics::class)->findOne(); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         $this->assertNotEmpty($record); | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         $this->assertEquals($record->name, 'some-event'); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         $this->assertEquals($record->value, 1.23); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         $this->assertEquals( | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             $record->timestamp->getTimestamp(), | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |             $datetime->getTimestamp() | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  | } | 
            
                        
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.