Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…to2ce into bugfixes49
  • Loading branch information
sivaschenko committed Aug 28, 2015
2 parents 74de725 + 45e7f26 commit d1726d1
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 105 deletions.
21 changes: 21 additions & 0 deletions dev/tests/integration/etc/di/preferences/ce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

return [
'Magento\Framework\Stdlib\CookieManagerInterface' => 'Magento\TestFramework\CookieManager',
'Magento\Framework\ObjectManager\DynamicConfigInterface' =>
'\Magento\TestFramework\ObjectManager\Configurator',
'Magento\Framework\App\RequestInterface' => 'Magento\TestFramework\Request',
'Magento\Framework\App\Request\Http' => 'Magento\TestFramework\Request',
'Magento\Framework\App\ResponseInterface' => 'Magento\TestFramework\Response',
'Magento\Framework\App\Response\Http' => 'Magento\TestFramework\Response',
'Magento\Framework\Interception\PluginListInterface' =>
'Magento\TestFramework\Interception\PluginList',
'Magento\Framework\Interception\ObjectManager\Config' =>
'Magento\TestFramework\ObjectManager\Config',
'Magento\Framework\View\LayoutInterface' => 'Magento\TestFramework\View\Layout',
'Magento\Framework\App\Resource\ConnectionAdapterInterface' => 'Magento\TestFramework\Db\ConnectionAdapter',
];
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\TestFramework;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DriverPool;

/**
Expand Down Expand Up @@ -89,24 +90,21 @@ protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool,
'default_setup' => ['type' => 'Magento\TestFramework\Db\ConnectionAdapter']
]
);
$diPreferences = [];
$diPreferencesPath = __DIR__ . '/../../../etc/di/preferences/';

$preferenceFiles = glob($diPreferencesPath . '*.php');

foreach ($preferenceFiles as $file) {
if (!is_readable($file)) {
throw new LocalizedException(__("'%1' is not readable file.", $file));
}
$diPreferences = array_replace($diPreferences, include $file);
}

$this->_primaryConfigData['preferences'] = array_replace(
$this->_primaryConfigData['preferences'],
[
'Magento\Framework\Stdlib\CookieManagerInterface' => 'Magento\TestFramework\CookieManager',
'Magento\Framework\ObjectManager\DynamicConfigInterface' =>
'\Magento\TestFramework\ObjectManager\Configurator',
'Magento\Framework\App\RequestInterface' => 'Magento\TestFramework\Request',
'Magento\Framework\App\Request\Http' => 'Magento\TestFramework\Request',
'Magento\Framework\App\ResponseInterface' => 'Magento\TestFramework\Response',
'Magento\Framework\App\Response\Http' => 'Magento\TestFramework\Response',
'Magento\Framework\Interception\PluginListInterface' =>
'Magento\TestFramework\Interception\PluginList',
'Magento\Framework\Interception\ObjectManager\Config' =>
'Magento\TestFramework\ObjectManager\Config',
'Magento\Framework\View\LayoutInterface' => 'Magento\TestFramework\View\Layout',
'Magento\Framework\App\Resource\ConnectionAdapterInterface' =>
'Magento\TestFramework\Db\ConnectionAdapter',
]
$diPreferences
);
}
return $this->_primaryConfigData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function testCreate()
];
$connection = $this->model->create($dbConfig);
$this->assertInstanceOf('\Magento\Framework\DB\Adapter\AdapterInterface', $connection);
$this->assertAttributeInstanceOf('\Magento\Framework\Cache\FrontendInterface', '_cacheAdapter', $connection);
$this->assertAttributeInstanceOf('\Magento\Framework\Db\LoggerInterface', 'logger', $connection);
}
}
150 changes: 150 additions & 0 deletions dev/tests/static/framework/Magento/TestFramework/Utility/CodeCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\TestFramework\Utility;

/**
* Searches for usage of classes, namespaces, functions, etc in PHP files
*/
class CodeCheck
{
/**
* Check if the class is used in the content
*
* @param string $className
* @param string $content
* @return bool
*/
public static function isClassUsed($className, $content)
{
/* avoid matching namespace instead of class */
$content = preg_replace('/namespace[^;]+;/', '', $content);
return self::_isRegexpMatched('/[^a-z\d_]' . preg_quote($className, '/') . '[^a-z\d_\\\\]/iS', $content);
}

/**
* Check if the namespace is used in the content
*
* @param string $namespace
* @param string $content
* @return bool
*/
public static function isNamespaceUsed($namespace, $content)
{
return self::_isRegexpMatched('/namespace\s+' . preg_quote($namespace, '/') . ';/S', $content)
|| self::_isRegexpMatched('/[^a-zA-Z\d_]' . preg_quote($namespace . '\\', '/') . '/S', $content);
}

/**
* Check if the specified function is called in the content.
* Note that declarations are not considered.
*
* If class context is not specified, invocation of all functions or methods (of any class)
* will be matched across the board
*
* If some class is specified, only its methods will be matched as follows:
* - usage of class::method
* - usage of $this, self and static within the class and its descendants
*
* @param string $method
* @param string $content
* @param string $class
* @return bool
*/
public static function isFunctionCalled($method, $content, $class = null)
{
$quotedMethod = preg_quote($method, '/');
if (!$class) {
return self::_isRegexpMatched(
'/(?<![a-z\d_:]|->|function\s)' . $quotedMethod . '\s*\(/iS',
$content
);
}
// without opening parentheses to match static callbacks notation
if (self::_isRegexpMatched(
'/' . preg_quote($class, '/') . '::\s*' . $quotedMethod . '[^a-z\d_]/iS',
$content
)
) {
return true;
}
if (self::isClassOrInterface($content, $class) || self::isDirectDescendant($content, $class)) {
return self::_isRegexpMatched('/this->' . $quotedMethod . '\s*\(/iS', $content)
|| self::_isRegexpMatched(
'/(self|static|parent)::\s*' . $quotedMethod . '\s*\(/iS',
$content
);
}
}

/**
* Check if methods or functions are used in the content
*
* If class context is specified, only the class and its descendants will be checked.
*
* @param string $property
* @param string $content
* @param string $class
* @return bool
*/
public static function isPropertyUsed($property, $content, $class = null)
{
if ($class) {
if (!self::isClassOrInterface($content, $class) && !self::isDirectDescendant($content, $class)) {
return false;
}
}
return self::_isRegexpMatched(
'/[^a-z\d_]' . preg_quote($property, '/') . '[^a-z\d_]/iS',
$content
);
}

/**
* Analyze content to determine whether it is a declaration of the specified class/interface
*
* @param string $content
* @param string $name
* @return bool
*/
public static function isClassOrInterface($content, $name)
{
return self::_isRegexpMatched('/\b(?:class|interface)\s+' . preg_quote($name, '/') . '\b[^{]*\{/iS', $content);
}

/**
* Analyze content to determine whether this is a direct descendant of the specified class/interface
*
* @param string $content
* @param string $name
* @return bool
*/
public static function isDirectDescendant($content, $name)
{
$name = preg_quote($name, '/');
return self::_isRegexpMatched(
'/\s+extends\s+' . $name . '\b|\s+implements\s+[^{]*\b' . $name . '\b[^{^\\\\]*\{/iS',
$content
);
}

/**
* Check if content matches the regexp
*
* @param string $regexp
* @param string $content
* @throws \Exception
* @return bool True if the content matches the regexp
*/
protected static function _isRegexpMatched($regexp, $content)
{
$result = preg_match($regexp, $content);
if ($result === false) {
throw new \Exception('An error occurred when matching regexp "' . $regexp . '""');
}
return 1 === $result;
}
}
43 changes: 19 additions & 24 deletions dev/tests/static/testsuite/Magento/Test/Legacy/ObsoleteCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

