/** * Constructor adds test groups defined on global level * and adds additional logic for test names retrieval * * @see PHPUnit_Framework_TestSuite::__construct() */ public function __construct($theClass = '', $groups = array()) { if (!$theClass instanceof ReflectionClass) { $theClass = EcomDev_Utils_Reflection::getReflection($theClass); } // Check annotations for test case name $annotations = PHPUnit_Util_Test::parseTestMethodAnnotations($theClass->getName()); if (isset($annotations['name'])) { $this->suiteName = $annotations['name']; } // Creates all test instances parent::__construct($theClass); // Just sort-out them by our internal groups foreach ($groups as $group) { $this->groups[$group] = $this->tests(); } foreach ($this->tests() as $test) { if ($test instanceof PHPUnit_Framework_TestSuite) { /* @todo * Post an issue into PHPUnit bugtracker for * impossibility for specifying group by parent test case * Because it is a very dirty hack :( **/ $testGroups = array(); foreach ($groups as $group) { $testGroups[$group] = $test->tests(); } EcomDev_Utils_Reflection::setRestrictedPropertyValue($test, 'groups', $testGroups); } } // Remove un grouped tests group, if it exists if (isset($this->groups[self::NO_GROUP_KEYWORD])) { unset($this->groups[self::NO_GROUP_KEYWORD]); } }
/** * Proxied mock instance retrieval * * @return PHPUnit_Framework_MockObject_MockObject */ public function getMockInstance() { if ($this->mockInstance === null) { $reflection = ReflectionUtil::getReflection(ReflectionUtil::getRestrictedPropertyValue($this, 'type')); $this->mockInstance = $reflection->isAbstract() || $reflection->isInterface() ? $this->getMockForAbstractClass() : $this->getMock(); } return $this->mockInstance; }
/** * Returns processed file path * * @param string $fileName * @param string $relatedClassName * @param string $type * @return string|bool */ protected function _getFilePath($fileName, $relatedClassName, $type) { $reflection = EcomDev_Utils_Reflection::getReflection($relatedClassName); $fileObject = new SplFileInfo($reflection->getFileName()); $basePath = $fileObject->getPath(); // While base path is within a base directory of Magento installment while (strpos($basePath, Mage::getBaseDir()) && !is_dir($basePath . DS . $type)) { $basePath = dirname($basePath); } if (basename($basePath)) { return $basePath . DS . $type . DS . $fileName; } return false; }
/** * Returns app reflection instance * * @return ReflectionClass|ReflectionObject */ protected function getAppReflection() { $appClass = (string) Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_APP); $reflectionClass = EcomDev_Utils_Reflection::getReflection($appClass); return $reflectionClass; }
/** * Abstract cnstraint constructor, * provides unified interface for working with multiple types of evalation * * @param string $type * @param mixed $expectedValue * * @throws PHPUnit_Framework_Exception */ public function __construct($type, $expectedValue = null) { $reflection = EcomDev_Utils_Reflection::getReflection(get_class($this)); $types = array(); foreach ($reflection->getConstants() as $name => $constant) { if (strpos($name, 'TYPE_') === 0) { $types[] = $constant; } } if (empty($type) || !is_string($type) || !in_array($type, $types)) { throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $type); } if (isset($this->_expectedValueValidation[$type])) { $expectedValueType = isset($this->_expectedValueValidation[$type][2]) ? $this->_expectedValueValidation[$type][2] : ''; // Mandatory check if (isset($this->_expectedValueValidation[$type][0]) && $this->_expectedValueValidation[$type][0] && $expectedValue === null) { throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue); } // Type check if (isset($this->_expectedValueValidation[$type][1]) && $expectedValue !== null && !$this->_expectedValueValidation[$type][1]($expectedValue)) { throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue); } } $this->_type = $type; $this->_expectedValue = $expectedValue; }
/** * Loads test cases from search path, * Will return cached result * * @param string $searchPath path for searching files with tests * @param string $moduleCodeDir path where the module files are placed (e.g. app/code/local), * used for determining the class name * @return array */ protected static function _loadTestCases($searchPath, $moduleCodeDir) { if (Mage::app()->useCache(self::CACHE_TYPE)) { $cachedTestCases = Mage::app()->loadCache(self::CACHE_TYPE . md5($searchPath)); if ($cachedTestCases) { return unserialize($cachedTestCases); } } $testCases = array(); $directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($searchPath)); foreach ($directoryIterator as $fileObject) { /* @var $fileObject SplFileObject */ // Skip entry if it is not a php file if (!$fileObject->isFile() && !$fileObject->isLink() || $fileObject->getBasename('.php') === $fileObject->getBasename()) { continue; } $classPath = substr($fileObject->getPath() . DS . $fileObject->getBasename('.php'), strlen($moduleCodeDir)); $className = uc_words(ltrim($classPath, DS), '_', DS); // Add unit test case only // if it is a valid class extended from PHPUnit_Framework_TestCase if (class_exists($className, true)) { $reflectionClass = EcomDev_Utils_Reflection::getReflection($className); if (!$reflectionClass->isSubclassOf('PHPUnit_Framework_TestCase') || $reflectionClass->isAbstract()) { continue; } $testCases[] = $className; } } if (Mage::app()->useCache(self::CACHE_TYPE)) { Mage::app()->saveCache(serialize($testCases), self::CACHE_TYPE . md5($searchPath), array(self::CACHE_TAG)); } return $testCases; }
/** * Retrieves the loader for a particular entity type and data type * * @throws InvalidArgumentException * @param string $entityType * @param string $dataType * @return EcomDev_PHPUnit_Model_Mysql4_Fixture * @deprecated since 0.3.0 */ protected function _getComplexLoader($entityType, $dataType) { if (!$dataType) { throw new InvalidArgumentException('Must specify a data type for the loader'); } $reflection = EcomDev_Utils_Reflection::getReflection($this); $loaders = Mage::getConfig()->getNode($reflection->getConstant("XML_PATH_FIXTURE_{$dataType}_LOADERS")); if (isset($loaders->{$entityType})) { $classAlias = (string) $loaders->{$entityType}; } elseif (isset($loaders->{self::DEFAULT_EAV_LOADER_NODE})) { $classAlias = (string) $loaders->{self::DEFAULT_EAV_LOADER_NODE}; } else { $classAlias = self::DEFAULT_EAV_LOADER_CLASS; } return Mage::getResourceSingleton($classAlias); }
/** * Has method for making abstract testable * * @param array $method * @return bool */ protected function hasMethod($method) { $reflection = EcomDev_Utils_Reflection::getReflection($this); return $reflection->hasMethod($method); }
/** * Evaluates that layout block is an instance of expected class/interface * * @param EcomDev_PHPUnit_Constraint_Layout_LoggerInterface $other * @return boolean */ protected function evaluateInstanceOf($other) { $blockInfo = $other->findFirst(self::ACTION_BLOCK_CREATED, $this->_blockName); if ($blockInfo === false) { $this->setActualValue(false); return false; } $this->setActualValue($blockInfo['class']); $actualReflection = EcomDev_Utils_Reflection::getReflection($this->_actualValue); return $this->_actualValue === $this->_expectedValue || $actualReflection->isSubclassOf($this->_expectedValue); }
/** * Returns processed file path * * @param string $fileName * @param string $relatedClassName * @param string $type * @return string|bool */ protected function _getFilePath($fileName, $relatedClassName, $type) { $reflection = EcomDev_Utils_Reflection::getReflection($relatedClassName); $fileObject = new SplFileInfo($reflection->getFileName()); return $this->_checkFilePath(array($fileObject->getPath(), $fileObject->getPath() . DS . $fileObject->getBasename('.php')), $fileName, $type); }
/** * Get the setup model used by a Magento module * * @param $moduleName * @return mixed * @throws UnexpectedValueException */ protected function _getSetupModelForModule($moduleName) { $resources = Mage::getConfig()->getNode('global/resources')->children(); $resourceName = 'eav_setup'; $className = 'Mage_Eav_Model_Entity_Setup'; foreach ($resources as $resName => $resource) { if (!$resource->setup) { continue; } if (isset($resource->setup->module) && $resource->setup->module == $moduleName && isset($resource->setup->class)) { $className = $resource->setup->getClassName(); $resourceName = $resName; break; } } $setupModel = new $className($resourceName); $setupReflection = EcomDev_Utils_Reflection::getReflection($setupModel); if (!$setupReflection->hasMethod('addAttribute')) { throw new UnexpectedValueException('Problem loading EAV setup model'); } return $setupModel; }
/** * Returns class name from configuration path, * If $interface is specified, then it additionally checks it for implementation * * * @param string $configPath * @param string $interface * @throws RuntimeException * @return string */ protected function _getClassNameFromConfig($configPath, $interface = null) { $className = (string) $this->getConfig()->getNode($configPath); $reflection = EcomDev_Utils_Reflection::getReflection($className); if ($interface !== null && !$reflection->implementsInterface($interface)) { throw new RuntimeException(sprintf('Invalid class name defined in configuration path %s, because %s does not implement %s interface', $configPath, $interface)); } return $className; }