| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Hyde\Testing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Hyde\Facades\Features; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Hyde\Facades\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Hyde\Framework\Actions\ConvertsArrayToFrontMatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Hyde\Hyde; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Hyde\Pages\Concerns\HydePage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Hyde\Pages\MarkdownPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Hyde\Support\Models\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\View\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use LaravelZero\Framework\Testing\TestCase as BaseTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function strip_newlines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | require_once __DIR__.'/helpers.php'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | abstract class TestCase extends BaseTestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     use CreatesApplication; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     use ResetsApplication; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     protected static bool $booted = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected array $fileMemory = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * Setup the test environment. | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     protected function setUp(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         parent::setUp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         if (! static::$booted) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->resetApplication(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             static::$booted = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * Clean up the testing environment before the next test. | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     protected function tearDown(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->cleanUpFilesystem(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if (method_exists(\Illuminate\View\Component::class, 'flushCache')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             /** Until https://github.com/laravel/framework/pull/44648 makes its way into Laravel Zero, we need to clear the cache ourselves */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             Component::flushCache(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             Component::forgetComponentsResolver(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             Component::forgetFactory(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         Features::clearMockedInstances(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         parent::tearDown(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     protected function assertEqualsIgnoringLineEndingType(string $expected, string $actual): void | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             strip_newlines($expected, true), | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             strip_newlines($actual, true), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     /** @internal */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     protected function mockRoute(?Route $route = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         view()->share('currentRoute', $route ?? (new Route(new MarkdownPage()))); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** @internal */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     protected function mockPage(?HydePage $page = null, ?string $currentPage = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         view()->share('page', $page ?? new MarkdownPage()); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         view()->share('currentPage', $currentPage ?? 'PHPUnit'); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** @internal */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     protected function mockCurrentPage(string $currentPage) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         view()->share('currentPage', $currentPage); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * Create a temporary file in the project directory. | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * The TestCase will automatically remove the file when the test is completed. | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     protected function file(string $path, ?string $contents = null): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if ($contents) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             file_put_contents(Hyde::path($path), $contents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             Hyde::touch($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |         $this->cleanUpWhenDone($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * Create a temporary directory in the project directory. | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * The TestCase will automatically remove the entire directory when the test is completed. | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     protected function directory(string $path): void | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         Filesystem::makeDirectory($path, recursive: true, force: true); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         $this->cleanUpWhenDone($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * Create a temporary Markdown+FrontMatter file in the project directory. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     protected function markdown(string $path, string $contents = '', array $matter = []): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |         $this->file($path, (new ConvertsArrayToFrontMatter())->execute($matter).$contents); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     protected function cleanUpFilesystem(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if (sizeof($this->fileMemory) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             foreach ($this->fileMemory as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 if (Filesystem::isDirectory($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     $dontDelete = ['_site', '_media', '_pages', '_posts', '_docs', 'app', 'config', 'storage', 'vendor', 'node_modules']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                     if (! in_array($file, $dontDelete)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                         Filesystem::deleteDirectory($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     Filesystem::unlink($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |             $this->fileMemory = []; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * Mark a path to be deleted when the test is completed. | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     protected function cleanUpWhenDone(string $path): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |         $this->fileMemory[] = $path; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 151 |  |  |  |