Skip to content

Commit

Permalink
Add all PHP 7 unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
monque committed Aug 26, 2016
1 parent 12d096a commit 35fc175
Show file tree
Hide file tree
Showing 22 changed files with 388 additions and 71 deletions.
4 changes: 2 additions & 2 deletions doc/migration70.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Backward incompatible changes:
-
name: Changes to the handling of indirect variables, properties, and methods
status: CHECK
check: v7dot0/Precedence
check: v7dot0/ParseDifference
-
name: list() no longer assigns variables in reverse order
status: CHECK
Expand Down Expand Up @@ -152,7 +152,7 @@ Backward incompatible changes:
-
name: yield is now a right associative operator
status: CHECK
check: v7dot0/Precedence
check: v7dot0/ParseDifference
-
name: Functions cannot have multiple parameters with the same name
status: CHECK
Expand Down
14 changes: 8 additions & 6 deletions src/Changes/v7dot0/Deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@ public function leaveNode($node)
*
* @see http://php.net/manual/en/migration70.deprecated.php#migration70.deprecated.php4-constructors
*/
if ($node instanceof Stmt\ClassMethod) {
if ($node->name == $this->visitor->getClassName()) {
$this->addSpot('DEPRECATED', true, 'PHP 4 style constructor is deprecated');
}
if ($node instanceof Stmt\ClassMethod &&
$node->migName == $this->visitor->getClass()->name) {
$this->addSpot('DEPRECATED', true, 'PHP 4 style constructor is deprecated');

/**
* password_hash() salt option
*
* @see http://php.net/manual/en/migration70.deprecated.php#migration70.deprecated.pwshash-salt-option
*/
} elseif ($node instanceof Expr\FuncCall && ParserHelper::isSameFunc($node->name, 'password_hash') && isset($node->args[2])) {
} elseif ($node instanceof Expr\FuncCall &&
ParserHelper::isSameFunc($node->migName, 'password_hash') &&
isset($node->args[2])) {
$this->addSpot('DEPRECATED', false, 'salt option for password_hash() is deprecated');

/**
* LDAP deprecations
*
* @see http://php.net/manual/en/migration70.deprecated.php#migration70.deprecated.ldap
*/
} elseif ($node instanceof Expr\FuncCall && ParserHelper::isSameFunc($node->name, 'ldap_sort')) {
} elseif ($node instanceof Expr\FuncCall &&
ParserHelper::isSameFunc($node->migName, 'ldap_sort')) {
$this->addSpot('DEPRECATED', true, 'ldap_sort() is deprecated');
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/Changes/v7dot0/ExceptionHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ public function leaveNode($node)
*
* @see http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.error-handling
*/
if ($node instanceof Expr\FuncCall && ParserHelper::isSameFunc($node->name, 'set_exception_handler')) {
if (!isset($node->args[0])) {
return;
}
if (!$node instanceof Expr\FuncCall ||
!ParserHelper::isSameFunc($node->name, 'set_exception_handler') ||
!isset($node->args[0])) {
return;
}

$affected = true;
$certain = false;
$callback = $node->args[0]->value;
$affected = true;
$certain = false;
$callback = $node->args[0]->value;

if ($callback instanceof Expr\Closure) {
if (!isset($callback->params[0]) || !isset($callback->params[0]->type)) {
$affected = false;
$certain = true;
} elseif (ParserHelper::isSameClass($callback->params[0]->type, 'Exception')) {
$affected = true;
$certain = true;
}
if ($callback instanceof Expr\Closure) {
if (!isset($callback->params[0]) || !isset($callback->params[0]->type)) {
$affected = false;
$certain = true;
} elseif (ParserHelper::isSameClass($callback->params[0]->type, 'Exception')) {
$affected = true;
$certain = true;
}
}

if ($affected) {
$this->addSpot('WARNING', $certain, 'set_exception_handler() is no longer guaranteed to receive Exception objects');
}
if ($affected) {
$this->addSpot('WARNING', $certain, 'set_exception_handler() is no longer guaranteed to receive Exception objects');
}
}
}
6 changes: 4 additions & 2 deletions src/Changes/v7dot0/IntegerOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public function leaveNode($node)
*
* @see http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.integers.negative-bitshift
*/
if ($node instanceof Expr\BinaryOp\ShiftLeft || $node instanceof Expr\BinaryOp\ShiftRight) {
if ($node instanceof Expr\BinaryOp\ShiftLeft ||
$node instanceof Expr\BinaryOp\ShiftRight) {
$affect = true;
$certain = false;

if ($node->right instanceof Scalar\LNumber) {
$affect = false;
} elseif ($node->right instanceof Expr\UnaryMinus && $node->right->expr instanceof Scalar\LNumber) {
} elseif ($node->right instanceof Expr\UnaryMinus &&
$node->right->expr instanceof Scalar\LNumber) {
$certain = true;
}

Expand Down
20 changes: 11 additions & 9 deletions src/Changes/v7dot0/Introduced.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ class Introduced extends AbstractIntroduced

/** @see http://php.net/manual/en/migration70.new-functions.php */
protected $funcTable = [
// Closure
'Closure::call',

// CSPRNG
'random_bytes', 'random_int',

// Error Handling and Logging
'error_clear_last',

// Generator
'Generator::getReturn',

// GNU Multiple Precision
'gmp_random_seed',

Expand All @@ -36,16 +30,24 @@ class Introduced extends AbstractIntroduced
// POSIX
'posix_setrlimit',

// Zlib Compression
'inflate_add', 'deflate_add', 'inflate_init', 'deflate_init',
];

protected $methodTable = [
// Generator
'Generator::getReturn',

// Closure
'Closure::call',

// Reflection
'ReflectionParameter::getType', 'ReflectionParameter::hasType',
'ReflectionFunctionAbstract::getReturnType',
'ReflectionFunctionAbstract::hasReturnType',

// Zip
'ZipArchive::setCompressionIndex', 'ZipArchive::setCompressionName',

// Zlib Compression
'inflate_add', 'deflate_add', 'inflate_init', 'deflate_init',
];

/** @see http://php.net/manual/en/migration70.classes.php */
Expand Down
2 changes: 1 addition & 1 deletion src/Changes/v7dot0/KeywordReserved.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function leaveNode($node)
return;
}

$name = $node->namespacedName->toString();
$name = $node->name;

if ($this->forbiddenTable->has($name)) {
$this->addSpot('FATAL', true, 'Keyword "'.$name.'" cannot be used to name class-like');
Expand Down
12 changes: 12 additions & 0 deletions tests/Changes/AbstractIntroducedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function testNewFunc()
$code = sprintf("function %s() {}", strtoupper($name));
$this->assertHasSpot($code);

// Namespaced
$code = sprintf("namespace Dummy; function %s() {}", $name);
$this->assertNotSpot($code);

// Conditional name
$code = sprintf("if (!function_exists('%s')) { function %s() {} }", $name, $name);
$this->assertNotSpot($code);
Expand Down Expand Up @@ -64,6 +68,10 @@ public function testNewMethod()
// Case Insensitive name
$code = $this->genMethod(strtoupper($class), strtoupper($method));
$this->assertHasSpot($code);

// Namespaced
$code = 'namespace Dummy; '.$this->genMethod($class, $method);
$this->assertNotSpot($code);
}
}

Expand All @@ -86,6 +94,10 @@ public function testNewClass()
$code = sprintf("class %s {}", strtoupper($name));
$this->assertHasSpot($code);

// Namespaced
$code = sprintf("namespace Dummy; class %s {}", $name);
$this->assertNotSpot($code);

// Conditional name
// Removed, because of autoload it's too rare to see
// $code = sprintf("if (!class_exists('%s')) { class %s {} }", $name, $name);
Expand Down
23 changes: 23 additions & 0 deletions tests/Changes/AbstractRemovedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function testFunc()
// Case Insensitive name
$code = sprintf("%s();", strtoupper($name));
$this->assertHasSpot($code);

// Namespaced
$code = sprintf("use Dummy as %s; %s();", $name, $name);
$this->assertHasSpot($code);

$code = sprintf("dummy\%s();", $name, $name);
$this->assertNotSpot($code);
}
}

