3 namespace Drupal\Tests\simpletest\Unit;
5 use Drupal\Tests\UnitTestCase;
8 * @requires extension curl
9 * @coversDefaultClass \Drupal\simpletest\TestBase
12 class TestBaseTest extends UnitTestCase {
15 * Helper method for constructing a mock TestBase object.
17 * TestBase is abstract, so we have to mock it. We'll also
18 * mock the storeAssertion() method so we don't need the database.
20 * @param string $test_id
21 * An identifying name for the mocked test.
24 * Mock of Drupal\simpletest\TestBase.
26 public function getTestBaseForAssertionTests($test_id) {
27 $mock_test_base = $this->getMockBuilder('Drupal\simpletest\TestBase')
28 ->setConstructorArgs([$test_id])
29 ->setMethods(['storeAssertion'])
30 ->getMockForAbstractClass();
31 // Override storeAssertion() so we don't need a database.
32 $mock_test_base->expects($this->any())
33 ->method('storeAssertion')
34 ->will($this->returnValue(NULL));
35 return $mock_test_base;
39 * Invoke methods that are protected or private.
41 * @param object $object
42 * Object on which to invoke the method.
43 * @param string $method_name
44 * Name of the method to invoke.
45 * @param array $arguments
46 * Array of arguments to be passed to the method.
49 * Value returned by the invoked method.
51 public function invokeProtectedMethod($object, $method_name, array $arguments) {
52 $ref_method = new \ReflectionMethod($object, $method_name);
53 $ref_method->setAccessible(TRUE);
54 return $ref_method->invokeArgs($object, $arguments);
58 * Provides data for the random string validation test.
61 * - The expected result of the validation.
62 * - The string to validate.
64 public function providerRandomStringValidate() {
66 [FALSE, ' curry paste'],
67 [FALSE, 'curry paste '],
68 [FALSE, 'curry paste'],
69 [FALSE, 'curry paste'],
70 [TRUE, 'curry paste'],
71 [TRUE, 'thai green curry paste'],
72 [TRUE, '@startswithat'],
73 [TRUE, 'contains@at'],
78 * @covers ::randomStringValidate
79 * @dataProvider providerRandomStringValidate
81 public function testRandomStringValidate($expected, $string) {
82 $mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase');
83 $actual = $mock_test_base->randomStringValidate($string);
84 $this->assertEquals($expected, $actual);
88 * Provides data for testRandomString() and others.
91 * - The number of items (characters, object properties) we expect any of
92 * the random functions to give us.
94 public function providerRandomItems() {
107 * @covers ::randomString
108 * @dataProvider providerRandomItems
110 public function testRandomString($length) {
111 $mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase');
112 $string = $mock_test_base->randomString($length);
113 $this->assertEquals($length, strlen($string));
114 // randomString() should always include an ampersand ('&') and a
115 // greater than ('>') if $length is greater than 3.
117 $this->assertContains('&', $string);
118 $this->assertContains('>', $string);
123 * @covers ::randomObject
124 * @dataProvider providerRandomItems
126 public function testRandomObject($size) {
127 $test_base = $this->getTestBaseForAssertionTests('test_id');
128 // Note: count((array)object) works for now, maybe not later.
129 $this->assertEquals($size, count((array) $test_base->randomObject($size)));
133 * @covers ::checkRequirements
135 public function testCheckRequirements() {
136 $test_base = $this->getTestBaseForAssertionTests('test_id');
137 $this->assertInternalType(
139 $this->invokeProtectedMethod($test_base, 'checkRequirements', [])
144 * Data provider for testAssert().
147 * Standard dataProvider array of arrays:
148 * - Expected result from assert().
149 * - Expected status stored in TestBase->assertions.
150 * - Status, passed to assert().
151 * - Message, passed to assert().
152 * - Group, passed to assert().
153 * - Caller, passed to assert().
155 public function providerAssert() {
157 [TRUE, 'pass', TRUE, 'Yay pass', 'test', []],
158 [FALSE, 'fail', FALSE, 'Boo fail', 'test', []],
159 [TRUE, 'pass', 'pass', 'Yay pass', 'test', []],
160 [FALSE, 'fail', 'fail', 'Boo fail', 'test', []],
161 [FALSE, 'exception', 'exception', 'Boo fail', 'test', []],
162 [FALSE, 'debug', 'debug', 'Boo fail', 'test', []],
168 * @dataProvider providerAssert
170 public function testAssert($expected, $assertion_status, $status, $message, $group, $caller) {
171 $test_id = 'luke_i_am_your_' . $assertion_status;
172 $test_base = $this->getTestBaseForAssertionTests($test_id);
174 // Verify some startup values.
175 $this->assertAttributeEmpty('assertions', $test_base);
176 if (is_string($status)) {
177 $this->assertEquals(0, $test_base->results['#' . $status]);
180 // assert() is protected so we have to make it accessible.
181 $ref_assert = new \ReflectionMethod($test_base, 'assert');
182 $ref_assert->setAccessible(TRUE);
184 // Call assert() from within our hall of mirrors.
187 $ref_assert->invokeArgs($test_base,
188 [$status, $message, $group, $caller]
192 // Check the side-effects of assert().
193 if (is_string($status)) {
194 $this->assertEquals(1, $test_base->results['#' . $status]);
196 $this->assertAttributeNotEmpty('assertions', $test_base);
197 // Make a ReflectionProperty for the assertions property,
198 // since it's protected.
199 $ref_assertions = new \ReflectionProperty($test_base, 'assertions');
200 $ref_assertions->setAccessible(TRUE);
201 $assertions = $ref_assertions->getValue($test_base);
202 $assertion = reset($assertions);
203 $this->assertEquals($assertion_status, $assertion['status']);
204 $this->assertEquals($test_id, $assertion['test_id']);
205 $this->assertEquals(get_class($test_base), $assertion['test_class']);
206 $this->assertEquals($message, $assertion['message']);
207 $this->assertEquals($group, $assertion['message_group']);
211 * Data provider for assertTrue().
213 public function providerAssertTrue() {
221 * @covers ::assertTrue
222 * @dataProvider providerAssertTrue
224 public function testAssertTrue($expected, $value) {
225 $test_base = $this->getTestBaseForAssertionTests('test_id');
228 $this->invokeProtectedMethod($test_base, 'assertTrue', [$value])
233 * @covers ::assertFalse
234 * @dataProvider providerAssertTrue
236 public function testAssertFalse($expected, $value) {
237 $test_base = $this->getTestBaseForAssertionTests('test_id');
240 $this->invokeProtectedMethod($test_base, 'assertFalse', [$value])
245 * Data provider for assertNull().
247 public function providerAssertNull() {
255 * @covers ::assertNull
256 * @dataProvider providerAssertNull
258 public function testAssertNull($expected, $value) {
259 $test_base = $this->getTestBaseForAssertionTests('test_id');
262 $this->invokeProtectedMethod($test_base, 'assertNull', [$value])
267 * @covers ::assertNotNull
268 * @dataProvider providerAssertNull
270 public function testAssertNotNull($expected, $value) {
271 $test_base = $this->getTestBaseForAssertionTests('test_id');
274 $this->invokeProtectedMethod($test_base, 'assertNotNull', [$value])
279 * Data provider for tests of equality assertions.
281 * Used by testAssertIdentical(), testAssertEqual(), testAssertNotIdentical(),
282 * and testAssertNotEqual().
285 * Array of test data.
286 * - Expected assertion value for identical comparison.
287 * - Expected assertion value for equal comparison.
288 * - First value to compare.
289 * - Second value to compare.
291 public function providerEqualityAssertions() {
293 // Integers and floats.
295 [FALSE, TRUE, 0, 0.0],
296 [FALSE, TRUE, '0', 0],
297 [FALSE, TRUE, '0.0', 0.0],
298 [FALSE, FALSE, 23, 77],
299 [TRUE, TRUE, 23.0, 23.0],
301 [FALSE, FALSE, 'foof', 'yay'],
302 [TRUE, TRUE, 'yay', 'yay'],
303 // Bools with type conversion.
304 [TRUE, TRUE, TRUE, TRUE],
305 [TRUE, TRUE, FALSE, FALSE],
306 [FALSE, TRUE, NULL, FALSE],
307 [FALSE, TRUE, 'TRUE', TRUE],
308 [FALSE, FALSE, 'FALSE', FALSE],
309 [FALSE, TRUE, 0, FALSE],
310 [FALSE, TRUE, 1, TRUE],
311 [FALSE, TRUE, -1, TRUE],
312 [FALSE, TRUE, '1', TRUE],
313 [FALSE, TRUE, '1.3', TRUE],
315 [FALSE, FALSE, 'NULL', NULL],
316 [TRUE, TRUE, NULL, NULL],
321 * @covers ::assertIdentical
322 * @dataProvider providerEqualityAssertions
324 public function testAssertIdentical($expected_identical, $expected_equal, $first, $second) {
325 $test_base = $this->getTestBaseForAssertionTests('test_id');
328 $this->invokeProtectedMethod($test_base, 'assertIdentical', [$first, $second])
333 * @covers ::assertNotIdentical
334 * @dataProvider providerEqualityAssertions
336 public function testAssertNotIdentical($expected_identical, $expected_equal, $first, $second) {
337 $test_base = $this->getTestBaseForAssertionTests('test_id');
339 (!$expected_identical),
340 $this->invokeProtectedMethod($test_base, 'assertNotIdentical', [$first, $second])
345 * @covers ::assertEqual
346 * @dataProvider providerEqualityAssertions
348 public function testAssertEqual($expected_identical, $expected_equal, $first, $second) {
349 $test_base = $this->getTestBaseForAssertionTests('test_id');
352 $this->invokeProtectedMethod($test_base, 'assertEqual', [$first, $second])
357 * @covers ::assertNotEqual
358 * @dataProvider providerEqualityAssertions
360 public function testAssertNotEqual($expected_identical, $expected_equal, $first, $second) {
361 $test_base = $this->getTestBaseForAssertionTests('test_id');
364 $this->invokeProtectedMethod($test_base, 'assertNotEqual', [$first, $second])
369 * Data provider for testAssertIdenticalObject().
371 public function providerAssertIdenticalObject() {
372 $obj1 = new \stdClass();
376 $obj4 = new \stdClass();
378 [TRUE, $obj1, $obj2],
379 [TRUE, $obj1, $obj3],
380 [FALSE, $obj1, $obj4],
385 * @covers ::assertIdenticalObject
386 * @dataProvider providerAssertIdenticalObject
388 public function testAssertIdenticalObject($expected, $first, $second) {
389 $test_base = $this->getTestBaseForAssertionTests('test_id');
392 $this->invokeProtectedMethod($test_base, 'assertIdenticalObject', [$first, $second])
399 public function testPass() {
400 $test_base = $this->getTestBaseForAssertionTests('test_id');
403 $this->invokeProtectedMethod($test_base, 'pass', [])
410 public function testFail() {
411 $test_base = $this->getTestBaseForAssertionTests('test_id');
414 $this->invokeProtectedMethod($test_base, 'fail', [])
419 * Data provider for testError().
422 * - Expected status for assertion.
423 * - Group for use in assert().
425 public function providerError() {
427 ['debug', 'User notice'],
428 ['exception', 'Not User notice'],
434 * @dataProvider providerError
436 public function testError($status, $group) {
437 // Mock up a TestBase object.
438 $mock_test_base = $this->getMockBuilder('Drupal\simpletest\TestBase')
439 ->setMethods(['assert'])
440 ->getMockForAbstractClass();
442 // Set expectations for assert().
443 $mock_test_base->expects($this->once())
445 // The first argument to assert() should be the expected $status. This is
446 // the most important expectation of this test.
448 // Arbitrary return value.
449 ->willReturn("$status:$group");
454 $this->invokeProtectedMethod($mock_test_base, 'error', ['msg', $group])
459 * @covers ::getRandomGenerator
461 public function testGetRandomGenerator() {
462 $test_base = $this->getTestBaseForAssertionTests('test_id');
463 $this->assertInstanceOf(
464 'Drupal\Component\Utility\Random',
465 $this->invokeProtectedMethod($test_base, 'getRandomGenerator', [])