| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SmrTest\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Smr\Container\DiContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Smr\MySqlProperties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class DiContainerTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package SmrTest\Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @covers \Smr\Container\DiContainer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class DiContainerTest extends TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	private const PHPDI_COMPILED_CONTAINER_FILE = "/tmp/CompiledContainer.php"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	protected function setUp(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		if (file_exists(self::PHPDI_COMPILED_CONTAINER_FILE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			unlink(self::PHPDI_COMPILED_CONTAINER_FILE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	public function test_compilation_enabled_true() { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		// Given environment variable is turned off | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		unset($_ENV["DISABLE_PHPDI_COMPILATION"]); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		// And the container is built | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 		DiContainer::initializeContainer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 		// Then | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		self::assertFileExists(self::PHPDI_COMPILED_CONTAINER_FILE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function test_compilation_enabled_false() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		// Given environment variable is turned on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$_ENV["DISABLE_PHPDI_COMPILATION"] = "true"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		// And the container is built | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		DiContainer::initializeContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		// Then | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		self::assertFileDoesNotExist(self::PHPDI_COMPILED_CONTAINER_FILE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public function test_container_get_and_make() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		// Start with a fresh container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		DiContainer::initializeContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		// The first get should construct a new object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$class = MySqlProperties::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$instance1 = DiContainer::get($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		self::assertInstanceOf($class, $instance1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		// Getting the same class should now give the exact same object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$instance2 = DiContainer::get($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		self::assertSame($instance1, $instance2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		// Using make should construct a new object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$instance3 = DiContainer::make($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		self::assertNotSame($instance1, $instance3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		self::assertEquals($instance1, $instance3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |