| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\Newsletter\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Spatie\Newsletter\Exceptions\InvalidNewsletterList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Spatie\Newsletter\NewsletterList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Spatie\Newsletter\NewsletterListCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class NewsletterListCollectionTest extends PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $newsletterListCollection; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         parent::setUp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         $this->newsletterListCollection = NewsletterListCollection::createFromConfig( | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |                 'lists' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |                     'list1' => ['id' => 1], | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                     'list2' => ['id' => 2], | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |                     'list3' => ['id' => 3], | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                 'defaultListName' => 'list3', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 31 |  | View Code Duplication |     public function it_can_find_a_list_by_its_name() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $list = $this->newsletterListCollection->findByName('list2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertInstanceOf(NewsletterList::class, $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->assertSame(2, $list->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 41 |  | View Code Duplication |     public function it_will_use_the_default_list_when_not_specifing_a_listname() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $list = $this->newsletterListCollection->findByName(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertInstanceOf(NewsletterList::class, $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertSame(3, $list->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function it_will_throw_an_exception_when_using_a_default_list_that_does_not_exist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->expectException(InvalidNewsletterList::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $newsletterListCollection = NewsletterListCollection::createFromConfig( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'lists' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     'list1' => ['id' => 'list1'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'defaultListName' => 'list2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $newsletterListCollection->findByName(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function it_will_throw_an_exception_when_trying_to_find_a_list_that_does_not_exist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->expectException(InvalidNewsletterList::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->newsletterListCollection->findByName('blabla'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  | 
            
                        
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.