/** * Set upt the test environment */ public function setUp() { $this->dataDirs = array(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Data'); // get the objects we need $config = new Config(); $config->setValue('autoload/dirs', $this->dataDirs); $config->setValue('enforcement/dirs', array()); $this->structureMap = new StructureMap($config->getValue('autoload/dirs'), $config->getValue('enforcement/dirs'), $config); // fill the map $this->structureMap->fill(); }
/** * Will return true if the specified file has specified contracts, false if not. * * @param string $file File to check for contracts * * @return bool */ protected function findContracts($file) { // We need to get our array of needles $needles = array(PBC_KEYWORD_INVARIANT, PBC_KEYWORD_POST, PBC_KEYWORD_PRE); // If we have to enforce things like @param or @returns, we have to be more sensitive if ($this->config->getValue('enforcement/enforce-default-type-safety') === true) { $needles[] = '@var'; $needles[] = '@param'; $needles[] = '@return'; } // Open the file and search it piece by piece until we find something or the file ends. $rsc = fopen($file, 'r'); $recent = ''; while (!feof($rsc)) { // Get a current chunk $current = fread($rsc, 512); // We also check the last chunk as well, to avoid cutting the only needle we have in two. $haystack = $recent . $current; foreach ($needles as $needle) { // If we found something we can return true if (strpos($haystack, $needle) !== false) { return true; } } // Set recent for the next iteration $recent = $current; } // Still here? So nothing was found. return false; }
/** * Will register our autoloading method at the beginning of the spl autoloader stack * * @param boolean $throw Should we throw an exception on error? * @param boolean $prepend If you want to NOT prepend you might, but you should not * * @return null */ public function register($throw = true, $prepend = true) { // Now we have a config no matter what, we can store any instance we might need $this->config->storeInstances(); // We want to let our autoloader be the first in line so we can react on loads // and create/return our contracted definitions. // So lets use the prepend parameter here. spl_autoload_register(array($this, self::OUR_LOADER), $throw, $prepend); }
<?php /** * File bootstrapping the PHPUnit test environment * * PHP version 5 * * @category Php-by-contract * @package TechDivision\PBC * @author Bernhard Wick <*****@*****.**> * @copyright 2014 TechDivision GmbH - <*****@*****.**> * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link http://www.techdivision.com/ */ use TechDivision\PBC\Config; // Get the vendor dir $vendorDir = ''; if (realpath(__DIR__ . "/vendor")) { $vendorDir = realpath(__DIR__ . "/vendor"); } else { throw new Exception('Could not locate vendor dir'); } // Include the composer autoloader as a fallback $loader = (require $vendorDir . DIRECTORY_SEPARATOR . 'autoload.php'); $loader->add('TechDivision\\PBC', array(__DIR__ . '/src', __DIR__ . '/tests')); // Load the test config file $config = new Config(); $config->load(__DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'TechDivision' . DIRECTORY_SEPARATOR . 'PBC' . DIRECTORY_SEPARATOR . 'Tests' . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'tests.conf.json'); // We have to register our autoLoader to put our proxies in place $autoLoader = new TechDivision\PBC\AutoLoader($config); $autoLoader->register();
/** * Will test if a configuration using regexed paths can be used properly * * @return null */ public function testRegexMapping() { // We have to load the config for regular expressions in the project dirs $config = new Config(); $config->load(str_replace(DIRECTORY_SEPARATOR . 'Functional', '', __DIR__) . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'RegexTest' . DIRECTORY_SEPARATOR . 'regextest.conf.json'); $e = null; try { $regexTestClass1 = new RegexTestClass1(); } catch (Exception $e) { } // Did we get the right $e? $this->assertNull($e); $e = null; try { $regexTestClass2 = new RegexTestClass2(); } catch (\Exception $e) { } // Did we get the right $e? $this->assertNull($e); $e = null; try { $regexTestClass = new RegexTestClass(); } catch (\Exception $e) { } // Did we get the right $e? $this->assertNull($e); }
/** * Visitor method that adds a initialized class loader to the passed application. * * @param \TechDivision\Application\Interfaces\ApplicationInterface $application The application instance * @param \TechDivision\ApplicationServer\Api\Node\ClassLoaderNodeInterface $configuration The class loader configuration node * * @return void */ public static function visit(ApplicationInterface $application, ClassLoaderNodeInterface $configuration = null) { // load the web application path we want to register the class loader for $webappPath = $application->getWebappPath(); // initialize the class path and the enforcement directories $classPath = array(); $enforcementDirs = array(); // add the possible class path if folder is available foreach ($configuration->getDirectories() as $directory) { if (is_dir($webappPath . $directory->getNodeValue())) { array_push($classPath, $webappPath . $directory->getNodeValue()); if ($directory->isEnforced()) { array_push($enforcementDirs, $webappPath . $directory->getNodeValue()); } } } // initialize the arrays of different omit possibilities $omittedEnforcement = array(); $omittedAutoLoading = array(); // iterate over all namespaces and check if they are omitted in one or the other way foreach ($configuration->getNamespaces() as $namespace) { // is the enforcement omitted for this namespace? if ($namespace->omitEnforcement()) { $omittedEnforcement[] = $namespace->getNodeValue()->__toString(); } // is the autoloading omitted for this namespace? if ($namespace->omitAutoLoading()) { $omittedAutoLoading[] = $namespace->getNodeValue()->__toString(); } } // initialize the class loader configuration $config = new Config(); // set the environment mode we want to use $config->setValue('environment', $configuration->getEnvironment()); // set the cache directory $config->setValue('cache/dir', $application->getCacheDir()); // set the default autoloader values $config->setValue('autoloader/dirs', $classPath); // collect the omitted namespaces (if any) $config->setValue('autoloader/omit', $omittedAutoLoading); $config->setValue('enforcement/omit', $omittedEnforcement); // set the default enforcement configuration values $config->setValue('enforcement/dirs', $enforcementDirs); $config->setValue('enforcement/enforce-default-type-safety', $configuration->getTypeSafety()); $config->setValue('enforcement/processing', $configuration->getProcessing()); $config->setValue('enforcement/level', $configuration->getEnforcementLevel()); $config->setValue('enforcement/logger', $application->getInitialContext()->getSystemLogger()); // create the autoloader instance and fill the structure map $autoLoader = new self($config); // add the class loader instance to the application $application->addClassLoader($autoLoader); }
/** * Test the unsetValue() method * * @return void * * @depends testHasValue */ public function testUnsetValue() { // Get our config $config = new Config(); // Unset some values and test if they do not exist anymore $this->assertTrue($config->hasValue('environment')); $config->unsetValue('environment'); $this->assertFalse($config->hasValue('environment')); // Do the same for a more "complex" index $this->assertTrue($config->hasValue('enforcement/enforce-default-type-safety')); $config->unsetValue('enforcement/enforce-default-type-safety'); $this->assertFalse($config->hasValue('enforcement/enforce-default-type-safety')); }