MockBuilderTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * This file is part of the phpunit-mock-objects package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use PHPUnit\Framework\TestCase;
  11. use PHPUnit\Framework\MockObject\MockBuilder;
  12. class MockBuilderTest extends TestCase
  13. {
  14. public function testMockBuilderRequiresClassName()
  15. {
  16. $mock = $this->getMockBuilder(Mockable::class)->getMock();
  17. $this->assertTrue($mock instanceof Mockable);
  18. }
  19. public function testByDefaultMocksAllMethods()
  20. {
  21. $mock = $this->getMockBuilder(Mockable::class)->getMock();
  22. $this->assertNull($mock->mockableMethod());
  23. $this->assertNull($mock->anotherMockableMethod());
  24. }
  25. public function testMethodsToMockCanBeSpecified()
  26. {
  27. $mock = $this->getMockBuilder(Mockable::class)
  28. ->setMethods(['mockableMethod'])
  29. ->getMock();
  30. $this->assertNull($mock->mockableMethod());
  31. $this->assertTrue($mock->anotherMockableMethod());
  32. }
  33. public function testMethodExceptionsToMockCanBeSpecified()
  34. {
  35. $mock = $this->getMockBuilder(Mockable::class)
  36. ->setMethodsExcept(['mockableMethod'])
  37. ->getMock();
  38. $this->assertTrue($mock->mockableMethod());
  39. $this->assertNull($mock->anotherMockableMethod());
  40. }
  41. public function testEmptyMethodExceptionsToMockCanBeSpecified()
  42. {
  43. $mock = $this->getMockBuilder(Mockable::class)
  44. ->setMethodsExcept()
  45. ->getMock();
  46. $this->assertNull($mock->mockableMethod());
  47. $this->assertNull($mock->anotherMockableMethod());
  48. }
  49. public function testByDefaultDoesNotPassArgumentsToTheConstructor()
  50. {
  51. $mock = $this->getMockBuilder(Mockable::class)->getMock();
  52. $this->assertEquals([null, null], $mock->constructorArgs);
  53. }
  54. public function testMockClassNameCanBeSpecified()
  55. {
  56. $mock = $this->getMockBuilder(Mockable::class)
  57. ->setMockClassName('ACustomClassName')
  58. ->getMock();
  59. $this->assertTrue($mock instanceof ACustomClassName);
  60. }
  61. public function testConstructorArgumentsCanBeSpecified()
  62. {
  63. $mock = $this->getMockBuilder(Mockable::class)
  64. ->setConstructorArgs([23, 42])
  65. ->getMock();
  66. $this->assertEquals([23, 42], $mock->constructorArgs);
  67. }
  68. public function testOriginalConstructorCanBeDisabled()
  69. {
  70. $mock = $this->getMockBuilder(Mockable::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->assertNull($mock->constructorArgs);
  74. }
  75. public function testByDefaultOriginalCloneIsPreserved()
  76. {
  77. $mock = $this->getMockBuilder(Mockable::class)
  78. ->getMock();
  79. $cloned = clone $mock;
  80. $this->assertTrue($cloned->cloned);
  81. }
  82. public function testOriginalCloneCanBeDisabled()
  83. {
  84. $mock = $this->getMockBuilder(Mockable::class)
  85. ->disableOriginalClone()
  86. ->getMock();
  87. $mock->cloned = false;
  88. $cloned = clone $mock;
  89. $this->assertFalse($cloned->cloned);
  90. }
  91. public function testProvidesAFluentInterface()
  92. {
  93. $spec = $this->getMockBuilder(Mockable::class)
  94. ->setMethods(['mockableMethod'])
  95. ->setConstructorArgs([])
  96. ->setMockClassName('DummyClassName')
  97. ->disableOriginalConstructor()
  98. ->disableOriginalClone()
  99. ->disableAutoload();
  100. $this->assertTrue($spec instanceof MockBuilder);
  101. }
  102. }