From 9d9c38996c86806fafd28f95c743cc1f351b9013 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sat, 16 Mar 2013 00:38:59 +0100 Subject: [PATCH] [Intl] Simplified tests --- .../Tests/Collator/AbstractCollatorTest.php | 3 +- .../Collator/Verification/CollatorTest.php | 3 +- .../AbstractIntlDateFormatterTest.php | 3 +- .../Verification/IntlDateFormatterTest.php | 3 +- .../Tests/Globals/AbstractIntlGlobalsTest.php | 3 +- .../Globals/Verification/IntlGlobalsTest.php | 3 +- .../Component/Intl/Tests/IntlTestCase.php | 59 ------------- .../Intl/Tests/Locale/AbstractLocaleTest.php | 3 +- .../Tests/Locale/Verification/LocaleTest.php | 3 +- .../AbstractNumberFormatterTest.php | 12 +-- .../Verification/NumberFormatterTest.php | 3 +- .../Reader/BinaryBundleReaderTest.php | 6 +- .../Component/Intl/Util/IntlTestHelper.php | 87 +++++++++++++++++-- 13 files changed, 103 insertions(+), 88 deletions(-) delete mode 100644 src/Symfony/Component/Intl/Tests/IntlTestCase.php diff --git a/src/Symfony/Component/Intl/Tests/Collator/AbstractCollatorTest.php b/src/Symfony/Component/Intl/Tests/Collator/AbstractCollatorTest.php index 243e5e243d..319ff18793 100644 --- a/src/Symfony/Component/Intl/Tests/Collator/AbstractCollatorTest.php +++ b/src/Symfony/Component/Intl/Tests/Collator/AbstractCollatorTest.php @@ -13,14 +13,13 @@ namespace Symfony\Component\Intl\Tests\Collator; use Symfony\Component\Intl\Collator\StubCollator; use Symfony\Component\Intl\Locale; -use Symfony\Component\Intl\Tests\IntlTestCase; /** * Test case for Collator implementations. * * @author Bernhard Schussek */ -abstract class AbstractCollatorTest extends IntlTestCase +abstract class AbstractCollatorTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider asortProvider diff --git a/src/Symfony/Component/Intl/Tests/Collator/Verification/CollatorTest.php b/src/Symfony/Component/Intl/Tests/Collator/Verification/CollatorTest.php index 1add4da466..c8dbc13151 100644 --- a/src/Symfony/Component/Intl/Tests/Collator/Verification/CollatorTest.php +++ b/src/Symfony/Component/Intl/Tests/Collator/Verification/CollatorTest.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Intl\Tests\Collator\Verification; use Symfony\Component\Intl\Locale; use Symfony\Component\Intl\Tests\Collator\AbstractCollatorTest; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * Verifies that {@link AbstractCollatorTest} matches the behavior of the @@ -24,7 +25,7 @@ class CollatorTest extends AbstractCollatorTest { protected function setUp() { - $this->skipIfIntlExtensionNotLoaded(); + IntlTestHelper::requireFullIntl($this); parent::setUp(); } diff --git a/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php b/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php index 115757aebe..61c34f023a 100644 --- a/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php @@ -14,7 +14,6 @@ namespace Symfony\Component\Intl\Tests\DateFormatter; use Symfony\Component\Intl\DateFormatter\StubIntlDateFormatter; use Symfony\Component\Intl\Globals\StubIntlGlobals; use Symfony\Component\Intl\Intl; -use Symfony\Component\Intl\Tests\IntlTestCase; use Symfony\Component\Intl\Util\IcuVersion; use Symfony\Component\Intl\Util\Version; @@ -23,7 +22,7 @@ use Symfony\Component\Intl\Util\Version; * * @author Bernhard Schussek */ -abstract class AbstractIntlDateFormatterTest extends IntlTestCase +abstract class AbstractIntlDateFormatterTest extends \PHPUnit_Framework_TestCase { /** * When a time zone is not specified, it uses the system default however it returns null in the getter method diff --git a/src/Symfony/Component/Intl/Tests/DateFormatter/Verification/IntlDateFormatterTest.php b/src/Symfony/Component/Intl/Tests/DateFormatter/Verification/IntlDateFormatterTest.php index 25be488e37..d55163c017 100644 --- a/src/Symfony/Component/Intl/Tests/DateFormatter/Verification/IntlDateFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/DateFormatter/Verification/IntlDateFormatterTest.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Intl\Tests\DateFormatter\Verification; use Symfony\Component\Intl\DateFormatter\StubIntlDateFormatter; use Symfony\Component\Intl\Tests\DateFormatter\AbstractIntlDateFormatterTest; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * Verifies that {@link AbstractIntlDateFormatterTest} matches the behavior of @@ -24,7 +25,7 @@ class IntlDateFormatterTest extends AbstractIntlDateFormatterTest { protected function setUp() { - $this->skipIfIntlExtensionNotLoaded(); + IntlTestHelper::requireFullIntl($this); parent::setUp(); } diff --git a/src/Symfony/Component/Intl/Tests/Globals/AbstractIntlGlobalsTest.php b/src/Symfony/Component/Intl/Tests/Globals/AbstractIntlGlobalsTest.php index 5d8ef3d442..ffc9142a51 100644 --- a/src/Symfony/Component/Intl/Tests/Globals/AbstractIntlGlobalsTest.php +++ b/src/Symfony/Component/Intl/Tests/Globals/AbstractIntlGlobalsTest.php @@ -11,14 +11,13 @@ namespace Symfony\Component\Intl\Tests\Globals; -use Symfony\Component\Intl\Tests\IntlTestCase; /** * Test case for intl function implementations. * * @author Bernhard Schussek */ -abstract class AbstractIntlGlobalsTest extends IntlTestCase +abstract class AbstractIntlGlobalsTest extends \PHPUnit_Framework_TestCase { public function errorNameProvider() { diff --git a/src/Symfony/Component/Intl/Tests/Globals/Verification/IntlGlobalsTest.php b/src/Symfony/Component/Intl/Tests/Globals/Verification/IntlGlobalsTest.php index 1b1869ca42..c46033d880 100644 --- a/src/Symfony/Component/Intl/Tests/Globals/Verification/IntlGlobalsTest.php +++ b/src/Symfony/Component/Intl/Tests/Globals/Verification/IntlGlobalsTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Intl\Tests\Globals\Verification; use Symfony\Component\Intl\Tests\Globals\AbstractIntlGlobalsTest; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * Verifies that {@link AbstractIntlGlobalsTest} matches the behavior of the @@ -23,7 +24,7 @@ class IntlGlobalsTest extends AbstractIntlGlobalsTest { protected function setUp() { - $this->skipIfIntlExtensionNotLoaded(); + IntlTestHelper::requireFullIntl($this); parent::setUp(); } diff --git a/src/Symfony/Component/Intl/Tests/IntlTestCase.php b/src/Symfony/Component/Intl/Tests/IntlTestCase.php deleted file mode 100644 index fe16c9ff7c..0000000000 --- a/src/Symfony/Component/Intl/Tests/IntlTestCase.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Intl\Tests; - -use Symfony\Component\Intl\Intl; -use Symfony\Component\Intl\Util\IcuVersion; -use Symfony\Component\Intl\Util\Version; - -/** - * Base test case for the Intl component. - * - * @author Bernhard Schussek - */ -abstract class IntlTestCase extends \PHPUnit_Framework_TestCase -{ - protected function is32Bit() - { - return PHP_INT_SIZE == 4; - } - - protected function is64Bit() - { - return PHP_INT_SIZE == 8; - } - - protected function skipIfIntlExtensionNotLoaded() - { - if (!Intl::isExtensionLoaded()) { - $this->markTestSkipped('The intl extension is not available.'); - } - - if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) { - $this->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion()); - } - } - - protected function skipIfNot32Bit() - { - if (!$this->is32Bit()) { - $this->markTestSkipped('PHP must be compiled in 32 bit mode to run this test'); - } - } - - protected function skipIfNot64Bit() - { - if (!$this->is64Bit()) { - $this->markTestSkipped('PHP must be compiled in 64 bit mode to run this test'); - } - } -} diff --git a/src/Symfony/Component/Intl/Tests/Locale/AbstractLocaleTest.php b/src/Symfony/Component/Intl/Tests/Locale/AbstractLocaleTest.php index 5a7979af3e..df87e1c9c1 100644 --- a/src/Symfony/Component/Intl/Tests/Locale/AbstractLocaleTest.php +++ b/src/Symfony/Component/Intl/Tests/Locale/AbstractLocaleTest.php @@ -12,14 +12,13 @@ namespace Symfony\Component\Intl\Tests\Locale; use Symfony\Component\Intl\Stub\StubLocale; -use Symfony\Component\Intl\Tests\IntlTestCase; /** * Test case for Locale implementations. * * @author Bernhard Schussek */ -abstract class AbstractLocaleTest extends IntlTestCase +abstract class AbstractLocaleTest extends \PHPUnit_Framework_TestCase { public function testSetDefault() { diff --git a/src/Symfony/Component/Intl/Tests/Locale/Verification/LocaleTest.php b/src/Symfony/Component/Intl/Tests/Locale/Verification/LocaleTest.php index c85fb4e511..39d4f3cb03 100644 --- a/src/Symfony/Component/Intl/Tests/Locale/Verification/LocaleTest.php +++ b/src/Symfony/Component/Intl/Tests/Locale/Verification/LocaleTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Intl\Tests\Locale\Verification; use Symfony\Component\Intl\Tests\Locale\AbstractLocaleTest; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * Verifies that {@link AbstractLocaleTest} matches the behavior of the @@ -23,7 +24,7 @@ class LocaleTest extends AbstractLocaleTest { protected function setUp() { - $this->skipIfIntlExtensionNotLoaded(); + IntlTestHelper::requireFullIntl($this); parent::setUp(); } diff --git a/src/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php b/src/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php index 7c06019b94..de534bc087 100644 --- a/src/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/NumberFormatter/AbstractNumberFormatterTest.php @@ -15,14 +15,14 @@ use Symfony\Component\Intl\Globals\StubIntlGlobals; use Symfony\Component\Intl\Intl; use Symfony\Component\Intl\Locale; use Symfony\Component\Intl\NumberFormatter\StubNumberFormatter; -use Symfony\Component\Intl\Tests\IntlTestCase; use Symfony\Component\Intl\Util\IcuVersion; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * Note that there are some values written like -2147483647 - 1. This is the lower 32bit int max and is a known * behavior of PHP. */ -abstract class AbstractNumberFormatterTest extends IntlTestCase +abstract class AbstractNumberFormatterTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider formatCurrencyWithDecimalStyleProvider @@ -547,7 +547,7 @@ abstract class AbstractNumberFormatterTest extends IntlTestCase public function testParseTypeInt64With32BitIntegerInPhp32Bit() { - $this->skipIfNot32Bit(); + IntlTestHelper::require32Bit($this); $formatter = $this->getNumberFormatter('en', StubNumberFormatter::DECIMAL); @@ -573,7 +573,7 @@ abstract class AbstractNumberFormatterTest extends IntlTestCase public function testParseTypeInt64With32BitIntegerInPhp64Bit() { - $this->skipIfNot64Bit(); + IntlTestHelper::require64Bit($this); $formatter = $this->getNumberFormatter('en', StubNumberFormatter::DECIMAL); @@ -591,7 +591,7 @@ abstract class AbstractNumberFormatterTest extends IntlTestCase */ public function testParseTypeInt64With64BitIntegerInPhp32Bit() { - $this->skipIfNot32Bit(); + IntlTestHelper::require32Bit($this); $formatter = $this->getNumberFormatter('en', StubNumberFormatter::DECIMAL); @@ -610,7 +610,7 @@ abstract class AbstractNumberFormatterTest extends IntlTestCase */ public function testParseTypeInt64With64BitIntegerInPhp64Bit() { - $this->skipIfNot64Bit(); + IntlTestHelper::require64Bit($this); $formatter = $this->getNumberFormatter('en', StubNumberFormatter::DECIMAL); diff --git a/src/Symfony/Component/Intl/Tests/NumberFormatter/Verification/NumberFormatterTest.php b/src/Symfony/Component/Intl/Tests/NumberFormatter/Verification/NumberFormatterTest.php index 5154e63c01..28e6fe9090 100644 --- a/src/Symfony/Component/Intl/Tests/NumberFormatter/Verification/NumberFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/NumberFormatter/Verification/NumberFormatterTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Intl\Tests\NumberFormatter\Verification; use Symfony\Component\Intl\Tests\NumberFormatter\AbstractNumberFormatterTest; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * Note that there are some values written like -2147483647 - 1. This is the lower 32bit int max and is a known @@ -21,7 +22,7 @@ class NumberFormatterTest extends AbstractNumberFormatterTest { protected function setUp() { - $this->skipIfIntlExtensionNotLoaded(); + IntlTestHelper::requireFullIntl($this); parent::setUp(); } diff --git a/src/Symfony/Component/Intl/Tests/ResourceBundle/Reader/BinaryBundleReaderTest.php b/src/Symfony/Component/Intl/Tests/ResourceBundle/Reader/BinaryBundleReaderTest.php index 037e1a3cd7..3aefbae7fd 100644 --- a/src/Symfony/Component/Intl/Tests/ResourceBundle/Reader/BinaryBundleReaderTest.php +++ b/src/Symfony/Component/Intl/Tests/ResourceBundle/Reader/BinaryBundleReaderTest.php @@ -12,12 +12,12 @@ namespace Symfony\Component\Intl\Tests\ResourceBundle\Reader; use Symfony\Component\Intl\ResourceBundle\Reader\BinaryBundleReader; -use Symfony\Component\Intl\Tests\IntlTestCase; +use Symfony\Component\Intl\Util\IntlTestHelper; /** * @author Bernhard Schussek */ -class BinaryBundleReaderTest extends IntlTestCase +class BinaryBundleReaderTest extends \PHPUnit_Framework_TestCase { /** * @var BinaryBundleReader @@ -26,7 +26,7 @@ class BinaryBundleReaderTest extends IntlTestCase protected function setUp() { - $this->skipIfIntlExtensionNotLoaded(); + IntlTestHelper::requireFullIntl($this); $this->reader = new BinaryBundleReader(); } diff --git a/src/Symfony/Component/Intl/Util/IntlTestHelper.php b/src/Symfony/Component/Intl/Util/IntlTestHelper.php index a86f756ef9..47aaa72a1a 100644 --- a/src/Symfony/Component/Intl/Util/IntlTestHelper.php +++ b/src/Symfony/Component/Intl/Util/IntlTestHelper.php @@ -16,29 +16,102 @@ use Symfony\Component\Intl\Intl; /** * Helper class for preparing test cases that rely on the Intl component. * + * Any test that tests functionality relying on either the intl classes or + * the resource bundle data should call either of the methods + * {@link requireIntl()} or {@link requireFullIntl()}. Calling + * {@link requireFullIntl()} is only necessary if you use functionality in the + * test that is not provided by the stub intl implementation. + * * @author Bernhard Schussek */ class IntlTestHelper { /** - * Prepares the given test case to use the Intl component. + * Should be called before tests that work fine with the stub implementation. * * @param \PhpUnit_Framework_TestCase $testCase */ - public static function setUp(\PhpUnit_Framework_TestCase $testCase) + public static function requireIntl(\PhpUnit_Framework_TestCase $testCase) { - if (!Intl::isExtensionLoaded()) { - $testCase->markTestSkipped('The intl extension is not available.'); - } + // We only run tests if the version is *one specific version*. + // This condition is satisfied if + // + // * the intl extension is loaded with version Intl::getIcuStubVersion() + // * the intl extension is not loaded if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) { $testCase->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion()); } - Intl::setDataSource(Intl::STUB); - + // Normalize the default locale in case this is not done explicitly + // in the test \Locale::setDefault('en'); + + // Consequently, tests will + // + // * run only for one ICU version (see Intl::getIcuStubVersion()) + // there is no need to add control structures to your tests that + // change the test depending on the ICU version. + // + // Tests should only rely on functionality that is implemented in the + // stub classes. } + + /** + * Should be called before tests that require a feature-complete intl + * implementation. + * + * @param \PhpUnit_Framework_TestCase $testCase + */ + public static function requireFullIntl(\PhpUnit_Framework_TestCase $testCase) + { + // We only run tests if the intl extension is loaded... + if (!Intl::isExtensionLoaded()) { + $testCase->markTestSkipped('The intl extension is not available.'); + } + + // ... and only if the version is *one specific version*. + if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) { + $testCase->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion()); + } + + // Normalize the default locale in case this is not done explicitly + // in the test + \Locale::setDefault('en'); + + // Consequently, tests will + // + // * run only for one ICU version (see Intl::getIcuStubVersion()) + // there is no need to add control structures to your tests that + // change the test depending on the ICU version. + // * always use the C intl classes + // * always use the binary resource bundles (any locale is allowed) + } + + /** + * Skips the test unless the current system has a 32bit architecture. + * + * @param \PhpUnit_Framework_TestCase $testCase + */ + public static function require32Bit(\PhpUnit_Framework_TestCase $testCase) + { + if (4 !== PHP_INT_SIZE) { + $testCase->markTestSkipped('PHP must be compiled in 32 bit mode to run this test'); + } + } + + /** + * Skips the test unless the current system has a 64bit architecture. + * + * @param \PhpUnit_Framework_TestCase $testCase + */ + public static function require64Bit(\PhpUnit_Framework_TestCase $testCase) + { + if (8 !== PHP_INT_SIZE) { + $testCase->markTestSkipped('PHP must be compiled in 64 bit mode to run this test'); + } + } + /** * Must not be instantiated. */