Commit 65f1d493 authored by alexpott's avatar alexpott

Issue #2294503 by Mile23, neclimdul: Component Utilities unit test cleanups

parent f6e69bc8
......@@ -11,8 +11,11 @@
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Utility\Bytes
* Tests bytes size parsing helper methods.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Bytes
*/
class BytesTest extends UnitTestCase {
......@@ -43,6 +46,7 @@ public function testToInt($size, $expected_int) {
*/
public function providerTestToInt() {
return array(
array('1', 1),
array('1 byte', 1),
array('1 KB' , Bytes::KILOBYTE),
array('1 MB' , pow(Bytes::KILOBYTE, 2)),
......
......@@ -11,13 +11,18 @@
use Drupal\Component\Utility\Crypt;
/**
* @coversDefaultClass \Drupal\Component\Utility\Crypt
* Tests random byte generation.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Crypt
*/
class CryptTest extends UnitTestCase {
/**
* Tests \Drupal\Component\Utility\Crypt::randomBytes().
* Tests random byte generation.
*
* @covers ::randomBytes
*/
public function testRandomBytes() {
for ($i = 1; $i < 10; $i++) {
......@@ -30,14 +35,15 @@ public function testRandomBytes() {
}
/**
* Tests \Drupal\Component\Utility\Crypt::hashBase64().
* Tests hash generation.
*
* @dataProvider providerTestHashBase64
* @covers ::hashBase64
*
* @param string $data
* Data to hash.
* @param string $expected_hash
* Expected result from hashing $data.
*
* @dataProvider providerTestHashBase64
*/
public function testHashBase64($data, $expected_hash) {
$hash = Crypt::hashBase64($data);
......@@ -45,7 +51,10 @@ public function testHashBase64($data, $expected_hash) {
}
/**
* Tests \Drupal\Component\Utility\Crypt::hmacBase64().
* Tests HMAC generation.
*
* @dataProvider providerTestHmacBase64
* @covers ::hmacBase64
*
* @param string $data
* Data to hash.
......@@ -53,8 +62,6 @@ public function testHashBase64($data, $expected_hash) {
* Key to use in hashing process.
* @param string $expected_hmac
* Expected result from hashing $data using $key.
*
* @dataProvider providerTestHmacBase64
*/
public function testHmacBase64($data, $key, $expected_hmac) {
$hmac = Crypt::hmacBase64($data, $key);
......@@ -64,13 +71,14 @@ public function testHmacBase64($data, $key, $expected_hmac) {
/**
* Tests the hmacBase64 method with invalid parameters.
*
* @dataProvider providerTestHmacBase64Invalid
* @expectedException InvalidArgumentException
* @covers ::hmacBase64
*
* @param string $data
* Data to hash.
* @param string $key
* Key to use in hashing process.
*
* @dataProvider providerTestHmacBase64Invalid
* @expectedException InvalidArgumentException
*/
public function testHmacBase64Invalid($data, $key) {
Crypt::hmacBase64($data, $key);
......
......@@ -11,14 +11,20 @@
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Utility\Environment
* Test PHP Environment helper methods.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Environment
*/
class EnvironmentTest extends UnitTestCase {
/**
* Tests \Drupal\Component\Utility\Environment::checkMemoryLimit().
*
* @dataProvider providerTestCheckMemoryLimit
* @covers ::checkMemoryLimit
*
* @param string $required
* The required memory argument for
* \Drupal\Component\Utility\Environment::checkMemoryLimit().
......@@ -28,9 +34,6 @@ class EnvironmentTest extends UnitTestCase {
* @param bool $expected
* The expected return value from
* \Drupal\Component\Utility\Environment::checkMemoryLimit().
*
* @dataProvider providerTestCheckMemoryLimit
* @covers ::checkMemoryLimit
*/
public function testCheckMemoryLimit($required, $custom_memory_limit, $expected) {
$actual = Environment::checkMemoryLimit($required, $custom_memory_limit);
......
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Utility\NumberAlphadecimalTest.
*/
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\Number;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Utility\Number
* @group Utility
*/
class NumberAlphadecimalTest extends UnitTestCase {
/**
* Tests the alphadecimal conversion functions.
*
* @param int $value
* The integer value.
* @param string $expected
* The expected alphadecimal value.
*
* @dataProvider providerTestConversions
*/
public function testConversions($value, $expected) {
$this->assertSame(Number::intToAlphadecimal($value), $expected);
$this->assertSame($value, Number::alphadecimalToInt($expected));
}
/**
* Data provider for testConversions().
*
* @see testConversions()
*
* @return array
* An array containing:
* - The integer value.
* - The alphadecimal value.
*/
public function providerTestConversions() {
return array(
array(0, '00'),
array(1, '01'),
array(10, '0a'),
array(20, '0k'),
array(35, '0z'),
array(36, '110'),
array(100, '12s'),
);
}
}
......@@ -13,23 +13,26 @@
use Drupal\Tests\UnitTestCase;
/**
* Tests number step validation by Number::validStep().
* Tests number manipulation utilities.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Number
*/
class NumberTest extends UnitTestCase {
/**
* Tests Number::validStep() without offset.
*
* @dataProvider providerTestValidStep
* @covers ::validStep
*
* @param numeric $value
* The value argument for Number::validStep().
* @param numeric $step
* The step argument for Number::validStep().
* @param boolean $expected
* Expected return value from Number::validStep().
*
* @dataProvider providerTestValidStep
*/
public function testValidStep($value, $step, $expected) {
$return = Number::validStep($value, $step);
......@@ -39,6 +42,9 @@ public function testValidStep($value, $step, $expected) {
/**
* Tests Number::validStep() with offset.
*
* @dataProvider providerTestValidStepOffset
* @covers ::validStep
*
* @param numeric $value
* The value argument for Number::validStep().
* @param numeric $step
......@@ -47,8 +53,6 @@ public function testValidStep($value, $step, $expected) {
* The offset argument for Number::validStep().
* @param boolean $expected
* Expected return value from Number::validStep().
*
* @dataProvider providerTestValidStepOffset
*/
public function testValidStepOffset($value, $step, $offset, $expected) {
$return = Number::validStep($value, $step, $offset);
......@@ -117,4 +121,43 @@ public static function providerTestValidStepOffset() {
);
}
/**
* Tests the alphadecimal conversion functions.
*
* @dataProvider providerTestConversions
* @covers ::intToAlphadecimal
* @covers ::alphadecimalToInt
*
* @param int $value
* The integer value.
* @param string $expected
* The expected alphadecimal value.
*/
public function testConversions($value, $expected) {
$this->assertSame(Number::intToAlphadecimal($value), $expected);
$this->assertSame($value, Number::alphadecimalToInt($expected));
}
/**
* Data provider for testConversions().
*
* @see testConversions()
*
* @return array
* An array containing:
* - The integer value.
* - The alphadecimal value.
*/
public function providerTestConversions() {
return array(
array(0, '00'),
array(1, '01'),
array(10, '0a'),
array(20, '0k'),
array(35, '0z'),
array(36, '110'),
array(100, '12s'),
);
}
}
......@@ -12,8 +12,11 @@
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Utility\Random
* Tests random data generation.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Random
*/
class RandomTest extends UnitTestCase {
......@@ -29,7 +32,7 @@ class RandomTest extends UnitTestCase {
/**
* Tests unique random string generation.
*
* @see \Drupal\Component\Utility\Random::string()
* @covers ::string
*/
public function testRandomStringUniqueness() {
$strings = array();
......@@ -44,7 +47,7 @@ public function testRandomStringUniqueness() {
/**
* Tests unique random name generation.
*
* @see \Drupal\Component\Utility\Random::name()
* @covers ::name
*/
public function testRandomNamesUniqueness() {
$names = array();
......@@ -59,9 +62,8 @@ public function testRandomNamesUniqueness() {
/**
* Tests infinite loop prevention whilst generating random names.
*
* @see \Drupal\Component\Utility\Random::name()
*
* @expectedException RuntimeException
* @covers ::name
* @expectedException \RuntimeException
*/
public function testRandomNameException() {
// There are fewer than 100 possibilities so an exception should occur to
......@@ -76,9 +78,8 @@ public function testRandomNameException() {
/**
* Tests infinite loop prevention whilst generating random strings.
*
* @see \Drupal\Component\Utility\Random::string()
*
* @expectedException RuntimeException
* @covers ::string
* @expectedException \RuntimeException
*/
public function testRandomStringException() {
// There are fewer than 100 possibilities so an exception should occur to
......@@ -93,7 +94,7 @@ public function testRandomStringException() {
/**
* Tests random name generation if uniqueness is not enforced.
*
* @see \Drupal\Component\Utility\Random::name()
* @covers ::name
*/
public function testRandomNameNonUnique() {
// There are fewer than 100 possibilities if we were forcing uniqueness so
......@@ -108,7 +109,7 @@ public function testRandomNameNonUnique() {
/**
* Tests random string if uniqueness is not enforced.
*
* @see \Drupal\Component\Utility\Random::string()
* @covers ::string
*/
public function testRandomStringNonUnique() {
// There are fewer than 100 possibilities if we were forcing uniqueness so
......@@ -123,7 +124,7 @@ public function testRandomStringNonUnique() {
/**
* Tests random object generation to ensure the expected number of properties.
*
* @see \Drupal\Component\Utility\Random::object()
* @covers ::object
*/
public function testRandomObject() {
// For values of 0 and 1 \Drupal\Component\Utility\Random::object() will
......@@ -138,7 +139,7 @@ public function testRandomObject() {
/**
* Tests random string validation callbacks.
*
* @see \Drupal\Component\Utility\Random::name()
* @covers ::string
*/
public function testRandomStringValidator() {
$random = new Random();
......
......@@ -11,8 +11,11 @@
use Drupal\Component\Utility\SortArray;
/**
* @coversDefaultClass \Drupal\Component\Utility\SortArray
* Tests the SortArray component.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\SortArray
*/
class SortArrayTest extends UnitTestCase {
......@@ -20,6 +23,8 @@ class SortArrayTest extends UnitTestCase {
* Tests SortArray::sortByWeightElement() input against expected output.
*
* @dataProvider providerSortByWeightElement
* @covers ::sortByWeightElement
* @covers ::sortByKeyInt
*
* @param array $a
* The first input array for the SortArray::sortByWeightElement() method.
......@@ -27,9 +32,6 @@ class SortArrayTest extends UnitTestCase {
* The second input array for the SortArray::sortByWeightElement().
* @param integer $expected
* The expected output from calling the method.
*
* @see \Drupal\Component\Utility\SortArray::sortByWeightElement()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::providersortByWeightElement()
*/
public function testSortByWeightElement($a, $b, $expected) {
$result = SortArray::sortByWeightElement($a, $b);
......@@ -43,7 +45,6 @@ public function testSortByWeightElement($a, $b, $expected) {
* An array of tests, matching the parameter inputs for
* testSortByWeightElement.
*
* @see \Drupal\Component\Utility\SortArray::sortByWeightElement()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightElement()
*/
public function providerSortByWeightElement() {
......@@ -98,6 +99,8 @@ public function providerSortByWeightElement() {
* Tests SortArray::sortByWeightProperty() input against expected output.
*
* @dataProvider providerSortByWeightProperty
* @covers ::sortByWeightProperty
* @covers ::sortByKeyInt
*
* @param array $a
* The first input array for the SortArray::sortByWeightProperty() method.
......@@ -105,9 +108,6 @@ public function providerSortByWeightElement() {
* The second input array for the SortArray::sortByWeightProperty().
* @param integer $expected
* The expected output from calling the method.
*
* @see \Drupal\Component\Utility\SortArray::sortByWeightProperty()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::SortByWeightProperty()
*/
public function testSortByWeightProperty($a, $b, $expected) {
$result = SortArray::sortByWeightProperty($a, $b);
......@@ -121,7 +121,6 @@ public function testSortByWeightProperty($a, $b, $expected) {
* An array of tests, matching the parameter inputs for
* testSortByWeightProperty.
*
* @see \Drupal\Component\Utility\SortArray::sortByWeightProperty()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightProperty()
*/
public function providerSortByWeightProperty() {
......@@ -176,6 +175,8 @@ public function providerSortByWeightProperty() {
* Tests SortArray::sortByTitleElement() input against expected output.
*
* @dataProvider providerSortByTitleElement
* @covers ::sortByTitleElement
* @covers ::sortByKeyString
*
* @param array $a
* The first input item for comparison.
......@@ -183,9 +184,6 @@ public function providerSortByWeightProperty() {
* The second item for comparison.
* @param integer $expected
* The expected output from calling the method.
*
* @see \Drupal\Component\Utility\SortArray::sortByTitleElement()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::providerSortByTitleElement()
*/
public function testSortByTitleElement($a, $b, $expected) {
$result = SortArray::sortByTitleElement($a, $b);
......@@ -199,7 +197,6 @@ public function testSortByTitleElement($a, $b, $expected) {
* An array of tests, matching the parameter inputs for
* testSortByTitleElement.
*
* @see \Drupal\Component\Utility\SortArray::sortByTitleElement()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement()
*/
public function providerSortByTitleElement() {
......@@ -247,6 +244,8 @@ public function providerSortByTitleElement() {
* Tests SortArray::sortByTitleProperty() input against expected output.
*
* @dataProvider providerSortByTitleProperty
* @covers ::sortByTitleProperty
* @covers ::sortByKeyString
*
* @param array $a
* The first input item for comparison.
......@@ -254,9 +253,6 @@ public function providerSortByTitleElement() {
* The second item for comparison.
* @param integer $expected
* The expected output from calling the method.
*
* @see \Drupal\Component\Utility\SortArray::sortByTitleProperty()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::SortByTitleProperty()
*/
public function testSortByTitleProperty($a, $b, $expected) {
$result = SortArray::sortByTitleProperty($a, $b);
......@@ -270,7 +266,6 @@ public function testSortByTitleProperty($a, $b, $expected) {
* An array of tests, matching the parameter inputs for
* testSortByTitleProperty.
*
* @see \Drupal\Component\Utility\SortArray::sortByTitleProperty()
* @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty()
*/
public function providerSortByTitleProperty() {
......
......@@ -11,8 +11,11 @@
use Drupal\Component\Utility\String;
/**
* @coversDefaultClass \Drupal\Component\Utility\String
* Tests string filtering.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\String
*/
class StringTest extends UnitTestCase {
......@@ -20,6 +23,7 @@ class StringTest extends UnitTestCase {
* Tests String::checkPlain().
*
* @dataProvider providerCheckPlain
* @covers ::checkPlain
*
* @param string $text
* The text to provide to String::checkPlain().
......@@ -57,6 +61,7 @@ function providerCheckPlain() {
* Tests string formatting with String::format().
*
* @dataProvider providerFormat
* @covers ::format
*
* @param string $string
* The string to run through String::format().
......@@ -66,8 +71,6 @@ function providerCheckPlain() {
* The expected result from calling the function.
* @param string $message
* The message to display as output to the test.
*
* @see String::format()
*/
function testFormat($string, $args, $expected, $message) {
$result = String::format($string, $args);
......@@ -91,7 +94,7 @@ function providerFormat() {
/**
* Tests String::placeholder().
*
* @see String::placeholder()
* @covers ::placeholder
*/
function testPlaceholder() {
$this->assertEquals('<em class="placeholder">Some text</em>', String::placeholder('Some text'));
......@@ -101,6 +104,7 @@ function testPlaceholder() {
* Tests String::decodeEntities().
*
* @dataProvider providerDecodeEntities
* @covers ::decodeEntities
*/
public function testDecodeEntities($text, $expected) {
$this->assertEquals($expected, String::decodeEntities($text));
......
......@@ -11,15 +11,20 @@
use Drupal\Component\Utility\Timer;
/**
* @coversDefaultClass \Drupal\Component\Utility\Timer
* Tests the Timer system.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Timer
*/
class TimerTest extends UnitTestCase {
/**
* Tests Timer::read() time accumulation accuracy across multiple restarts.
*
* @see \Drupal\Component\Utility\Timer::read()
* @covers ::start
* @covers ::stop
* @covers ::read
*/
public function testTimer() {
Timer::start('test');
......
......@@ -11,20 +11,30 @@
use Drupal\Component\Utility\Unicode;
/**
* @coversDefaultClass \Drupal\Component\Utility\Unicode
* Test unicode handling features implemented in Unicode component.
*
* @group Utility
*
* @coversDefaultClass \Drupal\Component\Utility\Unicode
*/
class UnicodeTest extends UnitTestCase {
protected function setUp() {
/**
* {@inheritdoc}
*
* @covers ::check
*/
public function setUp() {
// Initialize unicode component.
Unicode::check();
}
/**
* Tests Unicode::getStatus() and Unicode::setStatus().
* Getting and settings the multibyte environment status.
*
* @dataProvider providerTestStatus
* @covers ::getStatus
* @covers ::setStatus
*/
public function testStatus($value, $expected, $invalid = FALSE) {
if ($invalid) {
......@@ -58,9 +68,11 @@ public function providerTestStatus() {
}
/**
* Tests Unicode::mimeHeaderEncode() and Unicode::mimeHeaderDecode().
* Tests multibyte encoding and decoding.
*
* @dataProvider providerTestMimeHeader
* @covers ::mimeHeaderEncode
* @covers ::mimeHeaderDecode
*/
public function testMimeHeader($value, $encoded) {
$this->assertEquals($encoded, Unicode::mimeHeaderEncode($value));
......@@ -84,9 +96,11 @@ public function providerTestMimeHeader() {
}
/**
* Tests Unicode::strtolower().
* Tests multibyte strtolower.
*
* @dataProvider providerStrtolower
* @covers ::strtolower
* @covers ::caseFlip
*/
public function testStrtolower($text, $expected, $multibyte = FALSE) {
$status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
......@@ -119,9 +133,11 @@ public function providerStrtolower() {
}
/**
* Tests Unicode::strtoupper().
* Tests multibyte strtoupper.
*
* @dataProvider providerStrtoupper
* @covers ::strtoupper
* @covers ::caseFlip
*/
public function testStrtoupper($text, $expected, $multibyte = FALSE) {
$status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
......@@ -154,9 +170,10 @@ public function providerStrtoupper() {
}
/**
* Tests Unicode::ucfirst().
* Tests multibyte ucfirst.
*
* @dataProvider providerUcfirst
* @covers ::ucfirst
*/
public function testUcfirst($text, $expected) {
$this->assertEquals($expected, Unicode::ucfirst($text));
......@@ -182,9 +199,10 @@ public function providerUcfirst() {
}
/**
* Tests Unicode::lcfirst().
* Tests multibyte lcfirst.
*
* @dataProvider providerLcfirst
* @covers ::lcfirst
*/
public function testLcfirst($text, $expected, $multibyte = FALSE) {
$status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
......@@ -213,9 +231,10 @@ public function providerLcfirst() {
}
/**
* Tests Unicode::ucwords().
* Tests multibyte ucwords.
*
* @dataProvider providerUcwords
* @covers ::ucwords
*/
public function testUcwords($text, $expected, $multibyte = FALSE) {
$status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
......@@ -246,9 +265,10 @@ public function providerUcwords() {
}
/**
* Tests Unicode::strlen().
* Tests multibyte strlen.
*
* @dataProvider providerStrlen
* @covers ::strlen
*/
public function testStrlen($text, $expected) {
// Run through multibyte code path.
......@@ -275,9 +295,10 @@ public function providerStrlen() {
}
/**
* Tests Unicode::substr().
* Tests multibyte substr.
*
* @dataProvider providerSubstr
* @covers ::substr
*/
public function testSubstr($text, $start, $length, $expected) {
// Run through multibyte code path.
......@@ -302,6 +323,7 @@ public function testSubstr($text, $start, $length, $expected) {
*/
public function providerSubstr() {
return array(
array('frànçAIS is über-åwesome', 0, NULL, 'frànçAIS is über-åwesome'),
array('frànçAIS is über-åwesome', 0, 0, ''),
array('frànçAIS is über-åwesome', 0, 1, 'f'),
array('frànçAIS is über-åwesome', 0, 8, 'frànçAIS'),
......@@ -330,12 +352,12 @@ public function providerSubstr() {
}
/**
* Tests Unicode::truncate().
* Tests multibyte truncate.
*
* @dataProvider providerTruncate
* @covers ::truncate
*/
public function testTruncate($text, $max_length, $expected, $wordsafe = FALSE, $add_ellipsis = FALSE) {
Unicode::check();
$this->assertEquals($expected, Unicode::truncate($text, $max_length, $wordsafe, $add_ellipsis));
}
......@@ -402,7 +424,10 @@ public function providerTruncate() {
}
/**
* Tests Unicode::truncateBytes().
* Tests multibyte truncate bytes.
*
* @dataProvider providerTestTruncateBytes
* @covers ::truncateBytes
*
* @param string $text
* The string to truncate.
......@@ -410,8 +435,6 @@ public function providerTruncate() {
* The upper limit on the returned string length.
* @param string $expected
* The expected return from Unicode::truncateBytes().
*
* @dataProvider providerTestTruncateBytes
*/
public function testTruncateBytes($text, $max_length, $expected) {
$this->assertEquals($expected, Unicode::truncateBytes($text, $max_length), 'The string was not correctly truncated.');
......@@ -436,7 +459,10 @@ public function providerTestTruncateBytes() {
}
/**
* Tests Unicode::validateUtf8().
* Tests UTF-8 validation.
*
* @dataProvider providerTestValidateUtf8
* @covers ::validateUtf8
*
* @param string $text
* The text to validate.
......@@ -444,8 +470,6 @@ public function providerTestTruncateBytes() {
* The expected return value from Unicode::validateUtf8().
* @param string $message