Skip to content

Commit

Permalink
Merge pull request #656 from kylekatarnls/feature/phpunit-update-depr…
Browse files Browse the repository at this point in the history
…ecated-usages

Update deprecated PHPUnit calls and fix array syntax for PHP 5.3 (Merged from #654)
  • Loading branch information
tvbeek committed Aug 8, 2019
2 parents b487b11 + 98195c9 commit df1bdf6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 52 deletions.
47 changes: 22 additions & 25 deletions src/test/php/PHPMD/AbstractTest.php
Expand Up @@ -17,6 +17,9 @@

namespace PHPMD;

use PDepend\Source\AST\ASTClass;
use PDepend\Source\AST\ASTFunction;
use PDepend\Source\AST\ASTMethod;
use PDepend\Source\Language\PHP\PHPBuilder;
use PDepend\Source\Language\PHP\PHPParserGeneric;
use PDepend\Source\Language\PHP\PHPTokenizerInternal;
Expand All @@ -26,6 +29,7 @@
use PHPMD\Node\InterfaceNode;
use PHPMD\Node\MethodNode;
use PHPMD\Node\TraitNode;
use PHPMD\Rule\Design\TooManyFields;
use PHPMD\Stubs\RuleStub;

/**
Expand Down Expand Up @@ -271,13 +275,9 @@ private function parseSource($sourceFile)
*/
protected function getClassMock($metric = null, $value = null)
{
$class = $this->getMock(
'PHPMD\\Node\\ClassNode',
array(),
array(null),
'',
false
);
$class = $this->getMockBuilder('PHPMD\\Node\\ClassNode')
->setConstructorArgs(array(new ASTClass('FooBar')))
->getMock();

if ($metric !== null) {
$class->expects($this->atLeastOnce())
Expand All @@ -298,7 +298,9 @@ protected function getClassMock($metric = null, $value = null)
protected function getMethodMock($metric = null, $value = null)
{
return $this->initFunctionOrMethod(
$this->getMock('PHPMD\\Node\\MethodNode', array(), array(null), '', false),
$this->getMockBuilder('PHPMD\\Node\\MethodNode')
->setConstructorArgs(array(new ASTMethod('fooBar')))
->getMock(),
$metric,
$value
);
Expand All @@ -314,7 +316,9 @@ protected function getMethodMock($metric = null, $value = null)
protected function createFunctionMock($metric = null, $value = null)
{
return $this->initFunctionOrMethod(
$this->getMock('PHPMD\\Node\\FunctionNode', array(), array(null), '', false),
$this->getMockBuilder('PHPMD\\Node\\FunctionNode')
->setConstructorArgs(array(new ASTFunction('fooBar')))
->getMock(),
$metric,
$value
);
Expand Down Expand Up @@ -360,7 +364,7 @@ protected function getReportMock($expectedInvokes = -1)
$expects = $this->exactly($expectedInvokes);
}

$report = $this->getMock('PHPMD\\Report');
$report = $this->getMockBuilder('PHPMD\\Report')->getMock();
$report->expects($expects)
->method('addRuleViolation');

Expand All @@ -386,7 +390,7 @@ protected function getRuleMock()
*/
protected function getRuleSetMock($expectedClass = null, $count = '*')
{
$ruleSet = $this->getMock('PHPMD\RuleSet');
$ruleSet = $this->getMockBuilder('PHPMD\RuleSet')->getMock();
if ($expectedClass === null) {
$ruleSet->expects($this->never())->method('apply');
} else {
Expand Down Expand Up @@ -416,13 +420,9 @@ protected function getRuleViolationMock(
$rule = null,
$description = null
) {
$ruleViolation = $this->getMock(
'PHPMD\\RuleViolation',
array(),
array(null, null, null),
'',
false
);
$ruleViolation = $this->getMockBuilder('PHPMD\\RuleViolation')
->setConstructorArgs(array(new TooManyFields(), new FunctionNode(new ASTFunction('fooBar')), 'Hello'))
->getMock();

if ($rule === null) {
$rule = new RuleStub();
Expand Down Expand Up @@ -465,13 +465,10 @@ protected function getErrorMock(
$file = '/foo/baz.php',
$message = 'Error in file "/foo/baz.php"') {

$processingError = $this->getMock(
'PHPMD\\ProcessingError',
array(),
array(null),
'',
false
);
$processingError = $this->getMockBuilder('PHPMD\\ProcessingError')
->setConstructorArgs(array(null))
->setMethods(array('getFile', 'getMessage'))
->getMock();

$processingError->expects($this->any())
->method('getFile')
Expand Down
12 changes: 6 additions & 6 deletions src/test/php/PHPMD/Node/ASTNodeTest.php
Expand Up @@ -33,7 +33,7 @@ class ASTNodeTest extends AbstractTest
*/
public function testGetImageDelegatesToGetImageMethodOfWrappedNode()
{
$mock = $this->getMock('PDepend\Source\AST\ASTNode');
$mock = $this->getMockBuilder('PDepend\Source\AST\ASTNode')->getMock();
$mock->expects($this->once())
->method('getImage');

Expand All @@ -48,7 +48,7 @@ public function testGetImageDelegatesToGetImageMethodOfWrappedNode()
*/
public function testGetNameDelegatesToGetImageMethodOfWrappedNode()
{
$mock = $this->getMock('PDepend\Source\AST\ASTNode');
$mock = $this->getMockBuilder('PDepend\Source\AST\ASTNode')->getMock();
$mock->expects($this->once())
->method('getImage');

Expand All @@ -63,7 +63,7 @@ public function testGetNameDelegatesToGetImageMethodOfWrappedNode()
*/
public function testHasSuppressWarningsAnnotationForAlwaysReturnsFalse()
{
$mock = $this->getMock('PDepend\Source\AST\ASTNode');
$mock = $this->getMockBuilder('PDepend\Source\AST\ASTNode')->getMock();

$node = new ASTNode($mock, __FILE__);
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
Expand All @@ -78,7 +78,7 @@ public function testHasSuppressWarningsAnnotationForAlwaysReturnsFalse()
*/
public function testGetParentNameReturnsNull()
{
$mock = $this->getMock('PDepend\Source\AST\ASTNode');
$mock = $this->getMockBuilder('PDepend\Source\AST\ASTNode')->getMock();
$node = new ASTNode($mock, __FILE__);

$this->assertNull($node->getParentName());
Expand All @@ -91,7 +91,7 @@ public function testGetParentNameReturnsNull()
*/
public function testGetNamespaceNameReturnsNull()
{
$mock = $this->getMock('PDepend\Source\AST\ASTNode');
$mock = $this->getMockBuilder('PDepend\Source\AST\ASTNode')->getMock();
$node = new ASTNode($mock, __FILE__);

$this->assertNull($node->getNamespaceName());
Expand All @@ -104,7 +104,7 @@ public function testGetNamespaceNameReturnsNull()
*/
public function testGetFullQualifiedNameReturnsNull()
{
$mock = $this->getMock('PDepend\Source\AST\ASTNode');
$mock = $this->getMockBuilder('PDepend\Source\AST\ASTNode')->getMock();
$node = new ASTNode($mock, __FILE__);

$this->assertNull($node->getFullQualifiedName());
Expand Down
2 changes: 1 addition & 1 deletion src/test/php/PHPMD/Node/ClassNodeTest.php
Expand Up @@ -55,7 +55,7 @@ public function testHasSuppressWarningsAnnotationForReturnsTrue()
$class = new ASTClass(null);
$class->setComment('/** @SuppressWarnings("PMD") */');

$rule = $this->getMock('PHPMD\\AbstractRule');
$rule = $this->getMockBuilder('PHPMD\\AbstractRule')->getMock();

$node = new ClassNode($class);

Expand Down
4 changes: 3 additions & 1 deletion src/test/php/PHPMD/Node/FunctionTest.php
Expand Up @@ -35,7 +35,9 @@ class FunctionNodeTest extends AbstractTest
*/
public function testMagicCallDelegatesToWrappedPHPDependFunction()
{
$function = $this->getMock('PDepend\\Source\\AST\\ASTFunction', array(), array(null));
$function = $this->getMockBuilder('PDepend\\Source\\AST\\ASTFunction')
->setConstructorArgs(array(null))
->getMock();
$function->expects($this->once())
->method('getStartLine');

Expand Down
4 changes: 3 additions & 1 deletion src/test/php/PHPMD/Node/MethodNodeTest.php
Expand Up @@ -37,7 +37,9 @@ class MethodNodeTest extends AbstractTest
*/
public function testMagicCallDelegatesToWrappedPHPDependMethod()
{
$method = $this->getMock('PDepend\\Source\\AST\\ASTMethod', array(), array(null));
$method = $this->getMockBuilder('PDepend\\Source\\AST\\ASTMethod')
->setConstructorArgs(array(null))
->getMock();
$method->expects($this->once())
->method('getStartLine');

Expand Down
12 changes: 6 additions & 6 deletions src/test/php/PHPMD/ParserFactoryTest.php
Expand Up @@ -35,7 +35,7 @@ public function testFactoryConfiguresInputDirectory()

$uri = $this->createFileUri('ParserFactory/Directory');

$phpmd = $this->getMock('PHPMD\\PHPMD', array('getInput'));
$phpmd = $this->getMockBuilder('PHPMD\\PHPMD')->setMethods(array('getInput'))->getMock();
$phpmd->expects($this->once())
->method('getInput')
->will($this->returnValue($uri));
Expand All @@ -58,7 +58,7 @@ public function testFactoryConfiguresInputFile()

$uri = $this->createFileUri('ParserFactory/File/Test.php');

$phpmd = $this->getMock('PHPMD\\PHPMD', array('getInput'));
$phpmd = $this->getMockBuilder('PHPMD\\PHPMD')->setMethods(array('getInput'))->getMock();
$phpmd->expects($this->once())
->method('getInput')
->will($this->returnValue($uri));
Expand All @@ -82,7 +82,7 @@ public function testFactoryConfiguresMultipleInputDirectories()
$uri1 = $this->createFileUri('ParserFactory/File');
$uri2 = $this->createFileUri('ParserFactory/Directory');

$phpmd = $this->getMock('PHPMD\\PHPMD', array('getInput'));
$phpmd = $this->getMockBuilder('PHPMD\\PHPMD')->setMethods(array('getInput'))->getMock();
$phpmd->expects($this->once())
->method('getInput')
->will($this->returnValue($uri1 . ',' . $uri2));
Expand All @@ -106,7 +106,7 @@ public function testFactoryConfiguresMultipleInputFilesAndDirectories()
$uri1 = $this->createFileUri('ParserFactory/File/Test.php');
$uri2 = $this->createFileUri('ParserFactory/Directory');

$phpmd = $this->getMock('PHPMD\\PHPMD', array('getInput'));
$phpmd = $this->getMockBuilder('PHPMD\\PHPMD')->setMethods(array('getInput'))->getMock();
$phpmd->expects($this->once())
->method('getInput')
->will($this->returnValue($uri1 . ',' . $uri2));
Expand All @@ -129,7 +129,7 @@ public function testFactoryConfiguresIgnorePattern()

$uri = $this->createFileUri('ParserFactory/File/Test.php');

$phpmd = $this->getMock('PHPMD\\PHPMD', array('getIgnorePattern', 'getInput'));
$phpmd = $this->getMockBuilder('PHPMD\\PHPMD')->setMethods(array('getIgnorePattern', 'getInput'))->getMock();
$phpmd->expects($this->exactly(2))
->method('getIgnorePattern')
->will($this->returnValue(array('Test')));
Expand All @@ -151,7 +151,7 @@ public function testFactoryConfiguresFileExtensions()

$uri = $this->createFileUri('ParserFactory/File/Test.php');

$phpmd = $this->getMock('PHPMD\\PHPMD', array('getFileExtensions', 'getInput'));
$phpmd = $this->getMockBuilder('PHPMD\\PHPMD')->setMethods(array('getFileExtensions', 'getInput'))->getMock();
$phpmd->expects($this->exactly(2))
->method('getFileExtensions')
->will($this->returnValue(array('.php')));
Expand Down
31 changes: 26 additions & 5 deletions src/test/php/PHPMD/ParserTest.php
Expand Up @@ -17,7 +17,11 @@

namespace PHPMD;

use PDepend\Metrics\AnalyzerFactory;
use PDepend\Source\Parser\InvalidStateException;
use PDepend\Util\Cache\CacheFactory;
use PDepend\Util\Configuration;
use Symfony\Component\DependencyInjection\Container;

/**
* Test case for the PHP_Depend backend adapter class.
Expand Down Expand Up @@ -147,7 +151,16 @@ public function testParserStoreParsingExceptionsInReport()
*/
private function getPHPDependMock()
{
return $this->getMock('PDepend\Engine', array(), array(null), '', false);
$container = new Container();
$config = new Configuration((object) array());

return $this->getMockBuilder('PDepend\Engine')
->setConstructorArgs(array(
$config,
new CacheFactory($config),
new AnalyzerFactory($container),
))
->getMock();
}

/**
Expand All @@ -157,7 +170,9 @@ private function getPHPDependMock()
*/
protected function getPHPDependClassMock()
{
$class = $this->getMock('PDepend\\Source\\AST\\ASTClass', array(), array(null));
$class = $this->getMockBuilder('PDepend\\Source\\AST\\ASTClass')
->setConstructorArgs(array(null))
->getMock();
$class->expects($this->any())
->method('getCompilationUnit')
->will($this->returnValue($this->getPHPDependFileMock('foo.php')));
Expand All @@ -182,7 +197,9 @@ protected function getPHPDependClassMock()
*/
protected function getPHPDependFunctionMock($fileName = '/foo/bar.php')
{
$function = $this->getMock('PDepend\Source\AST\ASTFunction', array(), array(null));
$function = $this->getMockBuilder('PDepend\Source\AST\ASTFunction')
->setConstructorArgs(array(null))
->getMock();
$function->expects($this->atLeastOnce())
->method('getCompilationUnit')
->will($this->returnValue($this->getPHPDependFileMock($fileName)));
Expand All @@ -198,7 +215,9 @@ protected function getPHPDependFunctionMock($fileName = '/foo/bar.php')
*/
protected function getPHPDependMethodMock($fileName = '/foo/bar.php')
{
$method = $this->getMock('PDepend\Source\AST\ASTMethod', array(), array(null));
$method = $this->getMockBuilder('PDepend\Source\AST\ASTMethod')
->setConstructorArgs(array(null))
->getMock();
$method->expects($this->atLeastOnce())
->method('getCompilationUnit')
->will($this->returnValue($this->getPHPDependFileMock($fileName)));
Expand All @@ -214,7 +233,9 @@ protected function getPHPDependMethodMock($fileName = '/foo/bar.php')
*/
protected function getPHPDependFileMock($fileName)
{
$file = $this->getMock('PDepend\Source\AST\ASTCompilationUnit', array(), array(null));
$file = $this->getMockBuilder('PDepend\Source\AST\ASTCompilationUnit')
->setConstructorArgs(array(null))
->getMock();
$file->expects($this->any())
->method('getFileName')
->will($this->returnValue($fileName));
Expand Down
15 changes: 8 additions & 7 deletions src/test/php/PHPMD/Renderer/AnsiRendererTest.php
Expand Up @@ -70,16 +70,17 @@ public function testRendererOutputsForReportWithContents()
$renderer->renderReport($report);
$renderer->end();

$expectedChunks = [
$expectedChunks = array(
PHP_EOL . "FILE: /bar.php" . PHP_EOL . "--------------" . PHP_EOL,
" 1 | \e[31mVIOLATION\e[0m | Test description" . PHP_EOL,
PHP_EOL . "FILE: /foo.php" . PHP_EOL . "--------------" . PHP_EOL,
" 2 | \e[31mVIOLATION\e[0m | Test description" . PHP_EOL,
" 3 | \e[31mVIOLATION\e[0m | Test description" . PHP_EOL,
PHP_EOL . "\e[33mERROR\e[0m while parsing /foo/baz.php" . PHP_EOL . "--------------------------------" . PHP_EOL,
PHP_EOL . "\e[33mERROR\e[0m while parsing /foo/baz.php" . PHP_EOL . "--------------------------------" .
(version_compare(PHP_VERSION, '5.4.0-dev', '<') ? '--' : '') . PHP_EOL,
"Error in file \"/foo/baz.php\"" . PHP_EOL,
PHP_EOL . "Found 3 violations and 1 error in 200ms" . PHP_EOL,
];
);

foreach($writer->getChunks() as $i => $chunk) {
$this->assertEquals(
Expand All @@ -102,7 +103,7 @@ public function testRendererOutputsForReportWithoutContents()
$report = $this->getReportMock(0);
$report->expects($this->atLeastOnce())
->method('getRuleViolations')
->will($this->returnValue(new \ArrayIterator([])));
->will($this->returnValue(new \ArrayIterator(array())));
$report->expects($this->atLeastOnce())
->method('isEmpty')
->will($this->returnValue(true));
Expand All @@ -111,7 +112,7 @@ public function testRendererOutputsForReportWithoutContents()
->will($this->returnValue(false));
$report->expects($this->atLeastOnce())
->method('getErrors')
->will($this->returnValue(new \ArrayIterator([])));
->will($this->returnValue(new \ArrayIterator(array())));
$report->expects($this->once())
->method('getElapsedTimeInMillis')
->will($this->returnValue(200));
Expand All @@ -123,10 +124,10 @@ public function testRendererOutputsForReportWithoutContents()
$renderer->renderReport($report);
$renderer->end();

$expectedChunks = [
$expectedChunks = array(
PHP_EOL . "Found 0 violations and 0 errors in 200ms" . PHP_EOL,
PHP_EOL . "\e[32mNo mess detected\e[0m" . PHP_EOL,
];
);

foreach($writer->getChunks() as $i => $chunk) {
$this->assertEquals(
Expand Down

0 comments on commit df1bdf6

Please sign in to comment.