| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage UnitTests | 
					
						
							|  |  |  |  * @since 2011-10-.. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Unit tests for Load class | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | require dirname(__FILE__) . '/../Load.php'; | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  | require_once 'vfsStream/vfsStream.php'; | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  | class LoadTest extends PHPUnit_Framework_TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static $inc_dirs = array(); | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     private static $file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     private $root; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static $file_contents; | 
					
						
							|  |  |  |     public static $autoload_array; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function run(PHPUnit_Framework_TestResult $result = NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setPreserveGlobalState(false); | 
					
						
							|  |  |  |         return parent::run($result); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-10-13 14:00:31 +04:00
										 |  |  |      * @TODO: Load->buildAutoload() should recieve AutoloadBuilder as a parameter | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |      * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can cause error. | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     public function setUp() | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  |         self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "DbDriver" => "/lib/core/model/DbDriver.php"); ?>'; | 
					
						
							|  |  |  |         self::$autoload_array = array('Db' => '/lib/core/model/Db.php', 'DbDriver' => '/lib/core/model/DbDriver.php'); | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         vfsStreamWrapper::register(); | 
					
						
							|  |  |  |         $this->root = vfsStream::create( | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |                  'lib' => array( | 
					
						
							|  |  |  |                      'core' => | 
					
						
							|  |  |  |                      array( | 
					
						
							|  |  |  |                          'util' => array( | 
					
						
							|  |  |  |                              'AutoloadBuilder.php' => '' | 
					
						
							|  |  |  |                          ), | 
					
						
							|  |  |  |                          'model' => array( | 
					
						
							|  |  |  |                              'Db.php' => '', | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  |                              'DbDriver.php' => '' | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |                          ), | 
					
						
							|  |  |  |                          'Registry.php' => '', | 
					
						
							|  |  |  |                          'Load.php' => '', | 
					
						
							|  |  |  |                          'devel.config' => ' development config file' | 
					
						
							|  |  |  |                      ) | 
					
						
							|  |  |  |                  ), | 
					
						
							|  |  |  |                  'autoload.php' => self::$file_contents | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!class_exists('AutoloadBuilder')) { | 
					
						
							|  |  |  |             $this->getMock('AutoloadBuilder'); | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         vfsStreamWrapper::setRoot($this->root); | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         self::$file = vfsStream::url('root/autoload.php'); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         set_new_overload(array($this, 'newCallback')); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     public function testSetAutoLoadFromExistingFile() | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |         $this->setConstants(); | 
					
						
							|  |  |  |         $this->assertFileExists(self::$file); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |         Load::setAutoloadFrom(self::$file); | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |         $autoload = require(self::$file); | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         $this->assertEquals(self::$autoload_array, $autoload); | 
					
						
							|  |  |  |         Load::autoload('Db'); | 
					
						
							| 
									
										
										
										
											2011-10-10 14:21:44 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     public function testAutoloadFromNonExistingFile() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |         $this->setConstants(); | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         $this->assertTrue($this->root->removeChild('autoload.php')); | 
					
						
							|  |  |  |         $this->assertFileNotExists(self::$file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Load::setAutoloadFrom(self::$file); | 
					
						
							|  |  |  |         $autoload = require(self::$file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals(self::$autoload_array, $autoload); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     public function testAutoloadArrayExists() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->assertFileExists(self::$file); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @TODO: Load - check if input file returns array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testFileForArray() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $autoload = require(self::$file); | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  |         $this->assertInternalType('array', $autoload); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     public function testAutoloadArrayNotEmpty() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $autoload = require(self::$file); | 
					
						
							|  |  |  |         $this->assertNotEmpty($autoload); | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         $this->assertArrayHasKey('Db', $autoload); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     public function testAutoloadGetFilePath() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |         $this->setConstants(); | 
					
						
							|  |  |  |         Load::setAutoloadFrom(self::$file); | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  |         $this->assertNotEmpty(Load::getFilePath('DbDriver')); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |      * @TODO: Load::getFilePath - check for wrong index | 
					
						
							|  |  |  |      * @expectedException PHPUnit_Framework_Error | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testAutoloadGetFilePathNullIndex() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setConstants(); | 
					
						
							|  |  |  |         Load::setAutoloadFrom(self::$file); | 
					
						
							|  |  |  |         $autoload = require(self::$file); | 
					
						
							|  |  |  |         $this->assertNotEmpty(Load::getFilePath('anton')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  |      * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload(); | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testDebugAutoload() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |         $this->setConstants(); | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  |         Load::setAutoloadFrom(self::$file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $autoload = require(self::$file); | 
					
						
							|  |  |  |         $this->assertNotEmpty($autoload); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!defined('DEBUG')) { | 
					
						
							|  |  |  |             define('DEBUG', true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Load::autoload('Some'); | 
					
						
							|  |  |  |         Load::autoload('DbDriver'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 13:50:15 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     protected function newCallback($className) | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         switch ($className) { | 
					
						
							|  |  |  |             case 'AutoloadBuilder': | 
					
						
							|  |  |  |                 return 'AutoloadBuilderMock'; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return $className; | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 18:04:05 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  |     private function setConstants() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!defined('PATH')) { | 
					
						
							|  |  |  |             define('PATH', vfsStream::url('root')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!defined('APP')) { | 
					
						
							|  |  |  |             define('APP', 'lib/core/tests/face'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |     public function tearDown() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-28 15:51:49 +04:00
										 |  |  | //        if (defined('PATH')) {
 | 
					
						
							|  |  |  | //            echo PHP_EOL . __CLASS__ . '   ' . PATH . PHP_EOL;
 | 
					
						
							|  |  |  | //        } else {
 | 
					
						
							|  |  |  | //            echo PHP_EOL . __CLASS__ . '   ' . 'PATH NOT DEFINED' . PHP_EOL;
 | 
					
						
							|  |  |  | //        }
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  |         unset_new_overload(); | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 10:44:54 +04:00
										 |  |  | class AutoloadBuilderMock | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function build() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $file = new vfsStreamFile('autoload.php'); | 
					
						
							|  |  |  |         $file->setContent(LoadTest::$file_contents); | 
					
						
							|  |  |  |         vfsStreamWrapper::getRoot()->addChild($file); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-06 16:33:50 +04:00
										 |  |  | } |