| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare( strict_types=1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace FileFetcher\Tests\Unit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use FileFetcher\CachingFileFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use FileFetcher\FileFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SimpleCache\Cache\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @covers \FileFetcher\CachingFileFetcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class CachingFileFetcherTest extends TestCase { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 	public function testGetFileWhenNotCached() { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 		$fileUrl = 'foo://bar'; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 		$fileContents = 'NyanData across the sky!'; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		$fileFetcher = $this->createMock( FileFetcher::class ); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		$fileFetcher->expects( $this->once() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 			->method( 'fetchFile' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 			->with( $fileUrl ) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 			->will( $this->returnValue( $fileContents ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		$cache = $this->createMock( Cache::class ); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 		$cache->expects( $this->once() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 			->method( 'get' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 			->with( $fileUrl ) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 			->will( $this->returnValue( null ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 		$cache->expects( $this->once() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 			->method( 'set' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 			->with( $fileUrl ); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		$cachingFetcher = new CachingFileFetcher( $fileFetcher, $cache ); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		$cachingFetcher->fetchFile( $fileUrl ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	public function testGetFileWhenCached() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$fileUrl = 'foo://bar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$fileContents = 'NyanData across the sky!'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$fileFetcher = $this->createMock( FileFetcher::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$fileFetcher->expects( $this->never() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			->method( 'fetchFile' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$cache = $this->createMock( Cache::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$cache->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			->method( 'get' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			->with( $fileUrl ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			->will( $this->returnValue( $fileContents ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$cache->expects( $this->never() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			->method( 'set' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$cachingFetcher = new CachingFileFetcher( $fileFetcher, $cache ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$cachingFetcher->fetchFile( $fileUrl ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  |