Skip to content

Commit

Permalink
Issue mockery#1056: Add configuration to decide how quik definitions …
Browse files Browse the repository at this point in the history
…should behave
  • Loading branch information
danydev committed May 25, 2020
1 parent eca936d commit 2b6c311
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
37 changes: 37 additions & 0 deletions library/Mockery/Configuration.php
Expand Up @@ -22,6 +22,9 @@

class Configuration
{
const QUICK_DEFINITIONS_APPLICATION_MODE_MOCK_AT_LEAST_ONCE = 'QUICK_DEFINITIONS_APPLICATION_MODE_MOCK_AT_LEAST_ONCE';
const QUICK_DEFINITIONS_APPLICATION_MODE_DEFAULT_EXPECTATION = 'QUICK_DEFINITIONS_APPLICATION_MODE_DEFAULT_EXPECTATION';

/**
* Boolean assertion of whether we can mock methods which do not actually
* exist for the given class or object (ignored for unreal mocks)
Expand All @@ -40,6 +43,13 @@ class Configuration
*/
protected $_allowMockingMethodsUnnecessarily = true;

/**
* Defines what a quick definition should produce.
*
* @var string
*/
protected $_quickDefinitionsApplicationMode = self::QUICK_DEFINITIONS_APPLICATION_MODE_DEFAULT_EXPECTATION;

/**
* Parameter map for use with PHP internal classes.
*
Expand Down Expand Up @@ -103,6 +113,33 @@ public function mockingMethodsUnnecessarilyAllowed()
return $this->_allowMockingMethodsUnnecessarily;
}

/**
* Set application mode for quick definitions
* See \Mockery\Configuration::QUICK_DEFINITIONS_APPLICATION_MODE_* for accepted values.
*
* @param string $applicationMode
*/
public function setQuickDefinitionsApplicationMode($applicationMode)
{
if (!in_array($applicationMode, array(
self::QUICK_DEFINITIONS_APPLICATION_MODE_DEFAULT_EXPECTATION,
self::QUICK_DEFINITIONS_APPLICATION_MODE_MOCK_AT_LEAST_ONCE
))) {
throw new \InvalidArgumentException('Unexpected application mode: ', $applicationMode);
}
$this->_quickDefinitionsApplicationMode = $applicationMode;
}

/**
* Get the quick definitions application mode.
*
* @return string See \Mockery\Configuration::QUICK_DEFINITIONS_APPLICATION_MODE_* for possible returned values.
*/
public function getQuickDefinitionsApplicationMode()
{
return $this->_quickDefinitionsApplicationMode;
}

/**
* Set a parameter map (array of param signature strings) for the method
* of an internal PHP class.
Expand Down
6 changes: 5 additions & 1 deletion library/Mockery/Container.php
Expand Up @@ -233,7 +233,11 @@ public function mock(...$args)
$mock->mockery_init($this, $config->getTargetObject(), $config->isInstanceMock());

if (!empty($quickdefs)) {
$mock->shouldReceive($quickdefs)->byDefault();
if (\Mockery::getConfiguration()->getQuickDefinitionsApplicationMode() === \Mockery\Configuration::QUICK_DEFINITIONS_APPLICATION_MODE_DEFAULT_EXPECTATION) {
$mock->shouldReceive($quickdefs)->byDefault();
} elseif (\Mockery::getConfiguration()->getQuickDefinitionsApplicationMode() === \Mockery\Configuration::QUICK_DEFINITIONS_APPLICATION_MODE_MOCK_AT_LEAST_ONCE) {
$mock->shouldReceive($quickdefs)->atLeast()->once();
}
}
if (!empty($expectationClosure)) {
$expectationClosure($mock);
Expand Down
17 changes: 17 additions & 0 deletions tests/Mockery/ExpectationTest.php
Expand Up @@ -20,6 +20,7 @@
*/

use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Configuration;
use Mockery\Exception\InvalidCountException;
use Mockery\MockInterface;

Expand Down Expand Up @@ -1866,6 +1867,22 @@ public function testGlobalConfigMayForbidMockingNonExistentMethodsOnObjects()
Mockery::close();
}

public function testGlobalConfigQuickDefinitionsApplicationModeDefaultExpectation()
{
\Mockery::getConfiguration()->setQuickDefinitionsApplicationMode(Configuration::QUICK_DEFINITIONS_APPLICATION_MODE_DEFAULT_EXPECTATION);
mock(array('foo'=>1));
$this->expectNotToPerformAssertions();
Mockery::close();
}

public function testGlobalConfigQuickDefinitionsApplicationModeMockAtLeastOnce()
{
\Mockery::getConfiguration()->setQuickDefinitionsApplicationMode(Configuration::QUICK_DEFINITIONS_APPLICATION_MODE_MOCK_AT_LEAST_ONCE);
mock(array('foo'=>1));
$this->expectException(\Mockery\Exception\InvalidCountException::class);
Mockery::close();
}

public function testAnExampleWithSomeExpectationAmends()
{
$service = mock('MyService');
Expand Down

0 comments on commit 2b6c311

Please sign in to comment.