Expand All @@ -55,4 +62,20 @@ public function testConst()
$this->assertNotSpot($code);
}
}

public function testVar()
{
// Not-new
$code = '$not_removed;';
$this->assertNotSpot($code);

$table = TestHelper::fetchProperty($this->change, 'varTable');
if (is_null($table)) {
return;
}
foreach ($table as $name => $dummy) {
$code = '$'.$name.';';
$this->assertHasSpot($code);
}
}
}
36 changes: 36 additions & 0 deletions tests/Changes/v7dot0/DeprecatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace PhpMigration\Changes\v7dot0;

use PhpMigration\Changes\AbstractChangeTest;

class DeprecatedTest extends AbstractChangeTest
{
public function testOldConstructor()
{
$this->assertHasSpot('class OldClass { function OldClass() {} }');

$this->assertHasSpot('namespace Dummy; class OldClass { function OldClass() {} }');

$this->assertNotSpot('class OldClass { function setName() {} }');

$this->assertNotSpot('class OldClass { function _construct() {} }');
}

public function testPasswordHash()
{
$this->assertHasSpot('password_hash($a, $b, $c);');

$this->assertHasSpot('password_hash($a, $b, []);');

$this->assertNotSpot('password_hash($a, $b);');

$this->assertNotSpot('Dummy\password_hash($a, $b, []);');
}

public function testLdapSort()
{
$this->assertHasSpot('ldap_sort();');

$this->assertNotSpot('Dummy\ldap_sort();');
}
}
26 changes: 26 additions & 0 deletions tests/Changes/v7dot0/ExceptionHandleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace PhpMigration\Changes\v7dot0;

use PhpMigration\Changes\AbstractChangeTest;

class ExceptionHandleTest extends AbstractChangeTest
{
public function test()
{
$this->assertNotSpot('set_exception_handler();');

$this->assertHasSpot('set_exception_handler($handler);');

$this->assertHasSpot('set_exception_handler([$this, "handler"]);');

$this->assertNotSpot('set_exception_handler(function () {});');

$this->assertNotSpot('set_exception_handler(function ($e) {});');

$this->assertHasSpot('set_exception_handler(function (Exception $e) {});');

$this->assertHasSpot('set_exception_handler(function (\Exception $e) {});');

$this->assertHasSpot('use Dummy\Exception; set_exception_handler(function (Exception $e) {});');
}
}
26 changes: 26 additions & 0 deletions tests/Changes/v7dot0/ForeachLoopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace PhpMigration\Changes\v7dot0;

use PhpMigration\Changes\AbstractChangeTest;

class ForeachLoopTest extends AbstractChangeTest
{
public function test()
{
$this->assertHasSpot('foreach ($arr as &$i) { current(); }');

$this->assertHasSpot('foreach ($arr as &$i) { key(); }');

$this->assertNotSpot('foreach ($arr as &$i) { end(); }');

$this->assertHasSpot('foreach ($arr as $key => &$i) { key(); }');

$this->assertNotSpot('foreach ($arr as $key => $i) { key(); }');

$this->assertNotSpot('foreach ($arr as $key => $i) {}');

$this->assertHasSpot('foreach ($a as $b) {foreach ($c as &$i) { key(); }}');

$this->assertHasSpot('foreach ($a as &$b) {foreach ($c as $i) { key(); }}');
}
}
2 changes: 2 additions & 0 deletions tests/Changes/v7dot0/FuncListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public function testVarOrder()
{
$this->assertHasSpot('list($a[], $a[], $a[]) = [1, 2, 3];');

$this->assertHasSpot('list($a[], $b, $a[]) = [1, 2, 3];');

$this->assertNotSpot('list($a, $b) = [1, 2];');
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Changes/v7dot0/FuncParametersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace PhpMigration\Changes\v7dot0;

use PhpMigration\Changes\AbstractChangeTest;

class FuncParametersTest extends AbstractChangeTest
{
public function test()
{
$this->assertHasSpot('function f($a, $a) {}');

$this->assertHasSpot('class C { function f($a, $a) {} }');

$this->assertHasSpot('$a = function ($a, $a) {};');

$this->assertHasSpot('function f($a, $b, $a) {}');

$this->assertHasSpot('class C { function f($a, $b, $a) {} }');

$this->assertHasSpot('$a = function ($a, $b, $a) {};');

$this->assertNotSpot('function f() {}');

$this->assertNotSpot('function f($a, $b) {}');
}
}
Loading

0 comments on commit 35fc175

Please sign in to comment.