PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/simpletest/tests/src/Unit/TestBaseTest.php

http://github.com/drupal/drupal
PHP | 470 lines | 258 code | 38 blank | 174 comment | 3 complexity | 0259d5dc585b80f7a7d046eaa2119635 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Tests\simpletest\Unit;
  3. use Drupal\Tests\UnitTestCase;
  4. /**
  5. * @requires extension curl
  6. * @coversDefaultClass \Drupal\simpletest\TestBase
  7. * @group simpletest
  8. * @group TestBase
  9. */
  10. class TestBaseTest extends UnitTestCase {
  11. /**
  12. * Helper method for constructing a mock TestBase object.
  13. *
  14. * TestBase is abstract, so we have to mock it. We'll also
  15. * mock the storeAssertion() method so we don't need the database.
  16. *
  17. * @param string $test_id
  18. * An identifying name for the mocked test.
  19. *
  20. * @return object
  21. * Mock of Drupal\simpletest\TestBase.
  22. */
  23. public function getTestBaseForAssertionTests($test_id) {
  24. $mock_test_base = $this->getMockBuilder('Drupal\simpletest\TestBase')
  25. ->setConstructorArgs([$test_id])
  26. ->setMethods(['storeAssertion'])
  27. ->getMockForAbstractClass();
  28. // Override storeAssertion() so we don't need a database.
  29. $mock_test_base->expects($this->any())
  30. ->method('storeAssertion')
  31. ->will($this->returnValue(NULL));
  32. return $mock_test_base;
  33. }
  34. /**
  35. * Invoke methods that are protected or private.
  36. *
  37. * @param object $object
  38. * Object on which to invoke the method.
  39. * @param string $method_name
  40. * Name of the method to invoke.
  41. * @param array $arguments
  42. * Array of arguments to be passed to the method.
  43. *
  44. * @return mixed
  45. * Value returned by the invoked method.
  46. */
  47. public function invokeProtectedMethod($object, $method_name, array $arguments) {
  48. $ref_method = new \ReflectionMethod($object, $method_name);
  49. $ref_method->setAccessible(TRUE);
  50. return $ref_method->invokeArgs($object, $arguments);
  51. }
  52. /**
  53. * Provides data for the random string validation test.
  54. *
  55. * @return array
  56. * - The expected result of the validation.
  57. * - The string to validate.
  58. */
  59. public function providerRandomStringValidate() {
  60. return [
  61. [FALSE, ' curry paste'],
  62. [FALSE, 'curry paste '],
  63. [FALSE, 'curry paste'],
  64. [FALSE, 'curry paste'],
  65. [TRUE, 'curry paste'],
  66. [TRUE, 'thai green curry paste'],
  67. [TRUE, '@startswithat'],
  68. [TRUE, 'contains@at'],
  69. ];
  70. }
  71. /**
  72. * @covers ::randomStringValidate
  73. * @dataProvider providerRandomStringValidate
  74. */
  75. public function testRandomStringValidate($expected, $string) {
  76. $mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase');
  77. $actual = $mock_test_base->randomStringValidate($string);
  78. $this->assertEquals($expected, $actual);
  79. }
  80. /**
  81. * Provides data for testRandomString() and others.
  82. *
  83. * @return array
  84. * - The number of items (characters, object properties) we expect any of
  85. * the random functions to give us.
  86. */
  87. public function providerRandomItems() {
  88. return [
  89. [NULL],
  90. [0],
  91. [1],
  92. [2],
  93. [3],
  94. [4],
  95. [7],
  96. ];
  97. }
  98. /**
  99. * @covers ::randomString
  100. * @dataProvider providerRandomItems
  101. */
  102. public function testRandomString($length) {
  103. $mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase');
  104. $string = $mock_test_base->randomString($length);
  105. $this->assertEquals($length, strlen($string));
  106. // randomString() should always include an ampersand ('&') and a
  107. // greater than ('>') if $length is greater than 3.
  108. if ($length > 4) {
  109. $this->assertContains('&', $string);
  110. $this->assertContains('>', $string);
  111. }
  112. }
  113. /**
  114. * @covers ::randomObject
  115. * @dataProvider providerRandomItems
  116. */
  117. public function testRandomObject($size) {
  118. $test_base = $this->getTestBaseForAssertionTests('test_id');
  119. // Note: count((array)object) works for now, maybe not later.
  120. $this->assertEquals($size, count((array) $test_base->randomObject($size)));
  121. }
  122. /**
  123. * @covers ::checkRequirements
  124. */
  125. public function testCheckRequirements() {
  126. $test_base = $this->getTestBaseForAssertionTests('test_id');
  127. $this->assertInternalType(
  128. 'array',
  129. $this->invokeProtectedMethod($test_base, 'checkRequirements', [])
  130. );
  131. }
  132. /**
  133. * Data provider for testAssert().
  134. *
  135. * @return array
  136. * Standard dataProvider array of arrays:
  137. * - Expected result from assert().
  138. * - Expected status stored in TestBase->assertions.
  139. * - Status, passed to assert().
  140. * - Message, passed to assert().
  141. * - Group, passed to assert().
  142. * - Caller, passed to assert().
  143. */
  144. public function providerAssert() {
  145. return [
  146. [TRUE, 'pass', TRUE, 'Yay pass', 'test', []],
  147. [FALSE, 'fail', FALSE, 'Boo fail', 'test', []],
  148. [TRUE, 'pass', 'pass', 'Yay pass', 'test', []],
  149. [FALSE, 'fail', 'fail', 'Boo fail', 'test', []],
  150. [FALSE, 'exception', 'exception', 'Boo fail', 'test', []],
  151. [FALSE, 'debug', 'debug', 'Boo fail', 'test', []],
  152. ];
  153. }
  154. /**
  155. * @covers ::assert
  156. * @dataProvider providerAssert
  157. */
  158. public function testAssert($expected, $assertion_status, $status, $message, $group, $caller) {
  159. $test_id = 'luke_i_am_your_' . $assertion_status;
  160. $test_base = $this->getTestBaseForAssertionTests($test_id);
  161. // Verify some startup values.
  162. $this->assertAttributeEmpty('assertions', $test_base);
  163. if (is_string($status)) {
  164. $this->assertEquals(0, $test_base->results['#' . $status]);
  165. }
  166. // assert() is protected so we have to make it accessible.
  167. $ref_assert = new \ReflectionMethod($test_base, 'assert');
  168. $ref_assert->setAccessible(TRUE);
  169. // Call assert() from within our hall of mirrors.
  170. $this->assertEquals(
  171. $expected,
  172. $ref_assert->invokeArgs($test_base,
  173. [$status, $message, $group, $caller]
  174. )
  175. );
  176. // Check the side-effects of assert().
  177. if (is_string($status)) {
  178. $this->assertEquals(1, $test_base->results['#' . $status]);
  179. }
  180. $this->assertAttributeNotEmpty('assertions', $test_base);
  181. // Make a ReflectionProperty for the assertions property,
  182. // since it's protected.
  183. $ref_assertions = new \ReflectionProperty($test_base, 'assertions');
  184. $ref_assertions->setAccessible(TRUE);
  185. $assertions = $ref_assertions->getValue($test_base);
  186. $assertion = reset($assertions);
  187. $this->assertEquals($assertion_status, $assertion['status']);
  188. $this->assertEquals($test_id, $assertion['test_id']);
  189. $this->assertEquals(get_class($test_base), $assertion['test_class']);
  190. $this->assertEquals($message, $assertion['message']);
  191. $this->assertEquals($group, $assertion['message_group']);
  192. }
  193. /**
  194. * Data provider for assertTrue().
  195. */
  196. public function providerAssertTrue() {
  197. return [
  198. [TRUE, TRUE],
  199. [FALSE, FALSE],
  200. ];
  201. }
  202. /**
  203. * @covers ::assertTrue
  204. * @dataProvider providerAssertTrue
  205. */
  206. public function testAssertTrue($expected, $value) {
  207. $test_base = $this->getTestBaseForAssertionTests('test_id');
  208. $this->assertEquals(
  209. $expected,
  210. $this->invokeProtectedMethod($test_base, 'assertTrue', [$value])
  211. );
  212. }
  213. /**
  214. * @covers ::assertFalse
  215. * @dataProvider providerAssertTrue
  216. */
  217. public function testAssertFalse($expected, $value) {
  218. $test_base = $this->getTestBaseForAssertionTests('test_id');
  219. $this->assertEquals(
  220. (!$expected),
  221. $this->invokeProtectedMethod($test_base, 'assertFalse', [$value])
  222. );
  223. }
  224. /**
  225. * Data provider for assertNull().
  226. */
  227. public function providerAssertNull() {
  228. return [
  229. [TRUE, NULL],
  230. [FALSE, ''],
  231. ];
  232. }
  233. /**
  234. * @covers ::assertNull
  235. * @dataProvider providerAssertNull
  236. */
  237. public function testAssertNull($expected, $value) {
  238. $test_base = $this->getTestBaseForAssertionTests('test_id');
  239. $this->assertEquals(
  240. $expected,
  241. $this->invokeProtectedMethod($test_base, 'assertNull', [$value])
  242. );
  243. }
  244. /**
  245. * @covers ::assertNotNull
  246. * @dataProvider providerAssertNull
  247. */
  248. public function testAssertNotNull($expected, $value) {
  249. $test_base = $this->getTestBaseForAssertionTests('test_id');
  250. $this->assertEquals(
  251. (!$expected),
  252. $this->invokeProtectedMethod($test_base, 'assertNotNull', [$value])
  253. );
  254. }
  255. /**
  256. * Data provider for tests of equality assertions.
  257. *
  258. * Used by testAssertIdentical(), testAssertEqual(), testAssertNotIdentical(),
  259. * and testAssertNotEqual().
  260. *
  261. * @return
  262. * Array of test data.
  263. * - Expected assertion value for identical comparison.
  264. * - Expected assertion value for equal comparison.
  265. * - First value to compare.
  266. * - Second value to compare.
  267. */
  268. public function providerEqualityAssertions() {
  269. return [
  270. // Integers and floats.
  271. [TRUE, TRUE, 0, 0],
  272. [FALSE, TRUE, 0, 0.0],
  273. [FALSE, TRUE, '0', 0],
  274. [FALSE, TRUE, '0.0', 0.0],
  275. [FALSE, FALSE, 23, 77],
  276. [TRUE, TRUE, 23.0, 23.0],
  277. // Strings.
  278. [FALSE, FALSE, 'foof', 'yay'],
  279. [TRUE, TRUE, 'yay', 'yay'],
  280. // Bools with type conversion.
  281. [TRUE, TRUE, TRUE, TRUE],
  282. [TRUE, TRUE, FALSE, FALSE],
  283. [FALSE, TRUE, NULL, FALSE],
  284. [FALSE, TRUE, 'TRUE', TRUE],
  285. [FALSE, FALSE, 'FALSE', FALSE],
  286. [FALSE, TRUE, 0, FALSE],
  287. [FALSE, TRUE, 1, TRUE],
  288. [FALSE, TRUE, -1, TRUE],
  289. [FALSE, TRUE, '1', TRUE],
  290. [FALSE, TRUE, '1.3', TRUE],
  291. // Null.
  292. [FALSE, FALSE, 'NULL', NULL],
  293. [TRUE, TRUE, NULL, NULL],
  294. ];
  295. }
  296. /**
  297. * @covers ::assertIdentical
  298. * @dataProvider providerEqualityAssertions
  299. */
  300. public function testAssertIdentical($expected_identical, $expected_equal, $first, $second) {
  301. $test_base = $this->getTestBaseForAssertionTests('test_id');
  302. $this->assertEquals(
  303. $expected_identical,
  304. $this->invokeProtectedMethod($test_base, 'assertIdentical', [$first, $second])
  305. );
  306. }
  307. /**
  308. * @covers ::assertNotIdentical
  309. * @dataProvider providerEqualityAssertions
  310. */
  311. public function testAssertNotIdentical($expected_identical, $expected_equal, $first, $second) {
  312. $test_base = $this->getTestBaseForAssertionTests('test_id');
  313. $this->assertEquals(
  314. (!$expected_identical),
  315. $this->invokeProtectedMethod($test_base, 'assertNotIdentical', [$first, $second])
  316. );
  317. }
  318. /**
  319. * @covers ::assertEqual
  320. * @dataProvider providerEqualityAssertions
  321. */
  322. public function testAssertEqual($expected_identical, $expected_equal, $first, $second) {
  323. $test_base = $this->getTestBaseForAssertionTests('test_id');
  324. $this->assertEquals(
  325. $expected_equal,
  326. $this->invokeProtectedMethod($test_base, 'assertEqual', [$first, $second])
  327. );
  328. }
  329. /**
  330. * @covers ::assertNotEqual
  331. * @dataProvider providerEqualityAssertions
  332. */
  333. public function testAssertNotEqual($expected_identical, $expected_equal, $first, $second) {
  334. $test_base = $this->getTestBaseForAssertionTests('test_id');
  335. $this->assertEquals(
  336. (!$expected_equal),
  337. $this->invokeProtectedMethod($test_base, 'assertNotEqual', [$first, $second])
  338. );
  339. }
  340. /**
  341. * Data provider for testAssertIdenticalObject().
  342. */
  343. public function providerAssertIdenticalObject() {
  344. $obj1 = new \stdClass();
  345. $obj1->foof = 'yay';
  346. $obj2 = $obj1;
  347. $obj3 = clone $obj1;
  348. $obj4 = new \stdClass();
  349. return [
  350. [TRUE, $obj1, $obj2],
  351. [TRUE, $obj1, $obj3],
  352. [FALSE, $obj1, $obj4],
  353. ];
  354. }
  355. /**
  356. * @covers ::assertIdenticalObject
  357. * @dataProvider providerAssertIdenticalObject
  358. */
  359. public function testAssertIdenticalObject($expected, $first, $second) {
  360. $test_base = $this->getTestBaseForAssertionTests('test_id');
  361. $this->assertEquals(
  362. $expected,
  363. $this->invokeProtectedMethod($test_base, 'assertIdenticalObject', [$first, $second])
  364. );
  365. }
  366. /**
  367. * @covers ::pass
  368. */
  369. public function testPass() {
  370. $test_base = $this->getTestBaseForAssertionTests('test_id');
  371. $this->assertEquals(
  372. TRUE,
  373. $this->invokeProtectedMethod($test_base, 'pass', [])
  374. );
  375. }
  376. /**
  377. * @covers ::fail
  378. */
  379. public function testFail() {
  380. $test_base = $this->getTestBaseForAssertionTests('test_id');
  381. $this->assertEquals(
  382. FALSE,
  383. $this->invokeProtectedMethod($test_base, 'fail', [])
  384. );
  385. }
  386. /**
  387. * Data provider for testError().
  388. *
  389. * @return array
  390. * - Expected status for assertion.
  391. * - Group for use in assert().
  392. */
  393. public function providerError() {
  394. return [
  395. ['debug', 'User notice'],
  396. ['exception', 'Not User notice'],
  397. ];
  398. }
  399. /**
  400. * @covers ::error
  401. * @dataProvider providerError
  402. */
  403. public function testError($status, $group) {
  404. // Mock up a TestBase object.
  405. $mock_test_base = $this->getMockBuilder('Drupal\simpletest\TestBase')
  406. ->setMethods(['assert'])
  407. ->getMockForAbstractClass();
  408. // Set expectations for assert().
  409. $mock_test_base->expects($this->once())
  410. ->method('assert')
  411. // The first argument to assert() should be the expected $status. This is
  412. // the most important expectation of this test.
  413. ->with($status)
  414. // Arbitrary return value.
  415. ->willReturn("$status:$group");
  416. // Invoke error().
  417. $this->assertEquals(
  418. "$status:$group",
  419. $this->invokeProtectedMethod($mock_test_base, 'error', ['msg', $group])
  420. );
  421. }
  422. /**
  423. * @covers ::getRandomGenerator
  424. */
  425. public function testGetRandomGenerator() {
  426. $test_base = $this->getTestBaseForAssertionTests('test_id');
  427. $this->assertInstanceOf(
  428. 'Drupal\Component\Utility\Random',
  429. $this->invokeProtectedMethod($test_base, 'getRandomGenerator', [])
  430. );
  431. }
  432. }