From f897e36e9695296a05dce114bad4fb61cbdbe429 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 02:18:41 +0100 Subject: [PATCH 1/8] [constant-trickery] Turn existing BigIntegerTest into an abstract TestCase. --- tests/Math/BigInteger/BigIntegerTest.php | 11 +++++++++++ .../{BigIntegerTest.php => BigInteger/TestCase.php} | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/Math/BigInteger/BigIntegerTest.php rename tests/Math/{BigIntegerTest.php => BigInteger/TestCase.php} (98%) diff --git a/tests/Math/BigInteger/BigIntegerTest.php b/tests/Math/BigInteger/BigIntegerTest.php new file mode 100644 index 00000000..e13fd22d --- /dev/null +++ b/tests/Math/BigInteger/BigIntegerTest.php @@ -0,0 +1,11 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Math_BigInteger_BigIntegerTest extends Math_BigInteger_TestCase +{ + +} diff --git a/tests/Math/BigIntegerTest.php b/tests/Math/BigInteger/TestCase.php similarity index 98% rename from tests/Math/BigIntegerTest.php rename to tests/Math/BigInteger/TestCase.php index 1317609f..793b32b7 100644 --- a/tests/Math/BigIntegerTest.php +++ b/tests/Math/BigInteger/TestCase.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Math_BigIntegerTest extends PHPUnit_Framework_TestCase +abstract class Math_BigInteger_TestCase extends PHPUnit_Framework_TestCase { public function getInstance($x = 0, $base = 10) { From 2c3b165556f4a622412c23ab7028b6eb01fbdb74 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 02:34:20 +0100 Subject: [PATCH 2/8] [constant-trickery] Add PhpseclibTestCase. No longer use PHPUnit directly. --- tests/Crypt/AES/TestCase.php | 2 +- tests/Crypt/Hash/TestCase.php | 2 +- tests/Math/BigInteger/TestCase.php | 2 +- tests/PhpseclibTestCase.php | 11 +++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 tests/PhpseclibTestCase.php diff --git a/tests/Crypt/AES/TestCase.php b/tests/Crypt/AES/TestCase.php index d5884129..f845e4bb 100644 --- a/tests/Crypt/AES/TestCase.php +++ b/tests/Crypt/AES/TestCase.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -abstract class Crypt_AES_TestCase extends PHPUnit_Framework_TestCase +abstract class Crypt_AES_TestCase extends PhpseclibTestCase { static public function setUpBeforeClass() { diff --git a/tests/Crypt/Hash/TestCase.php b/tests/Crypt/Hash/TestCase.php index 1489acd7..5f61244c 100644 --- a/tests/Crypt/Hash/TestCase.php +++ b/tests/Crypt/Hash/TestCase.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -abstract class Crypt_Hash_TestCase extends PHPUnit_Framework_TestCase +abstract class Crypt_Hash_TestCase extends PhpseclibTestCase { static public function setUpBeforeClass() { diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index 793b32b7..50475cdf 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -abstract class Math_BigInteger_TestCase extends PHPUnit_Framework_TestCase +abstract class Math_BigInteger_TestCase extends PhpseclibTestCase { public function getInstance($x = 0, $base = 10) { diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php new file mode 100644 index 00000000..dfe12719 --- /dev/null +++ b/tests/PhpseclibTestCase.php @@ -0,0 +1,11 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase +{ + +} From 2ad6f71002d42c09a03a9970fdc2ec966651f64c Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 02:48:19 +0100 Subject: [PATCH 3/8] [constant-trickery] Provide infrastructure for setting mode constants. --- tests/PhpseclibTestCase.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index dfe12719..8b5b1f80 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -7,5 +7,31 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase { + /** + * @param string $constant + * @param mixed $expected + * + * @return null + */ + static protected function ensureModeConstant($constant, $expected) + { + if (defined($constant)) + { + $value = constant($constant); + if ($value !== $expected) + { + self::markTestSkipped(sprintf( + "Skipping test because mode constant %s is %s instead of %s", + $constant, + $value, + $expected + )); + } + } + else + { + define($constant, $expected); + } + } } From b83ca1024648ecb308af51a7a5d06dccc6f3d781 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 02:57:21 +0100 Subject: [PATCH 4/8] [constant-trickery] Add extra test cases for BCMath and GMP. --- tests/Math/BigInteger/BCMathTest.php | 15 +++++++++++++++ tests/Math/BigInteger/BigIntegerTest.php | 11 ----------- tests/Math/BigInteger/GMPTest.php | 15 +++++++++++++++ tests/Math/BigInteger/InternalTest.php | 15 +++++++++++++++ tests/Math/BigInteger/TestCase.php | 2 ++ 5 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 tests/Math/BigInteger/BCMathTest.php delete mode 100644 tests/Math/BigInteger/BigIntegerTest.php create mode 100644 tests/Math/BigInteger/GMPTest.php create mode 100644 tests/Math/BigInteger/InternalTest.php diff --git a/tests/Math/BigInteger/BCMathTest.php b/tests/Math/BigInteger/BCMathTest.php new file mode 100644 index 00000000..7d953b9e --- /dev/null +++ b/tests/Math/BigInteger/BCMathTest.php @@ -0,0 +1,15 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Math_BigInteger_BCMathTest extends Math_BigInteger_TestCase +{ + static public function setUpBeforeClass() + { + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); + parent::setUpBeforeClass(); + } +} diff --git a/tests/Math/BigInteger/BigIntegerTest.php b/tests/Math/BigInteger/BigIntegerTest.php deleted file mode 100644 index e13fd22d..00000000 --- a/tests/Math/BigInteger/BigIntegerTest.php +++ /dev/null @@ -1,11 +0,0 @@ - - * @copyright MMXIII Andreas Fischer - * @license http://www.opensource.org/licenses/mit-license.html MIT License - */ - -class Math_BigInteger_BigIntegerTest extends Math_BigInteger_TestCase -{ - -} diff --git a/tests/Math/BigInteger/GMPTest.php b/tests/Math/BigInteger/GMPTest.php new file mode 100644 index 00000000..2aea998b --- /dev/null +++ b/tests/Math/BigInteger/GMPTest.php @@ -0,0 +1,15 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Math_BigInteger_GMPTest extends Math_BigInteger_TestCase +{ + static public function setUpBeforeClass() + { + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); + parent::setUpBeforeClass(); + } +} diff --git a/tests/Math/BigInteger/InternalTest.php b/tests/Math/BigInteger/InternalTest.php new file mode 100644 index 00000000..dcdc9129 --- /dev/null +++ b/tests/Math/BigInteger/InternalTest.php @@ -0,0 +1,15 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Math_BigInteger_InternalTest extends Math_BigInteger_TestCase +{ + static public function setUpBeforeClass() + { + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL); + parent::setUpBeforeClass(); + } +} diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index 50475cdf..74a92663 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -5,6 +5,8 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ +require_once 'Math/BigInteger.php'; + abstract class Math_BigInteger_TestCase extends PhpseclibTestCase { public function getInstance($x = 0, $base = 10) From 55e9f4d7effefd2f577f56b1f4a170e33cd9dbbc Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 03:03:07 +0100 Subject: [PATCH 5/8] [constant-trickery] Add runkit to PHP. Thanks to kherge for those lines. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 9f4ca74c..18d8cbea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,9 @@ php: - 5.3 - 5.4 +before_script: + - git clone git://github.com/zenovich/runkit.git && cd runkit && phpize && ./configure && make && make install && cd .. + - echo "extension=runkit.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` + script: - phpunit --verbose From 306f0711eda76a96a4cf8be0ea37f55248320df1 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 03:03:29 +0100 Subject: [PATCH 6/8] [constant-trickery] Try to use runkit to redefine mode constants. --- tests/PhpseclibTestCase.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index 8b5b1f80..196e87dd 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -21,12 +21,26 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase if ($value !== $expected) { - self::markTestSkipped(sprintf( - "Skipping test because mode constant %s is %s instead of %s", - $constant, - $value, - $expected - )); + if (function_exists('runkit_constant_redefine')) + { + if (!runkit_constant_redefine($constant, $expected)) + { + self::markTestSkipped(sprintf( + "Failed to redefine mode constant %s to %s", + $constant, + $expected + )); + } + } + else + { + self::markTestSkipped(sprintf( + "Skipping test because mode constant %s is %s instead of %s", + $constant, + $value, + $expected + )); + } } } else From 3c75fa1ad4f041755807d7f90d16af39ce123a58 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 03:07:41 +0100 Subject: [PATCH 7/8] [constant-trickery] Also skip tests when required extension is missing. --- tests/Math/BigInteger/BCMathTest.php | 5 +++++ tests/Math/BigInteger/GMPTest.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/tests/Math/BigInteger/BCMathTest.php b/tests/Math/BigInteger/BCMathTest.php index 7d953b9e..6440550b 100644 --- a/tests/Math/BigInteger/BCMathTest.php +++ b/tests/Math/BigInteger/BCMathTest.php @@ -9,6 +9,11 @@ class Math_BigInteger_BCMathTest extends Math_BigInteger_TestCase { static public function setUpBeforeClass() { + if (!extension_loaded('bcmath')) + { + self::markTestSkipped('BCMath extension is not available.'); + } + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); parent::setUpBeforeClass(); } diff --git a/tests/Math/BigInteger/GMPTest.php b/tests/Math/BigInteger/GMPTest.php index 2aea998b..c8895271 100644 --- a/tests/Math/BigInteger/GMPTest.php +++ b/tests/Math/BigInteger/GMPTest.php @@ -9,6 +9,11 @@ class Math_BigInteger_GMPTest extends Math_BigInteger_TestCase { static public function setUpBeforeClass() { + if (!extension_loaded('gmp')) + { + self::markTestSkipped('GNU Multiple Precision (GMP) extension is not available.'); + } + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); parent::setUpBeforeClass(); } From f62a9114b385a069e6d5b30ceffebb1d696263c1 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 12 Jan 2013 21:22:01 +0100 Subject: [PATCH 8/8] [constant-trickery] Reimport class definition to clear static variables. --- tests/Math/BigInteger/BCMathTest.php | 3 ++- tests/Math/BigInteger/GMPTest.php | 3 ++- tests/Math/BigInteger/InternalTest.php | 3 ++- tests/Math/BigInteger/TestCase.php | 7 +++++++ tests/PhpseclibTestCase.php | 23 +++++++++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tests/Math/BigInteger/BCMathTest.php b/tests/Math/BigInteger/BCMathTest.php index 6440550b..08d4b3fe 100644 --- a/tests/Math/BigInteger/BCMathTest.php +++ b/tests/Math/BigInteger/BCMathTest.php @@ -14,7 +14,8 @@ class Math_BigInteger_BCMathTest extends Math_BigInteger_TestCase self::markTestSkipped('BCMath extension is not available.'); } - self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); parent::setUpBeforeClass(); + + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); } } diff --git a/tests/Math/BigInteger/GMPTest.php b/tests/Math/BigInteger/GMPTest.php index c8895271..fcd898d1 100644 --- a/tests/Math/BigInteger/GMPTest.php +++ b/tests/Math/BigInteger/GMPTest.php @@ -14,7 +14,8 @@ class Math_BigInteger_GMPTest extends Math_BigInteger_TestCase self::markTestSkipped('GNU Multiple Precision (GMP) extension is not available.'); } - self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); parent::setUpBeforeClass(); + + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); } } diff --git a/tests/Math/BigInteger/InternalTest.php b/tests/Math/BigInteger/InternalTest.php index dcdc9129..1ca2ef74 100644 --- a/tests/Math/BigInteger/InternalTest.php +++ b/tests/Math/BigInteger/InternalTest.php @@ -9,7 +9,8 @@ class Math_BigInteger_InternalTest extends Math_BigInteger_TestCase { static public function setUpBeforeClass() { - self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL); parent::setUpBeforeClass(); + + self::ensureModeConstant('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL); } } diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index 74a92663..01edc475 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -9,6 +9,13 @@ require_once 'Math/BigInteger.php'; abstract class Math_BigInteger_TestCase extends PhpseclibTestCase { + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::reRequireFile('Math/BigInteger.php'); + } + public function getInstance($x = 0, $base = 10) { return new Math_BigInteger($x, $base); diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index 196e87dd..e69aa25a 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -48,4 +48,27 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase define($constant, $expected); } } + + /** + * @param string $filename + * + * @return null + */ + static protected function reRequireFile($filename) + { + if (function_exists('runkit_import')) + { + $result = runkit_import( + $filename, + RUNKIT_IMPORT_FUNCTIONS | + RUNKIT_IMPORT_CLASS_METHODS | + RUNKIT_IMPORT_OVERRIDE + ); + + if (!$result) + { + self::markTestSkipped("Failed to reimport file $filename"); + } + } + } }