| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare( strict_types=1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace FileFetcher\Tests\Unit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use FileFetcher\FileFetchingException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use FileFetcher\InMemoryFileFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use FileFetcher\NullFileFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use FileFetcher\SpyingFileFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use FileFetcher\ThrowingFileFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @covers \FileFetcher\SpyingFileFetcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class SpyingFileFetcherTest extends TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	public function testReturnsResultOfDecoratedFetcher() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		$innerFetcher = new InMemoryFileFetcher( [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			'url' => 'content' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		$this->assertSame( 'content', $spyingFetcher->fetchFile( 'url' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		$this->expectException( FileFetchingException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$spyingFetcher->fetchFile( 'foo' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	public function testWhenNoCalls_getFetchedUrlsReturnsEmptyArray() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$innerFetcher = new InMemoryFileFetcher( [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			'url' => 'content' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$this->assertSame( [], $spyingFetcher->getFetchedUrls() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	public function testWhenSomeCalls_getFetchedUrlsReturnsTheArguments() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$innerFetcher = new InMemoryFileFetcher( [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			'url' => 'content', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			'foo' => 'bar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$spyingFetcher->fetchFile( 'url' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$spyingFetcher->fetchFile( 'foo' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$spyingFetcher->fetchFile( 'url' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->assertSame( [ 'url', 'foo', 'url' ], $spyingFetcher->getFetchedUrls() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	public function testCallsCausingExceptionsGetRecorded() { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 		$spyingFetcher = new SpyingFileFetcher( new ThrowingFileFetcher() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 		// @codingStandardsIgnoreStart | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 			$spyingFetcher->fetchFile( 'url' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		catch ( FileFetchingException $ex ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			$spyingFetcher->fetchFile( 'foo' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		catch ( FileFetchingException $ex ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		// @codingStandardsIgnoreEnd | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		$this->assertSame( [ 'url', 'foo' ], $spyingFetcher->getFetchedUrls() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function testWhenThereAreSomeCalls_getFirstFetchedUrlReturnsTheFirstOne() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$innerFetcher = new InMemoryFileFetcher( [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			'url' => 'content', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			'foo' => 'bar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$spyingFetcher->fetchFile( 'url' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$spyingFetcher->fetchFile( 'foo' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$this->assertSame( 'url', $spyingFetcher->getFirstFetchedUrl() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	public function testWhenThereAreNoCalls_getFirstFetchedUrlReturnsNull() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$spyingFetcher = new SpyingFileFetcher( new NullFileFetcher() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$this->assertNull( $spyingFetcher->getFirstFetchedUrl() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |