PhpunitCompatibilityTraitTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Drupal\Tests;
  3. /**
  4. * Tests the PHPUnit forward compatibility trait.
  5. *
  6. * @coversDefaultClass \Drupal\Tests\PhpunitCompatibilityTrait
  7. * @group Tests
  8. */
  9. class PhpunitCompatibilityTraitTest extends UnitTestCase {
  10. /**
  11. * Tests that getMock is available and calls the correct parent method.
  12. *
  13. * @covers ::getMock
  14. * @dataProvider providerMockVersions
  15. */
  16. public function testGetMock($className, $expected) {
  17. $class = new $className();
  18. $this->assertSame($expected, $class->getMock($this->randomMachineName()));
  19. }
  20. /**
  21. * Tests that createMock is available and calls the correct parent method.
  22. *
  23. * @covers ::createMock
  24. * @dataProvider providerMockVersions
  25. */
  26. public function testCreateMock($className, $expected) {
  27. $class = new $className();
  28. $this->assertSame($expected, $class->createMock($this->randomMachineName()));
  29. }
  30. /**
  31. * Returns the class names and the string they return.
  32. *
  33. * @return array
  34. */
  35. public function providerMockVersions() {
  36. return [
  37. [UnitTestCasePhpunit4TestClass::class, 'PHPUnit 4'],
  38. [UnitTestCasePhpunit4TestClassExtends::class, 'PHPUnit 4'],
  39. [UnitTestCasePhpunit6TestClass::class, 'PHPUnit 6'],
  40. [UnitTestCasePhpunit6TestClassExtends::class, 'PHPUnit 6'],
  41. ];
  42. }
  43. }
  44. /**
  45. * Test class for \PHPUnit\Framework\TestCase in PHPUnit 4.
  46. */
  47. class Phpunit4TestClass {
  48. public function getMock($originalClassName) {
  49. return 'PHPUnit 4';
  50. }
  51. }
  52. /**
  53. * Test class for \PHPUnit\Framework\TestCase in PHPUnit 6.
  54. */
  55. class Phpunit6TestClass {
  56. public function createMock($originalClassName) {
  57. return 'PHPUnit 6';
  58. }
  59. public function getMockbuilder() {
  60. return new Mockbuilder();
  61. }
  62. }
  63. /**
  64. * Test double for PHPUnit_Framework_MockObject_MockBuilder.
  65. */
  66. class Mockbuilder {
  67. public function __call($name, $arguments) {
  68. return $this;
  69. }
  70. public function getMock() {
  71. return 'PHPUnit 6';
  72. }
  73. }
  74. /**
  75. * Test class for \Drupal\Tests\UnitTestCase with PHPUnit 4.
  76. */
  77. class UnitTestCasePhpunit4TestClass extends Phpunit4TestClass {
  78. use PhpunitCompatibilityTrait;
  79. }
  80. /**
  81. * Test class for \Drupal\Tests\UnitTestCase with PHPUnit 4.
  82. */
  83. class UnitTestCasePhpunit4TestClassExtends extends UnitTestCasePhpunit4TestClass {
  84. }
  85. /**
  86. * Test class for \Drupal\Tests\UnitTestCase with PHPUnit 6.
  87. */
  88. class UnitTestCasePhpunit6TestClass extends Phpunit6TestClass {
  89. use PhpunitCompatibilityTrait;
  90. }
  91. /**
  92. * Test class for \Drupal\Tests\UnitTestCase with PHPUnit 6.
  93. */
  94. class UnitTestCasePhpunit6TestClassExtends extends UnitTestCasePhpunit6TestClass {
  95. }