View source
<?php
namespace Drupal\Tests\simpletest\Unit;
use Drupal\Tests\UnitTestCase;
class TestBaseTest extends UnitTestCase {
public function getTestBaseForAssertionTests($test_id) {
$mock_test_base = $this
->getMockBuilder('Drupal\\simpletest\\TestBase')
->setConstructorArgs(array(
$test_id,
))
->setMethods(array(
'storeAssertion',
))
->getMockForAbstractClass();
$mock_test_base
->expects($this
->any())
->method('storeAssertion')
->will($this
->returnValue(NULL));
return $mock_test_base;
}
public function invokeProtectedMethod($object, $method_name, array $arguments) {
$ref_method = new \ReflectionMethod($object, $method_name);
$ref_method
->setAccessible(TRUE);
return $ref_method
->invokeArgs($object, $arguments);
}
public function providerRandomStringValidate() {
return array(
array(
FALSE,
' curry paste',
),
array(
FALSE,
'curry paste ',
),
array(
FALSE,
'curry paste',
),
array(
FALSE,
'curry paste',
),
array(
TRUE,
'curry paste',
),
array(
TRUE,
'thai green curry paste',
),
array(
TRUE,
'@startswithat',
),
array(
TRUE,
'contains@at',
),
);
}
public function testRandomStringValidate($expected, $string) {
$mock_test_base = $this
->getMockForAbstractClass('Drupal\\simpletest\\TestBase');
$actual = $mock_test_base
->randomStringValidate($string);
$this
->assertEquals($expected, $actual);
}
public function providerRandomItems() {
return [
[
NULL,
],
[
0,
],
[
1,
],
[
2,
],
[
3,
],
[
4,
],
[
7,
],
];
}
public function testRandomString($length) {
$mock_test_base = $this
->getMockForAbstractClass('Drupal\\simpletest\\TestBase');
$string = $mock_test_base
->randomString($length);
$this
->assertEquals($length, strlen($string));
if ($length > 4) {
$this
->assertContains('&', $string);
$this
->assertContains('>', $string);
}
}
public function testRandomObject($size) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals($size, count((array) $test_base
->randomObject($size)));
}
public function testCheckRequirements() {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertInternalType('array', $this
->invokeProtectedMethod($test_base, 'checkRequirements', array()));
}
public function providerAssert() {
return array(
array(
TRUE,
'pass',
TRUE,
'Yay pass',
'test',
array(),
),
array(
FALSE,
'fail',
FALSE,
'Boo fail',
'test',
array(),
),
array(
TRUE,
'pass',
'pass',
'Yay pass',
'test',
array(),
),
array(
FALSE,
'fail',
'fail',
'Boo fail',
'test',
array(),
),
array(
FALSE,
'exception',
'exception',
'Boo fail',
'test',
array(),
),
array(
FALSE,
'debug',
'debug',
'Boo fail',
'test',
array(),
),
);
}
public function testAssert($expected, $assertion_status, $status, $message, $group, $caller) {
$test_id = 'luke_i_am_your_' . $assertion_status;
$test_base = $this
->getTestBaseForAssertionTests($test_id);
$this
->assertAttributeEmpty('assertions', $test_base);
if (is_string($status)) {
$this
->assertEquals(0, $test_base->results['#' . $status]);
}
$ref_assert = new \ReflectionMethod($test_base, 'assert');
$ref_assert
->setAccessible(TRUE);
$this
->assertEquals($expected, $ref_assert
->invokeArgs($test_base, array(
$status,
$message,
$group,
$caller,
)));
if (is_string($status)) {
$this
->assertEquals(1, $test_base->results['#' . $status]);
}
$this
->assertAttributeNotEmpty('assertions', $test_base);
$ref_assertions = new \ReflectionProperty($test_base, 'assertions');
$ref_assertions
->setAccessible(TRUE);
$assertions = $ref_assertions
->getValue($test_base);
$assertion = reset($assertions);
$this
->assertEquals($assertion_status, $assertion['status']);
$this
->assertEquals($test_id, $assertion['test_id']);
$this
->assertEquals(get_class($test_base), $assertion['test_class']);
$this
->assertEquals($message, $assertion['message']);
$this
->assertEquals($group, $assertion['message_group']);
}
public function providerAssertTrue() {
return array(
array(
TRUE,
TRUE,
),
array(
FALSE,
FALSE,
),
);
}
public function testAssertTrue($expected, $value) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals($expected, $this
->invokeProtectedMethod($test_base, 'assertTrue', array(
$value,
)));
}
public function testAssertFalse($expected, $value) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals(!$expected, $this
->invokeProtectedMethod($test_base, 'assertFalse', array(
$value,
)));
}
public function providerAssertNull() {
return array(
array(
TRUE,
NULL,
),
array(
FALSE,
'',
),
);
}
public function testAssertNull($expected, $value) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals($expected, $this
->invokeProtectedMethod($test_base, 'assertNull', array(
$value,
)));
}
public function testAssertNotNull($expected, $value) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals(!$expected, $this
->invokeProtectedMethod($test_base, 'assertNotNull', array(
$value,
)));
}
public function providerEqualityAssertions() {
return [
[
TRUE,
TRUE,
0,
0,
],
[
FALSE,
TRUE,
0,
0.0,
],
[
FALSE,
TRUE,
'0',
0,
],
[
FALSE,
TRUE,
'0.0',
0.0,
],
[
FALSE,
FALSE,
23,
77,
],
[
TRUE,
TRUE,
23.0,
23.0,
],
[
FALSE,
FALSE,
'foof',
'yay',
],
[
TRUE,
TRUE,
'yay',
'yay',
],
[
TRUE,
TRUE,
TRUE,
TRUE,
],
[
TRUE,
TRUE,
FALSE,
FALSE,
],
[
FALSE,
TRUE,
NULL,
FALSE,
],
[
FALSE,
TRUE,
'TRUE',
TRUE,
],
[
FALSE,
FALSE,
'FALSE',
FALSE,
],
[
FALSE,
TRUE,
0,
FALSE,
],
[
FALSE,
TRUE,
1,
TRUE,
],
[
FALSE,
TRUE,
-1,
TRUE,
],
[
FALSE,
TRUE,
'1',
TRUE,
],
[
FALSE,
TRUE,
'1.3',
TRUE,
],
[
FALSE,
FALSE,
'NULL',
NULL,
],
[
TRUE,
TRUE,
NULL,
NULL,
],
];
}
public function testAssertIdentical($expected_identical, $expected_equal, $first, $second) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals($expected_identical, $this
->invokeProtectedMethod($test_base, 'assertIdentical', array(
$first,
$second,
)));
}
public function testAssertNotIdentical($expected_identical, $expected_equal, $first, $second) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals(!$expected_identical, $this
->invokeProtectedMethod($test_base, 'assertNotIdentical', array(
$first,
$second,
)));
}
public function testAssertEqual($expected_identical, $expected_equal, $first, $second) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals($expected_equal, $this
->invokeProtectedMethod($test_base, 'assertEqual', array(
$first,
$second,
)));
}
public function testAssertNotEqual($expected_identical, $expected_equal, $first, $second) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals(!$expected_equal, $this
->invokeProtectedMethod($test_base, 'assertNotEqual', array(
$first,
$second,
)));
}
public function providerAssertIdenticalObject() {
$obj1 = new \stdClass();
$obj1->foof = 'yay';
$obj2 = $obj1;
$obj3 = clone $obj1;
$obj4 = new \stdClass();
return array(
array(
TRUE,
$obj1,
$obj2,
),
array(
TRUE,
$obj1,
$obj3,
),
array(
FALSE,
$obj1,
$obj4,
),
);
}
public function testAssertIdenticalObject($expected, $first, $second) {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals($expected, $this
->invokeProtectedMethod($test_base, 'assertIdenticalObject', array(
$first,
$second,
)));
}
public function testPass() {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals(TRUE, $this
->invokeProtectedMethod($test_base, 'pass', array()));
}
public function testFail() {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertEquals(FALSE, $this
->invokeProtectedMethod($test_base, 'fail', array()));
}
public function providerError() {
return array(
array(
'debug',
'User notice',
),
array(
'exception',
'Not User notice',
),
);
}
public function testError($status, $group) {
$mock_test_base = $this
->getMockBuilder('Drupal\\simpletest\\TestBase')
->setMethods(array(
'assert',
))
->getMockForAbstractClass();
$mock_test_base
->expects($this
->once())
->method('assert')
->with($status)
->willReturn("{$status}:{$group}");
$this
->assertEquals("{$status}:{$group}", $this
->invokeProtectedMethod($mock_test_base, 'error', array(
'msg',
$group,
)));
}
public function testGetRandomGenerator() {
$test_base = $this
->getTestBaseForAssertionTests('test_id');
$this
->assertInstanceOf('Drupal\\Component\\Utility\\Random', $this
->invokeProtectedMethod($test_base, 'getRandomGenerator', array()));
}
}