class ObsoleteCodeTest extends \PHPUnit_Framework_TestCase
{
/**
* Message text that is used to render suggestions
*/
const SUGGESTION_MESSAGE = 'Use "%s" instead.';

/**@#+
* Lists of obsolete entities from fixtures
*
Expand Down Expand Up @@ -427,11 +422,11 @@ protected function _testObsoleteConstants($content)
list($constant, $class, $replacement) = $row;
if ($class) {
$class = ltrim($class, '\\');
$this->checkConstantWithFullClasspath($constant, $class, $replacement, $content);
$this->checkConstantWithClasspath($constant, $class, $replacement, $content);
$this->_checkConstantWithFullClasspath($constant, $class, $replacement, $content);
$this->_checkConstantWithClasspath($constant, $class, $replacement, $content);
} else {
$regex = '\b' . preg_quote($constant, '/') . '\b';
$this->checkExistenceOfObsoleteConstants($regex, '', $content, $constant, $replacement, $class);
$this->_checkExistenceOfObsoleteConstants($regex, '', $content, $constant, $replacement, $class);
}
}
}
Expand Down Expand Up @@ -469,11 +464,11 @@ private function buildRegExFromObsoleteConstant($classPartialPath, $content, $co
* @param string $replacement
* @param string $content
*/
private function checkConstantWithFullClasspath($constant, $class, $replacement, $content)
private function _checkConstantWithFullClasspath($constant, $class, $replacement, $content)
{
$constantRegex = preg_quote($constant, '/');
$classRegex = preg_quote($class);
$this->checkExistenceOfObsoleteConstants(
$this->_checkExistenceOfObsoleteConstants(
$constantRegex,
$classRegex,
$content,
Expand All @@ -491,7 +486,7 @@ private function checkConstantWithFullClasspath($constant, $class, $replacement,
* @param string $replacement
* @param string $content
*/
private function checkConstantWithClasspath($constant, $class, $replacement, $content)
private function _checkConstantWithClasspath($constant, $class, $replacement, $content)
{
$classPathParts = explode('\\', $class);
$classPartialPath = '';
Expand Down Expand Up @@ -521,7 +516,7 @@ private function checkConstantWithClasspath($constant, $class, $replacement, $co
$this->_suggestReplacement(sprintf("Constant '%s' is obsolete.", $constant), $replacement)
);
} else {
$this->checkExistenceOfObsoleteConstants(
$this->_checkExistenceOfObsoleteConstants(
$constantRegex,
$classRegex,
$content,
Expand All @@ -543,7 +538,7 @@ private function checkConstantWithClasspath($constant, $class, $replacement, $co
* @param string $replacement
* @param string $class
*/
private function checkExistenceOfObsoleteConstants(
private function _checkExistenceOfObsoleteConstants(
$constantRegex,
$classRegex,
$content,
Expand All @@ -562,14 +557,14 @@ private function checkExistenceOfObsoleteConstants(
$matchClass = preg_match($classRegexFull, $content, $matchClassString);
if ($matchClass === 1) {
if ($matchClassString['classAlias']) {
$result = $this->checkAliasUseNamespace(
$result = $this->_checkAliasUseNamespace(
$constantRegex,
$matchConstantString,
$matchClassString,
$class
);
} else {
$result = $this->checkNoAliasUseNamespace($matchConstantString, $matchClassString, $class);
$result = $this->_checkNoAliasUseNamespace($matchConstantString, $matchClassString, $class);
}
} else {
foreach ($matchConstantString['classWithConst'] as $constantMatch) {
Expand Down Expand Up @@ -600,7 +595,7 @@ private function checkExistenceOfObsoleteConstants(
* @param string $class
* @return int
*/
private function checkAliasUseNamespace(
private function _checkAliasUseNamespace(
$constantRegex,
$matchConstantString,
$matchClassString,
Expand All @@ -618,7 +613,7 @@ private function checkAliasUseNamespace(
$foundAsComponent = true;
}
if (strpos($constantMatch, '::') !== false) {
$foundProperUse = $this->checkCompletePathOfClass(
$foundProperUse = $this->_checkCompletePathOfClass(
$constantMatch,
$matchClassString,
$class,
Expand All @@ -645,14 +640,14 @@ private function checkAliasUseNamespace(
* @param string $class
* @return int
*/
private function checkNoAliasUseNamespace(
private function _checkNoAliasUseNamespace(
$matchConstantString,
$matchClassString,
$class
) {
$foundProperUse = false;
foreach ($matchConstantString['constPart'] as $constantMatch) {
$foundProperUse = $this->checkCompletePathOfClass(
$foundProperUse = $this->_checkCompletePathOfClass(
$constantMatch,
$matchClassString,
$class
Expand All @@ -678,7 +673,7 @@ private function checkNoAliasUseNamespace(
* @param string $asComponent
* @return bool
*/
private function checkCompletePathOfClass(
private function _checkCompletePathOfClass(
$constantMatch,
$matchClassString,
$class,
Expand Down Expand Up @@ -706,7 +701,7 @@ private function checkCompletePathOfClass(
),
'\\'
));
if ($this->checkClasspathProperDivisionNoConstantPath(
if ($this->_checkClasspathProperDivisionNoConstantPath(
$pathInUseNamespaceTruncated,
$pathInUseNamespace,
$matchClassString,
Expand All @@ -715,7 +710,7 @@ private function checkCompletePathOfClass(
)) {
return true;
} else {
return $this->checkClasspathProperDivisionWithConstantPath(
return $this->_checkClasspathProperDivisionWithConstantPath(
$pathInUseNamespaceTruncated,
$pathInUseNamespace,
$pathWithConst,
Expand All @@ -735,7 +730,7 @@ private function checkCompletePathOfClass(
* @param bool $foundAsComponent
* @return bool
*/
private function checkClasspathProperDivisionNoConstantPath(
private function _checkClasspathProperDivisionNoConstantPath(
$pathInUseNamespaceTruncated,
$pathInUseNamespace,
$matchClassString,
Expand All @@ -760,7 +755,7 @@ private function checkClasspathProperDivisionNoConstantPath(
* @param bool $foundAsComponent
* @return bool
*/
private function checkClasspathProperDivisionWithConstantPath(
private function _checkClasspathProperDivisionWithConstantPath(
$pathInUseNamespaceTruncated,
$pathInUseNamespace,
$pathWithConst,
Expand Down
Loading

0 comments on commit d1726d1

Please sign in to comment.