MockBuilderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of the PHPUnit_MockObject 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. class Framework_MockBuilderTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testMockBuilderRequiresClassName()
  13. {
  14. $mock = $this->getMockBuilder(Mockable::class)->getMock();
  15. $this->assertTrue($mock instanceof Mockable);
  16. }
  17. public function testByDefaultMocksAllMethods()
  18. {
  19. $mock = $this->getMockBuilder(Mockable::class)->getMock();
  20. $this->assertNull($mock->mockableMethod());
  21. $this->assertNull($mock->anotherMockableMethod());
  22. }
  23. public function testMethodsToMockCanBeSpecified()
  24. {
  25. $mock = $this->getMockBuilder(Mockable::class)
  26. ->setMethods(['mockableMethod'])
  27. ->getMock();
  28. $this->assertNull($mock->mockableMethod());
  29. $this->assertTrue($mock->anotherMockableMethod());
  30. }
  31. public function testMethodExceptionsToMockCanBeSpecified()
  32. {
  33. $mock = $this->getMockBuilder(Mockable::class)
  34. ->setMethodsExcept(['mockableMethod'])
  35. ->getMock();
  36. $this->assertTrue($mock->mockableMethod());
  37. $this->assertNull($mock->anotherMockableMethod());
  38. }
  39. public function testEmptyMethodExceptionsToMockCanBeSpecified()
  40. {
  41. $mock = $this->getMockBuilder(Mockable::class)
  42. ->setMethodsExcept()
  43. ->getMock();
  44. $this->assertNull($mock->mockableMethod());
  45. $this->assertNull($mock->anotherMockableMethod());
  46. }
  47. public function testByDefaultDoesNotPassArgumentsToTheConstructor()
  48. {
  49. $mock = $this->getMockBuilder(Mockable::class)->getMock();
  50. $this->assertEquals([null, null], $mock->constructorArgs);
  51. }
  52. public function testMockClassNameCanBeSpecified()
  53. {
  54. $mock = $this->getMockBuilder(Mockable::class)
  55. ->setMockClassName('ACustomClassName')
  56. ->getMock();
  57. $this->assertTrue($mock instanceof ACustomClassName);
  58. }
  59. public function testConstructorArgumentsCanBeSpecified()
  60. {
  61. $mock = $this->getMockBuilder(Mockable::class)
  62. ->setConstructorArgs([23, 42])
  63. ->getMock();
  64. $this->assertEquals([23, 42], $mock->constructorArgs);
  65. }
  66. public function testOriginalConstructorCanBeDisabled()
  67. {
  68. $mock = $this->getMockBuilder(Mockable::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->assertNull($mock->constructorArgs);
  72. }
  73. public function testByDefaultOriginalCloneIsPreserved()
  74. {
  75. $mock = $this->getMockBuilder(Mockable::class)
  76. ->getMock();
  77. $cloned = clone $mock;
  78. $this->assertTrue($cloned->cloned);
  79. }
  80. public function testOriginalCloneCanBeDisabled()
  81. {
  82. $mock = $this->getMockBuilder(Mockable::class)
  83. ->disableOriginalClone()
  84. ->getMock();
  85. $mock->cloned = false;
  86. $cloned = clone $mock;
  87. $this->assertFalse($cloned->cloned);
  88. }
  89. public function testProvidesAFluentInterface()
  90. {
  91. $spec = $this->getMockBuilder(Mockable::class)
  92. ->setMethods(['mockableMethod'])
  93. ->setConstructorArgs([])
  94. ->setMockClassName('DummyClassName')
  95. ->disableOriginalConstructor()
  96. ->disableOriginalClone()
  97. ->disableAutoload();
  98. $this->assertTrue($spec instanceof PHPUnit_Framework_MockObject_MockBuilder);
  99. }
  100. }