PK LKi .travis.ymlnu W+A language: php
php:
- 7.0
- 7.0snapshot
- 7.1
- 7.1snapshot
- master
sudo: false
before_install:
- composer self-update
- composer clear-cache
install:
- travis_retry composer update --no-interaction --no-ansi --no-progress --no-suggest --optimize-autoloader --prefer-stable
script:
- ./vendor/bin/phpunit --coverage-clover=coverage.xml
after_success:
- bash <(curl -s https://codecov.io/bash)
notifications:
email: false
PK LK!2^ ^ .php_csnu W+A
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(
[
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'align_double_arrow' => true,
'align_equals' => true
],
'blank_line_after_namespace' => true,
'blank_line_before_return' => true,
'braces' => true,
'cast_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
'function_declaration' => true,
'header_comment' => ['header' => $header, 'separate' => 'none'],
'indentation_type' => true,
'line_ending' => true,
'lowercase_constants' => true,
'lowercase_keywords' => true,
'method_argument_space' => true,
'no_alias_functions' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_leading_namespace_whitespace' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_whitespace' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'self_accessor' => true,
'simplified_null_return' => true,
'single_blank_line_at_eof' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'ternary_operator_spaces' => true,
'trim_array_spaces' => true,
'visibility_required' => true,
]
)
->setFinder(
PhpCsFixer\Finder::create()
->files()
->in(__DIR__ . '/src')
->name('*.php')
);
PK LKL+ + README.mdnu W+A [![Latest Stable Version](https://img.shields.io/packagist/v/phpunit/phpunit-mock-objects.svg?style=flat-square)](https://packagist.org/packages/phpunit/phpunit-mock-objects)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg?style=flat-square)](https://php.net/)
[![Build Status](https://img.shields.io/travis/sebastianbergmann/phpunit-mock-objects/master.svg?style=flat-square)](https://travis-ci.org/sebastianbergmann/phpunit-mock-objects)
# PHPUnit_MockObject
**PHPUnit_MockObject** is the default mock object library for PHPUnit.
## Requirements
* PHP 5.6 is required but using the latest version of PHP is highly recommended
## Installation
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
composer require phpunit/phpunit-mock-objects
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
composer require --dev phpunit/phpunit-mock-objects
PK LK9&- CONTRIBUTING.mdnu W+A Please refer to [https://github.com/sebastianbergmann/phpunit/blob/master/.github/CONTRIBUTING.md](https://github.com/sebastianbergmann/phpunit/blob/master/.github/CONTRIBUTING.md) for details on how to contribute to this project.
PK LK<- -
.gitignorenu W+A /.idea
/.php_cs.cache
/composer.lock
/vendor
PK LK-5_r r phpunit.xmlnu W+A
tests
tests
src
PK LKZ>ؘ ؘ src/Generator.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Doctrine\Instantiator\Instantiator;
use Doctrine\Instantiator\Exception\InvalidArgumentException as InstantiatorInvalidArgumentException;
use Doctrine\Instantiator\Exception\UnexpectedValueException as InstantiatorUnexpectedValueException;
use PHPUnit\Util\InvalidArgumentHelper;
/**
* Mock Object Code Generator
*/
class PHPUnit_Framework_MockObject_Generator
{
/**
* @var array
*/
private static $cache = [];
/**
* @var Text_Template[]
*/
private static $templates = [];
/**
* @var array
*/
private $blacklistedMethodNames = [
'__CLASS__' => true,
'__DIR__' => true,
'__FILE__' => true,
'__FUNCTION__' => true,
'__LINE__' => true,
'__METHOD__' => true,
'__NAMESPACE__' => true,
'__TRAIT__' => true,
'__clone' => true,
'__halt_compiler' => true,
];
/**
* Returns a mock object for the specified class.
*
* @param string|string[] $type
* @param array $methods
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param bool $cloneArguments
* @param bool $callOriginalMethods
* @param object $proxyTarget
* @param bool $allowMockingUnknownTypes
*
* @return PHPUnit_Framework_MockObject_MockObject
*
* @throws InvalidArgumentException
* @throws PHPUnit\Framework\Exception
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
public function getMock($type, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null, $allowMockingUnknownTypes = true)
{
if (!is_array($type) && !is_string($type)) {
throw InvalidArgumentHelper::factory(1, 'array or string');
}
if (!is_string($mockClassName)) {
throw InvalidArgumentHelper::factory(4, 'string');
}
if (!is_array($methods) && !is_null($methods)) {
throw InvalidArgumentHelper::factory(2, 'array', $methods);
}
if ($type === 'Traversable' || $type === '\\Traversable') {
$type = 'Iterator';
}
if (is_array($type)) {
$type = array_unique(
array_map(
function ($type) {
if ($type === 'Traversable' ||
$type === '\\Traversable' ||
$type === '\\Iterator') {
return 'Iterator';
}
return $type;
},
$type
)
);
}
if (!$allowMockingUnknownTypes) {
if (is_array($type)) {
foreach ($type as $_type) {
if (!class_exists($_type, $callAutoload) &&
!interface_exists($_type, $callAutoload)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot stub or mock class or interface "%s" which does not exist',
$_type
)
);
}
}
} else {
if (!class_exists($type, $callAutoload) &&
!interface_exists($type, $callAutoload)
) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot stub or mock class or interface "%s" which does not exist',
$type
)
);
}
}
}
if (null !== $methods) {
foreach ($methods as $method) {
if (!preg_match('~[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*~', $method)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot stub or mock method with invalid name "%s"',
$method
)
);
}
}
if ($methods != array_unique($methods)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot stub or mock using a method list that contains duplicates: "%s" (duplicate: "%s")',
implode(', ', $methods),
implode(', ', array_unique(array_diff_assoc($methods, array_unique($methods))))
)
);
}
}
if ($mockClassName != '' && class_exists($mockClassName, false)) {
$reflect = new ReflectionClass($mockClassName);
if (!$reflect->implementsInterface('PHPUnit_Framework_MockObject_MockObject')) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Class "%s" already exists.',
$mockClassName
)
);
}
}
if ($callOriginalConstructor === false && $callOriginalMethods === true) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Proxying to original methods requires invoking the original constructor'
);
}
$mock = $this->generate(
$type,
$methods,
$mockClassName,
$callOriginalClone,
$callAutoload,
$cloneArguments,
$callOriginalMethods
);
return $this->getObject(
$mock['code'],
$mock['mockClassName'],
$type,
$callOriginalConstructor,
$callAutoload,
$arguments,
$callOriginalMethods,
$proxyTarget
);
}
/**
* @param string $code
* @param string $className
* @param array|string $type
* @param bool $callOriginalConstructor
* @param bool $callAutoload
* @param array $arguments
* @param bool $callOriginalMethods
* @param object $proxyTarget
*
* @return PHPUnit_Framework_MockObject_MockObject
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
private function getObject($code, $className, $type = '', $callOriginalConstructor = false, $callAutoload = false, array $arguments = [], $callOriginalMethods = false, $proxyTarget = null)
{
$this->evalClass($code, $className);
if ($callOriginalConstructor &&
is_string($type) &&
!interface_exists($type, $callAutoload)) {
if (count($arguments) == 0) {
$object = new $className;
} else {
$class = new ReflectionClass($className);
$object = $class->newInstanceArgs($arguments);
}
} else {
try {
$instantiator = new Instantiator;
$object = $instantiator->instantiate($className);
} catch (InstantiatorUnexpectedValueException $exception) {
if ($exception->getPrevious()) {
$exception = $exception->getPrevious();
}
throw new PHPUnit_Framework_MockObject_RuntimeException(
$exception->getMessage()
);
} catch (InstantiatorInvalidArgumentException $exception) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
$exception->getMessage()
);
}
}
if ($callOriginalMethods) {
if (!is_object($proxyTarget)) {
if (count($arguments) == 0) {
$proxyTarget = new $type;
} else {
$class = new ReflectionClass($type);
$proxyTarget = $class->newInstanceArgs($arguments);
}
}
$object->__phpunit_setOriginalObject($proxyTarget);
}
return $object;
}
/**
* @param string $code
* @param string $className
*/
private function evalClass($code, $className)
{
if (!class_exists($className, false)) {
eval($code);
}
}
/**
* Returns a mock object for the specified abstract class with all abstract
* methods of the class mocked. Concrete methods to mock can be specified with
* the last parameter
*
* @param string $originalClassName
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param array $mockedMethods
* @param bool $cloneArguments
*
* @return PHPUnit_Framework_MockObject_MockObject
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
* @throws PHPUnit\Framework\Exception
*/
public function getMockForAbstractClass($originalClassName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true)
{
if (!is_string($originalClassName)) {
throw InvalidArgumentHelper::factory(1, 'string');
}
if (!is_string($mockClassName)) {
throw InvalidArgumentHelper::factory(3, 'string');
}
if (class_exists($originalClassName, $callAutoload) ||
interface_exists($originalClassName, $callAutoload)) {
$reflector = new ReflectionClass($originalClassName);
$methods = $mockedMethods;
foreach ($reflector->getMethods() as $method) {
if ($method->isAbstract() && !in_array($method->getName(), $methods)) {
$methods[] = $method->getName();
}
}
if (empty($methods)) {
$methods = null;
}
return $this->getMock(
$originalClassName,
$methods,
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$cloneArguments
);
}
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf('Class "%s" does not exist.', $originalClassName)
);
}
/**
* Returns a mock object for the specified trait with all abstract methods
* of the trait mocked. Concrete methods to mock can be specified with the
* `$mockedMethods` parameter.
*
* @param string $traitName
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param array $mockedMethods
* @param bool $cloneArguments
*
* @return PHPUnit_Framework_MockObject_MockObject
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
* @throws PHPUnit\Framework\Exception
*/
public function getMockForTrait($traitName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true)
{
if (!is_string($traitName)) {
throw InvalidArgumentHelper::factory(1, 'string');
}
if (!is_string($mockClassName)) {
throw InvalidArgumentHelper::factory(3, 'string');
}
if (!trait_exists($traitName, $callAutoload)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Trait "%s" does not exist.',
$traitName
)
);
}
$className = $this->generateClassName(
$traitName,
'',
'Trait_'
);
$classTemplate = $this->getTemplate('trait_class.tpl');
$classTemplate->setVar(
[
'prologue' => 'abstract ',
'class_name' => $className['className'],
'trait_name' => $traitName
]
);
$this->evalClass(
$classTemplate->render(),
$className['className']
);
return $this->getMockForAbstractClass($className['className'], $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $mockedMethods, $cloneArguments);
}
/**
* Returns an object for the specified trait.
*
* @param string $traitName
* @param array $arguments
* @param string $traitClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
*
* @return object
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
* @throws PHPUnit\Framework\Exception
*/
public function getObjectForTrait($traitName, array $arguments = [], $traitClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true)
{
if (!is_string($traitName)) {
throw InvalidArgumentHelper::factory(1, 'string');
}
if (!is_string($traitClassName)) {
throw InvalidArgumentHelper::factory(3, 'string');
}
if (!trait_exists($traitName, $callAutoload)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Trait "%s" does not exist.',
$traitName
)
);
}
$className = $this->generateClassName(
$traitName,
$traitClassName,
'Trait_'
);
$classTemplate = $this->getTemplate('trait_class.tpl');
$classTemplate->setVar(
[
'prologue' => '',
'class_name' => $className['className'],
'trait_name' => $traitName
]
);
return $this->getObject(
$classTemplate->render(),
$className['className']
);
}
/**
* @param array|string $type
* @param array $methods
* @param string $mockClassName
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param bool $cloneArguments
* @param bool $callOriginalMethods
*
* @return array
*/
public function generate($type, array $methods = null, $mockClassName = '', $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false)
{
if (is_array($type)) {
sort($type);
}
if ($mockClassName == '') {
$key = md5(
is_array($type) ? implode('_', $type) : $type .
serialize($methods) .
serialize($callOriginalClone) .
serialize($cloneArguments) .
serialize($callOriginalMethods)
);
if (isset(self::$cache[$key])) {
return self::$cache[$key];
}
}
$mock = $this->generateMock(
$type,
$methods,
$mockClassName,
$callOriginalClone,
$callAutoload,
$cloneArguments,
$callOriginalMethods
);
if (isset($key)) {
self::$cache[$key] = $mock;
}
return $mock;
}
/**
* @param string $wsdlFile
* @param string $className
* @param array $methods
* @param array $options
*
* @return string
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
public function generateClassFromWsdl($wsdlFile, $className, array $methods = [], array $options = [])
{
if (!extension_loaded('soap')) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'The SOAP extension is required to generate a mock object from WSDL.'
);
}
$options = array_merge($options, ['cache_wsdl' => WSDL_CACHE_NONE]);
$client = new SoapClient($wsdlFile, $options);
$_methods = array_unique($client->__getFunctions());
unset($client);
sort($_methods);
$methodTemplate = $this->getTemplate('wsdl_method.tpl');
$methodsBuffer = '';
foreach ($_methods as $method) {
$nameStart = strpos($method, ' ') + 1;
$nameEnd = strpos($method, '(');
$name = substr($method, $nameStart, $nameEnd - $nameStart);
if (empty($methods) || in_array($name, $methods)) {
$args = explode(
',',
substr(
$method,
$nameEnd + 1,
strpos($method, ')') - $nameEnd - 1
)
);
foreach (range(0, count($args) - 1) as $i) {
$args[$i] = substr($args[$i], strpos($args[$i], '$'));
}
$methodTemplate->setVar(
[
'method_name' => $name,
'arguments' => implode(', ', $args)
]
);
$methodsBuffer .= $methodTemplate->render();
}
}
$optionsBuffer = 'array(';
foreach ($options as $key => $value) {
$optionsBuffer .= $key . ' => ' . $value;
}
$optionsBuffer .= ')';
$classTemplate = $this->getTemplate('wsdl_class.tpl');
$namespace = '';
if (strpos($className, '\\') !== false) {
$parts = explode('\\', $className);
$className = array_pop($parts);
$namespace = 'namespace ' . implode('\\', $parts) . ';' . "\n\n";
}
$classTemplate->setVar(
[
'namespace' => $namespace,
'class_name' => $className,
'wsdl' => $wsdlFile,
'options' => $optionsBuffer,
'methods' => $methodsBuffer
]
);
return $classTemplate->render();
}
/**
* @param array|string $type
* @param array|null $methods
* @param string $mockClassName
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param bool $cloneArguments
* @param bool $callOriginalMethods
*
* @return array
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
private function generateMock($type, $methods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods)
{
$methodReflections = [];
$classTemplate = $this->getTemplate('mocked_class.tpl');
$additionalInterfaces = [];
$cloneTemplate = '';
$isClass = false;
$isInterface = false;
$isMultipleInterfaces = false;
if (is_array($type)) {
foreach ($type as $_type) {
if (!interface_exists($_type, $callAutoload)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Interface "%s" does not exist.',
$_type
)
);
}
$isMultipleInterfaces = true;
$additionalInterfaces[] = $_type;
$typeClass = new ReflectionClass($this->generateClassName(
$_type,
$mockClassName,
'Mock_'
)['fullClassName']
);
foreach ($this->getClassMethods($_type) as $method) {
if (in_array($method, $methods)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Duplicate method "%s" not allowed.',
$method
)
);
}
$methodReflections[$method] = $typeClass->getMethod($method);
$methods[] = $method;
}
}
}
$mockClassName = $this->generateClassName(
$type,
$mockClassName,
'Mock_'
);
if (class_exists($mockClassName['fullClassName'], $callAutoload)) {
$isClass = true;
} elseif (interface_exists($mockClassName['fullClassName'], $callAutoload)) {
$isInterface = true;
}
if (!$isClass && !$isInterface) {
$prologue = 'class ' . $mockClassName['originalClassName'] . "\n{\n}\n\n";
if (!empty($mockClassName['namespaceName'])) {
$prologue = 'namespace ' . $mockClassName['namespaceName'] .
" {\n\n" . $prologue . "}\n\n" .
"namespace {\n\n";
$epilogue = "\n\n}";
}
$cloneTemplate = $this->getTemplate('mocked_clone.tpl');
} else {
$class = new ReflectionClass($mockClassName['fullClassName']);
if ($class->isFinal()) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Class "%s" is declared "final" and cannot be mocked.',
$mockClassName['fullClassName']
)
);
}
if ($class->hasMethod('__clone')) {
$cloneMethod = $class->getMethod('__clone');
if (!$cloneMethod->isFinal()) {
if ($callOriginalClone && !$isInterface) {
$cloneTemplate = $this->getTemplate('unmocked_clone.tpl');
} else {
$cloneTemplate = $this->getTemplate('mocked_clone.tpl');
}
}
} else {
$cloneTemplate = $this->getTemplate('mocked_clone.tpl');
}
}
if (is_object($cloneTemplate)) {
$cloneTemplate = $cloneTemplate->render();
}
if (is_array($methods) && empty($methods) &&
($isClass || $isInterface)) {
$methods = $this->getClassMethods($mockClassName['fullClassName']);
}
if (!is_array($methods)) {
$methods = [];
}
$mockedMethods = '';
$configurable = [];
foreach ($methods as $methodName) {
if ($methodName != '__construct' && $methodName != '__clone') {
$configurable[] = strtolower($methodName);
}
}
if (isset($class)) {
// https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
if ($isInterface && $class->implementsInterface(Traversable::class) &&
!$class->implementsInterface(Iterator::class) &&
!$class->implementsInterface(IteratorAggregate::class)) {
$additionalInterfaces[] = Iterator::class;
$methods = array_merge($methods, $this->getClassMethods(Iterator::class));
}
foreach ($methods as $methodName) {
try {
$method = $class->getMethod($methodName);
if ($this->canMockMethod($method)) {
$mockedMethods .= $this->generateMockedMethodDefinitionFromExisting(
$method,
$cloneArguments,
$callOriginalMethods
);
}
} catch (ReflectionException $e) {
$mockedMethods .= $this->generateMockedMethodDefinition(
$mockClassName['fullClassName'],
$methodName,
$cloneArguments
);
}
}
} elseif ($isMultipleInterfaces) {
foreach ($methods as $methodName) {
if ($this->canMockMethod($methodReflections[$methodName])) {
$mockedMethods .= $this->generateMockedMethodDefinitionFromExisting(
$methodReflections[$methodName],
$cloneArguments,
$callOriginalMethods
);
}
}
} else {
foreach ($methods as $methodName) {
$mockedMethods .= $this->generateMockedMethodDefinition(
$mockClassName['fullClassName'],
$methodName,
$cloneArguments
);
}
}
$method = '';
if (!in_array('method', $methods) && (!isset($class) || !$class->hasMethod('method'))) {
$methodTemplate = $this->getTemplate('mocked_class_method.tpl');
$method = $methodTemplate->render();
}
$classTemplate->setVar(
[
'prologue' => isset($prologue) ? $prologue : '',
'epilogue' => isset($epilogue) ? $epilogue : '',
'class_declaration' => $this->generateMockClassDeclaration(
$mockClassName,
$isInterface,
$additionalInterfaces
),
'clone' => $cloneTemplate,
'mock_class_name' => $mockClassName['className'],
'mocked_methods' => $mockedMethods,
'method' => $method,
'configurable' => '[' . implode(', ', array_map(function ($m) {
return '\'' . $m . '\'';
}, $configurable)) . ']'
]
);
return [
'code' => $classTemplate->render(),
'mockClassName' => $mockClassName['className']
];
}
/**
* @param array|string $type
* @param string $className
* @param string $prefix
*
* @return array
*/
private function generateClassName($type, $className, $prefix)
{
if (is_array($type)) {
$type = implode('_', $type);
}
if ($type[0] == '\\') {
$type = substr($type, 1);
}
$classNameParts = explode('\\', $type);
if (count($classNameParts) > 1) {
$type = array_pop($classNameParts);
$namespaceName = implode('\\', $classNameParts);
$fullClassName = $namespaceName . '\\' . $type;
} else {
$namespaceName = '';
$fullClassName = $type;
}
if ($className == '') {
do {
$className = $prefix . $type . '_' .
substr(md5(mt_rand()), 0, 8);
} while (class_exists($className, false));
}
return [
'className' => $className,
'originalClassName' => $type,
'fullClassName' => $fullClassName,
'namespaceName' => $namespaceName
];
}
/**
* @param array $mockClassName
* @param bool $isInterface
* @param array $additionalInterfaces
*
* @return string
*/
private function generateMockClassDeclaration(array $mockClassName, $isInterface, array $additionalInterfaces = [])
{
$buffer = 'class ';
$additionalInterfaces[] = 'PHPUnit_Framework_MockObject_MockObject';
$interfaces = implode(', ', $additionalInterfaces);
if ($isInterface) {
$buffer .= sprintf(
'%s implements %s',
$mockClassName['className'],
$interfaces
);
if (!in_array($mockClassName['originalClassName'], $additionalInterfaces)) {
$buffer .= ', ';
if (!empty($mockClassName['namespaceName'])) {
$buffer .= $mockClassName['namespaceName'] . '\\';
}
$buffer .= $mockClassName['originalClassName'];
}
} else {
$buffer .= sprintf(
'%s extends %s%s implements %s',
$mockClassName['className'],
!empty($mockClassName['namespaceName']) ? $mockClassName['namespaceName'] . '\\' : '',
$mockClassName['originalClassName'],
$interfaces
);
}
return $buffer;
}
/**
* @param ReflectionMethod $method
* @param bool $cloneArguments
* @param bool $callOriginalMethods
*
* @return string
*/
private function generateMockedMethodDefinitionFromExisting(ReflectionMethod $method, $cloneArguments, $callOriginalMethods)
{
if ($method->isPrivate()) {
$modifier = 'private';
} elseif ($method->isProtected()) {
$modifier = 'protected';
} else {
$modifier = 'public';
}
if ($method->isStatic()) {
$modifier .= ' static';
}
if ($method->returnsReference()) {
$reference = '&';
} else {
$reference = '';
}
if ($method->hasReturnType()) {
$returnType = (string) $method->getReturnType();
} else {
$returnType = '';
}
if (preg_match('#\*[ \t]*+@deprecated[ \t]*+(.*?)\r?+\n[ \t]*+\*(?:[ \t]*+@|/$)#s', $method->getDocComment(), $deprecation)) {
$deprecation = trim(preg_replace('#[ \t]*\r?\n[ \t]*+\*[ \t]*+#', ' ', $deprecation[1]));
} else {
$deprecation = false;
}
return $this->generateMockedMethodDefinition(
$method->getDeclaringClass()->getName(),
$method->getName(),
$cloneArguments,
$modifier,
$this->getMethodParameters($method),
$this->getMethodParameters($method, true),
$returnType,
$reference,
$callOriginalMethods,
$method->isStatic(),
$deprecation,
$method->hasReturnType() && $method->getReturnType()->allowsNull()
);
}
/**
* @param string $className
* @param string $methodName
* @param bool $cloneArguments
* @param string $modifier
* @param string $argumentsForDeclaration
* @param string $argumentsForCall
* @param string $returnType
* @param string $reference
* @param bool $callOriginalMethods
* @param bool $static
* @param bool|string $deprecation
* @param bool $allowsReturnNull
*
* @return string
*/
private function generateMockedMethodDefinition($className, $methodName, $cloneArguments = true, $modifier = 'public', $argumentsForDeclaration = '', $argumentsForCall = '', $returnType = '', $reference = '', $callOriginalMethods = false, $static = false, $deprecation = false, $allowsReturnNull = false)
{
if ($static) {
$templateFile = 'mocked_static_method.tpl';
} else {
if ($returnType === 'void') {
$templateFile = sprintf(
'%s_method_void.tpl',
$callOriginalMethods ? 'proxied' : 'mocked'
);
} else {
$templateFile = sprintf(
'%s_method.tpl',
$callOriginalMethods ? 'proxied' : 'mocked'
);
}
}
// Mocked interfaces returning 'self' must explicitly declare the
// interface name as the return type. See
// https://bugs.php.net/bug.php?id=70722
if ($returnType === 'self') {
$returnType = $className;
}
if (false !== $deprecation) {
$deprecation = "The $className::$methodName method is deprecated ($deprecation).";
$deprecationTemplate = $this->getTemplate('deprecation.tpl');
$deprecationTemplate->setVar(
[
'deprecation' => var_export($deprecation, true),
]
);
$deprecation = $deprecationTemplate->render();
}
$template = $this->getTemplate($templateFile);
$template->setVar(
[
'arguments_decl' => $argumentsForDeclaration,
'arguments_call' => $argumentsForCall,
'return_delim' => $returnType ? ': ' : '',
'return_type' => $allowsReturnNull ? '?' . $returnType : $returnType,
'arguments_count' => !empty($argumentsForCall) ? count(explode(',', $argumentsForCall)) : 0,
'class_name' => $className,
'method_name' => $methodName,
'modifier' => $modifier,
'reference' => $reference,
'clone_arguments' => $cloneArguments ? 'true' : 'false',
'deprecation' => $deprecation
]
);
return $template->render();
}
/**
* @param ReflectionMethod $method
*
* @return bool
*/
private function canMockMethod(ReflectionMethod $method)
{
if ($method->isConstructor() ||
$method->isFinal() ||
$method->isPrivate() ||
$this->isMethodNameBlacklisted($method->getName())) {
return false;
}
return true;
}
/**
* Returns whether a method name is blacklisted
*
* @param string $name
*
* @return bool
*/
private function isMethodNameBlacklisted($name)
{
return isset($this->blacklistedMethodNames[$name]);
}
/**
* Returns the parameters of a function or method.
*
* @param ReflectionMethod $method
* @param bool $forCall
*
* @return string
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
private function getMethodParameters(ReflectionMethod $method, $forCall = false)
{
$parameters = [];
foreach ($method->getParameters() as $i => $parameter) {
$name = '$' . $parameter->getName();
/* Note: PHP extensions may use empty names for reference arguments
* or "..." for methods taking a variable number of arguments.
*/
if ($name === '$' || $name === '$...') {
$name = '$arg' . $i;
}
if ($parameter->isVariadic()) {
if ($forCall) {
continue;
}
$name = '...' . $name;
}
$nullable = '';
$default = '';
$reference = '';
$typeDeclaration = '';
if (!$forCall) {
if ($parameter->hasType() && (string) $parameter->getType() !== 'self') {
if (version_compare(PHP_VERSION, '7.1', '>=') && $parameter->allowsNull()) {
$nullable = '?';
}
$typeDeclaration = (string) $parameter->getType() . ' ';
} elseif ($parameter->isArray()) {
$typeDeclaration = 'array ';
} elseif ($parameter->isCallable()) {
$typeDeclaration = 'callable ';
} else {
try {
$class = $parameter->getClass();
} catch (ReflectionException $e) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot mock %s::%s() because a class or ' .
'interface used in the signature is not loaded',
$method->getDeclaringClass()->getName(),
$method->getName()
),
0,
$e
);
}
if ($class !== null) {
$typeDeclaration = $class->getName() . ' ';
}
}
if (!$parameter->isVariadic()) {
if ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
$default = ' = ' . var_export($value, true);
} elseif ($parameter->isOptional()) {
$default = ' = null';
}
}
}
if ($parameter->isPassedByReference()) {
$reference = '&';
}
$parameters[] = $nullable . $typeDeclaration . $reference . $name . $default;
}
return implode(', ', $parameters);
}
/**
* @param string $className
*
* @return array
*/
public function getClassMethods($className)
{
$class = new ReflectionClass($className);
$methods = [];
foreach ($class->getMethods() as $method) {
if ($method->isPublic() || $method->isAbstract()) {
$methods[] = $method->getName();
}
}
return $methods;
}
/**
* @param string $template
*
* @return Text_Template
*/
private function getTemplate($template)
{
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'Generator' . DIRECTORY_SEPARATOR . $template;
if (!isset(self::$templates[$filename])) {
self::$templates[$filename] = new Text_Template($filename);
}
return self::$templates[$filename];
}
}
PK LKe7 " src/Exception/RuntimeException.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class PHPUnit_Framework_MockObject_RuntimeException extends RuntimeException implements PHPUnit_Framework_MockObject_Exception
{
}
PK LK ( src/Exception/BadMethodCallException.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class PHPUnit_Framework_MockObject_BadMethodCallException extends BadMethodCallException implements PHPUnit_Framework_MockObject_Exception
{
}
PK LKs s src/Exception/Exception.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for exceptions used by PHPUnit_MockObject.
*/
interface PHPUnit_Framework_MockObject_Exception
{
}
PK LKo쾤 src/Invocation/Static.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\SelfDescribing;
use SebastianBergmann\Exporter\Exporter;
/**
* Represents a static invocation.
*/
class PHPUnit_Framework_MockObject_Invocation_Static implements PHPUnit_Framework_MockObject_Invocation, SelfDescribing
{
/**
* @var array
*/
protected static $uncloneableExtensions = [
'mysqli' => true,
'SQLite' => true,
'sqlite3' => true,
'tidy' => true,
'xmlwriter' => true,
'xsl' => true
];
/**
* @var array
*/
protected static $uncloneableClasses = [
'Closure',
'COMPersistHelper',
'IteratorIterator',
'RecursiveIteratorIterator',
'SplFileObject',
'PDORow',
'ZipArchive'
];
/**
* @var string
*/
public $className;
/**
* @var string
*/
public $methodName;
/**
* @var array
*/
public $parameters;
/**
* @var string
*/
public $returnType;
/**
* @var bool
*/
public $returnTypeNullable = false;
/**
* @param string $className
* @param string $methodName
* @param array $parameters
* @param string $returnType
* @param bool $cloneObjects
*/
public function __construct($className, $methodName, array $parameters, $returnType, $cloneObjects = false)
{
$this->className = $className;
$this->methodName = $methodName;
$this->parameters = $parameters;
if (strpos($returnType, '?') === 0) {
$returnType = substr($returnType, 1);
$this->returnTypeNullable = true;
}
$this->returnType = $returnType;
if (!$cloneObjects) {
return;
}
foreach ($this->parameters as $key => $value) {
if (is_object($value)) {
$this->parameters[$key] = $this->cloneObject($value);
}
}
}
/**
* @return string
*/
public function toString()
{
$exporter = new Exporter;
return sprintf(
'%s::%s(%s)%s',
$this->className,
$this->methodName,
implode(
', ',
array_map(
[$exporter, 'shortenedExport'],
$this->parameters
)
),
$this->returnType ? sprintf(': %s', $this->returnType) : ''
);
}
/**
* @return mixed Mocked return value.
*/
public function generateReturnValue()
{
switch ($this->returnType) {
case '': return;
case 'string': return $this->returnTypeNullable ? null : '';
case 'float': return $this->returnTypeNullable ? null : 0.0;
case 'int': return $this->returnTypeNullable ? null : 0;
case 'bool': return $this->returnTypeNullable ? null : false;
case 'array': return $this->returnTypeNullable ? null : [];
case 'void': return;
case 'callable':
case 'Closure':
return function () {
};
case 'Traversable':
case 'Generator':
$generator = function () {
yield;
};
return $generator();
default:
if ($this->returnTypeNullable) {
return;
}
$generator = new PHPUnit_Framework_MockObject_Generator;
return $generator->getMock($this->returnType, [], [], '', false);
}
}
/**
* @param object $original
*
* @return object
*/
protected function cloneObject($original)
{
$cloneable = null;
$object = new ReflectionObject($original);
// Check the blacklist before asking PHP reflection to work around
// https://bugs.php.net/bug.php?id=53967
if ($object->isInternal() &&
isset(self::$uncloneableExtensions[$object->getExtensionName()])) {
$cloneable = false;
}
if ($cloneable === null) {
foreach (self::$uncloneableClasses as $class) {
if ($original instanceof $class) {
$cloneable = false;
break;
}
}
}
if ($cloneable === null && method_exists($object, 'isCloneable')) {
$cloneable = $object->isCloneable();
}
if ($cloneable === null && $object->hasMethod('__clone')) {
$method = $object->getMethod('__clone');
$cloneable = $method->isPublic();
}
if ($cloneable === null) {
$cloneable = true;
}
if ($cloneable) {
try {
return clone $original;
} catch (Exception $e) {
return $original;
}
} else {
return $original;
}
}
}
PK LKeH src/Invocation/Object.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents a non-static invocation.
*/
class PHPUnit_Framework_MockObject_Invocation_Object extends PHPUnit_Framework_MockObject_Invocation_Static
{
/**
* @var object
*/
public $object;
/**
* @param string $className
* @param string $methodName
* @param array $parameters
* @param string $returnType
* @param object $object
* @param bool $cloneObjects
*/
public function __construct($className, $methodName, array $parameters, $returnType, $object, $cloneObjects = false)
{
parent::__construct($className, $methodName, $parameters, $returnType, $cloneObjects);
$this->object = $object;
}
}
PK LK src/Invokable.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for classes which can be invoked.
*
* The invocation will be taken from a mock object and passed to an object
* of this class.
*/
interface PHPUnit_Framework_MockObject_Invokable extends PHPUnit_Framework_MockObject_Verifiable
{
/**
* Invokes the invocation object $invocation so that it can be checked for
* expectations or matched against stubs.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation The invocation object passed from mock object
*
* @return object
*/
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation);
/**
* Checks if the invocation matches.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation The invocation object passed from mock object
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation);
}
PK LK7_Xh h src/InvocationMocker.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Mocker for invocations which are sent from
* PHPUnit_Framework_MockObject_MockObject objects.
*
* Keeps track of all expectations and stubs as well as registering
* identifications for builders.
*/
class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
{
/**
* @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
*/
protected $matchers = [];
/**
* @var PHPUnit_Framework_MockObject_Builder_Match[]
*/
protected $builderMap = [];
/**
* @var string[]
*/
private $configurableMethods = [];
/**
* @param array $configurableMethods
*/
public function __construct(array $configurableMethods)
{
$this->configurableMethods = $configurableMethods;
}
/**
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
*/
public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
$this->matchers[] = $matcher;
}
public function hasMatchers()
{
foreach ($this->matchers as $matcher) {
if ($matcher->hasMatchers()) {
return true;
}
}
return false;
}
/**
* @param mixed $id
*
* @return bool|null
*/
public function lookupId($id)
{
if (isset($this->builderMap[$id])) {
return $this->builderMap[$id];
}
return;
}
/**
* @param mixed $id
* @param PHPUnit_Framework_MockObject_Builder_Match $builder
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
{
if (isset($this->builderMap[$id])) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Match builder with id <' . $id . '> is already registered.'
);
}
$this->builderMap[$id] = $builder;
}
/**
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
$this,
$matcher,
$this->configurableMethods
);
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return mixed
*
* @throws Exception
*/
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$exception = null;
$hasReturnValue = false;
$returnValue = null;
foreach ($this->matchers as $match) {
try {
if ($match->matches($invocation)) {
$value = $match->invoked($invocation);
if (!$hasReturnValue) {
$returnValue = $value;
$hasReturnValue = true;
}
}
} catch (Exception $e) {
$exception = $e;
}
}
if ($exception !== null) {
throw $exception;
}
if ($hasReturnValue) {
return $returnValue;
} elseif (strtolower($invocation->methodName) == '__tostring') {
return '';
}
return $invocation->generateReturnValue();
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
foreach ($this->matchers as $matcher) {
if (!$matcher->matches($invocation)) {
return false;
}
}
return true;
}
/**
* @return bool
*/
public function verify()
{
foreach ($this->matchers as $matcher) {
$matcher->verify();
}
}
}
PK LKτ src/Verifiable.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\ExpectationFailedException;
/**
* Interface for classes which must verify a given expectation.
*/
interface PHPUnit_Framework_MockObject_Verifiable
{
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws ExpectationFailedException
*/
public function verify();
}
PK LKCm " src/Matcher/InvokedAtLeastOnce.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which checks if a method has been invoked at least one
* time.
*
* If the number of invocations is 0 it will throw an exception in verify.
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @return string
*/
public function toString()
{
return 'invoked at least once';
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count < 1) {
throw new ExpectationFailedException(
'Expected invocation at least once but it never occurred.'
);
}
}
}
PK LK' src/Matcher/MethodName.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Util\InvalidArgumentHelper;
/**
* Invocation matcher which looks for a specific method name in the invocations.
*
* Checks the method name all incoming invocations, the name is checked against
* the defined constraint $constraint. If the constraint is met it will return
* true in matches().
*/
class PHPUnit_Framework_MockObject_Matcher_MethodName extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @var Constraint
*/
protected $constraint;
/**
* @param Constraint|string
*
* @throws Constraint
*/
public function __construct($constraint)
{
if (!$constraint instanceof Constraint) {
if (!is_string($constraint)) {
throw InvalidArgumentHelper::factory(1, 'string');
}
$constraint = new IsEqual(
$constraint,
0,
10,
false,
true
);
}
$this->constraint = $constraint;
}
/**
* @return string
*/
public function toString()
{
return 'method name ' . $this->constraint->toString();
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return $this->constraint->evaluate($invocation->methodName, '', true);
}
}
PK LKB B src/Matcher/Parameters.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsAnything;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which looks for specific parameters in the invocations.
*
* Checks the parameters of all incoming invocations, the parameter list is
* checked against the defined constraints in $parameters. If the constraint
* is met it will return true in matches().
*/
class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @var Constraint[]
*/
protected $parameters = [];
/**
* @var PHPUnit_Framework_MockObject_Invocation
*/
protected $invocation;
/**
* @var ExpectationFailedException
*/
private $parameterVerificationResult;
/**
* @param array $parameters
*/
public function __construct(array $parameters)
{
foreach ($parameters as $parameter) {
if (!($parameter instanceof Constraint)) {
$parameter = new IsEqual(
$parameter
);
}
$this->parameters[] = $parameter;
}
}
/**
* @return string
*/
public function toString()
{
$text = 'with parameter';
foreach ($this->parameters as $index => $parameter) {
if ($index > 0) {
$text .= ' and';
}
$text .= ' ' . $index . ' ' . $parameter->toString();
}
return $text;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->invocation = $invocation;
$this->parameterVerificationResult = null;
try {
$this->parameterVerificationResult = $this->verify();
return $this->parameterVerificationResult;
} catch (ExpectationFailedException $e) {
$this->parameterVerificationResult = $e;
throw $this->parameterVerificationResult;
}
}
/**
* Checks if the invocation $invocation matches the current rules. If it
* does the matcher will get the invoked() method called which should check
* if an expectation is met.
*
* @return bool
*
* @throws ExpectationFailedException
*/
public function verify()
{
if (isset($this->parameterVerificationResult)) {
return $this->guardAgainstDuplicateEvaluationOfParameterConstraints();
}
if ($this->invocation === null) {
throw new ExpectationFailedException(
'Mocked method does not exist.'
);
}
if (count($this->invocation->parameters) < count($this->parameters)) {
$message = 'Parameter count for invocation %s is too low.';
// The user called `->with($this->anything())`, but may have meant
// `->withAnyParameters()`.
//
// @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
if (count($this->parameters) === 1 &&
get_class($this->parameters[0]) === IsAnything::class) {
$message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
}
throw new ExpectationFailedException(
sprintf($message, $this->invocation->toString())
);
}
foreach ($this->parameters as $i => $parameter) {
$parameter->evaluate(
$this->invocation->parameters[$i],
sprintf(
'Parameter %s for invocation %s does not match expected ' .
'value.',
$i,
$this->invocation->toString()
)
);
}
return true;
}
/**
* @return bool
*
* @throws ExpectationFailedException
*/
private function guardAgainstDuplicateEvaluationOfParameterConstraints()
{
if ($this->parameterVerificationResult instanceof Exception) {
throw $this->parameterVerificationResult;
}
return (bool) $this->parameterVerificationResult;
}
}
PK LKVc # src/Matcher/InvokedAtLeastCount.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which checks if a method has been invoked at least
* N times.
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
private $requiredInvocations;
/**
* @param int $requiredInvocations
*/
public function __construct($requiredInvocations)
{
$this->requiredInvocations = $requiredInvocations;
}
/**
* @return string
*/
public function toString()
{
return 'invoked at least ' . $this->requiredInvocations . ' times';
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count < $this->requiredInvocations) {
throw new ExpectationFailedException(
'Expected invocation at least ' . $this->requiredInvocations .
' times but it occurred ' . $count . ' time(s).'
);
}
}
}
PK LKO] ] src/Matcher/InvokedCount.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which checks if a method has been invoked a certain amount
* of times.
* If the number of invocations exceeds the value it will immediately throw an
* exception,
* If the number is less it will later be checked in verify() and also throw an
* exception.
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
protected $expectedCount;
/**
* @param int $expectedCount
*/
public function __construct($expectedCount)
{
$this->expectedCount = $expectedCount;
}
/**
* @return bool
*/
public function isNever()
{
return $this->expectedCount == 0;
}
/**
* @return string
*/
public function toString()
{
return 'invoked ' . $this->expectedCount . ' time(s)';
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @throws ExpectationFailedException
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
parent::invoked($invocation);
$count = $this->getInvocationCount();
if ($count > $this->expectedCount) {
$message = $invocation->toString() . ' ';
switch ($this->expectedCount) {
case 0: {
$message .= 'was not expected to be called.';
}
break;
case 1: {
$message .= 'was not expected to be called more than once.';
}
break;
default: {
$message .= sprintf(
'was not expected to be called more than %d times.',
$this->expectedCount
);
}
}
throw new ExpectationFailedException($message);
}
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count !== $this->expectedCount) {
throw new ExpectationFailedException(
sprintf(
'Method was expected to be called %d times, ' .
'actually called %d times.',
$this->expectedCount,
$count
)
);
}
}
}
PK LK- - # src/Matcher/StatelessInvocation.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which does not care about previous state from earlier
* invocations.
*
* This abstract class can be implemented by matchers which does not care about
* state but only the current run-time value of the invocation itself.
*/
abstract class PHPUnit_Framework_MockObject_Matcher_StatelessInvocation implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* Registers the invocation $invocation in the object as being invoked.
* This will only occur after matches() returns true which means the
* current invocation is the correct one.
*
* The matcher can store information from the invocation which can later
* be checked in verify(), or it can check the values directly and throw
* and exception if an expectation is not met.
*
* If the matcher is a stub it will also have a return value.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return mixed
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
}
/**
* Checks if the invocation $invocation matches the current rules. If it does
* the matcher will get the invoked() method called which should check if an
* expectation is met.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return bool
*/
public function verify()
{
}
}
PK LKr- src/Matcher/InvokedAtIndex.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which checks if a method was invoked at a certain index.
*
* If the expected index number does not match the current invocation index it
* will not match which means it skips all method and parameter matching. Only
* once the index is reached will the method and parameter start matching and
* verifying.
*
* If the index is never reached it will throw an exception in index.
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* @var int
*/
protected $sequenceIndex;
/**
* @var int
*/
protected $currentIndex = -1;
/**
* @param int $sequenceIndex
*/
public function __construct($sequenceIndex)
{
$this->sequenceIndex = $sequenceIndex;
}
/**
* @return string
*/
public function toString()
{
return 'invoked at sequence index ' . $this->sequenceIndex;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->currentIndex++;
return $this->currentIndex == $this->sequenceIndex;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws ExpectationFailedException
*/
public function verify()
{
if ($this->currentIndex < $this->sequenceIndex) {
throw new ExpectationFailedException(
sprintf(
'The expected invocation at index %s was never reached.',
$this->sequenceIndex
)
);
}
}
}
PK LK
` " src/Matcher/InvokedAtMostCount.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which checks if a method has been invoked at least
* N times.
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtMostCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
private $allowedInvocations;
/**
* @param int $allowedInvocations
*/
public function __construct($allowedInvocations)
{
$this->allowedInvocations = $allowedInvocations;
}
/**
* @return string
*/
public function toString()
{
return 'invoked at most ' . $this->allowedInvocations . ' times';
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count > $this->allowedInvocations) {
throw new ExpectationFailedException(
'Expected invocation at most ' . $this->allowedInvocations .
' times but it occurred ' . $count . ' time(s).'
);
}
}
}
PK LKKM src/Matcher/Invocation.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\SelfDescribing;
/**
* Interface for classes which matches an invocation based on its
* method name, argument, order or call count.
*/
interface PHPUnit_Framework_MockObject_Matcher_Invocation extends SelfDescribing, PHPUnit_Framework_MockObject_Verifiable
{
/**
* Registers the invocation $invocation in the object as being invoked.
* This will only occur after matches() returns true which means the
* current invocation is the correct one.
*
* The matcher can store information from the invocation which can later
* be checked in verify(), or it can check the values directly and throw
* and exception if an expectation is not met.
*
* If the matcher is a stub it will also have a return value.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return mixed
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation);
/**
* Checks if the invocation $invocation matches the current rules. If it does
* the matcher will get the invoked() method called which should check if an
* expectation is met.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation);
}
PK LKyxQ& & src/Matcher/InvokedRecorder.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Records invocations and provides convenience methods for checking them later
* on.
* This abstract class can be implemented by matchers which needs to check the
* number of times an invocation has occurred.
*/
abstract class PHPUnit_Framework_MockObject_Matcher_InvokedRecorder implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* @var PHPUnit_Framework_MockObject_Invocation[]
*/
protected $invocations = [];
/**
* @return int
*/
public function getInvocationCount()
{
return count($this->invocations);
}
/**
* @return PHPUnit_Framework_MockObject_Invocation[]
*/
public function getInvocations()
{
return $this->invocations;
}
/**
* @return bool
*/
public function hasBeenInvoked()
{
return count($this->invocations) > 0;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->invocations[] = $invocation;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return true;
}
}
PK LKP6 % src/Matcher/ConsecutiveParameters.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\ExpectationFailedException;
/**
* Invocation matcher which looks for sets of specific parameters in the invocations.
*
* Checks the parameters of the incoming invocations, the parameter list is
* checked against the defined constraints in $parameters. If the constraint
* is met it will return true in matches().
*
* It takes a list of match groups and and increases a call index after each invocation.
* So the first invocation uses the first group of constraints, the second the next and so on.
*/
class PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @var array
*/
private $parameterGroups = [];
/**
* @var array
*/
private $invocations = [];
/**
* @param array $parameterGroups
*/
public function __construct(array $parameterGroups)
{
foreach ($parameterGroups as $index => $parameters) {
foreach ($parameters as $parameter) {
if (!$parameter instanceof Constraint) {
$parameter = new IsEqual($parameter);
}
$this->parameterGroups[$index][] = $parameter;
}
}
}
/**
* @return string
*/
public function toString()
{
$text = 'with consecutive parameters';
return $text;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->invocations[] = $invocation;
$callIndex = count($this->invocations) - 1;
$this->verifyInvocation($invocation, $callIndex);
return false;
}
public function verify()
{
foreach ($this->invocations as $callIndex => $invocation) {
$this->verifyInvocation($invocation, $callIndex);
}
}
/**
* Verify a single invocation
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation
* @param int $callIndex
*
* @throws ExpectationFailedException
*/
private function verifyInvocation(PHPUnit_Framework_MockObject_Invocation $invocation, $callIndex)
{
if (isset($this->parameterGroups[$callIndex])) {
$parameters = $this->parameterGroups[$callIndex];
} else {
// no parameter assertion for this call index
return;
}
if ($invocation === null) {
throw new ExpectationFailedException(
'Mocked method does not exist.'
);
}
if (count($invocation->parameters) < count($parameters)) {
throw new ExpectationFailedException(
sprintf(
'Parameter count for invocation %s is too low.',
$invocation->toString()
)
);
}
foreach ($parameters as $i => $parameter) {
$parameter->evaluate(
$invocation->parameters[$i],
sprintf(
'Parameter %s for invocation #%d %s does not match expected ' .
'value.',
$i,
$callIndex,
$invocation->toString()
)
);
}
}
}
PK LKEk src/Matcher/AnyInvokedCount.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method has been invoked zero or more
* times. This matcher will always match.
*/
class PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @return string
*/
public function toString()
{
return 'invoked zero or more times';
}
public function verify()
{
}
}
PK LK5 src/Matcher/AnyParameters.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which allows any parameters to a method.
*/
class PHPUnit_Framework_MockObject_Matcher_AnyParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @return string
*/
public function toString()
{
return 'with any parameters';
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return true;
}
}
PK LKCw src/Stub.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\SelfDescribing;
/**
* An object that stubs the process of a normal method for a mock object.
*
* The stub object will replace the code for the stubbed method and return a
* specific value instead of the original value.
*/
interface PHPUnit_Framework_MockObject_Stub extends SelfDescribing
{
/**
* Fakes the processing of the invocation $invocation by returning a
* specific value.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation The invocation which was mocked and matched by the current method and argument matchers
*
* @return mixed
*/
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation);
}
PK LK5hi i src/Builder/Identity.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for unique identifiers.
*
* Defines the interface for recording unique identifiers. The identifiers
* can be used to define the invocation order of expectations. The expectation
* is recorded using id() and then defined in order using
* PHPUnit_Framework_MockObject_Builder_Match::after().
*/
interface PHPUnit_Framework_MockObject_Builder_Identity
{
/**
* Sets the identification of the expectation to $id.
*
* @note The identifier is unique per mock object.
*
* @param string $id Unique identification of expectation.
*/
public function id($id);
}
PK LK" src/Builder/Match.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for invocation order matches.
*/
interface PHPUnit_Framework_MockObject_Builder_Match extends PHPUnit_Framework_MockObject_Builder_Stub
{
/**
* Defines the expectation which must occur before the current is valid.
*
* @param string $id The identification of the expectation that should
* occur before this one.
*
* @return PHPUnit_Framework_MockObject_Builder_Stub
*/
public function after($id);
}
PK LKW O O src/Builder/MethodNameMatch.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for matcher of method names.
*/
interface PHPUnit_Framework_MockObject_Builder_MethodNameMatch extends PHPUnit_Framework_MockObject_Builder_ParametersMatch
{
/**
* Adds a new method name match and returns the parameter match object for
* further matching possibilities.
*
* @param PHPUnit_Framework_Constraint $name Constraint for matching method, if a string is passed it will use the PHPUnit_Framework_Constraint_IsEqual
*
* @return PHPUnit_Framework_MockObject_Builder_ParametersMatch
*/
public function method($name);
}
PK LK{o o src/Builder/InvocationMocker.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder for mocked or stubbed invocations.
*
* Provides methods for building expectations without having to resort to
* instantiating the various matchers manually. These methods also form a
* more natural way of reading the expectation. This class should be together
* with the test case PHPUnit_Framework_MockObject_TestCase.
*/
class PHPUnit_Framework_MockObject_Builder_InvocationMocker implements PHPUnit_Framework_MockObject_Builder_MethodNameMatch
{
/**
* @var PHPUnit_Framework_MockObject_Stub_MatcherCollection
*/
protected $collection;
/**
* @var PHPUnit_Framework_MockObject_Matcher
*/
protected $matcher;
/**
* @var string[]
*/
private $configurableMethods = [];
/**
* @param PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
* @param array $configurableMethods
*/
public function __construct(PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection, PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher, array $configurableMethods)
{
$this->collection = $collection;
$this->matcher = new PHPUnit_Framework_MockObject_Matcher(
$invocationMatcher
);
$this->collection->addMatcher($this->matcher);
$this->configurableMethods = $configurableMethods;
}
/**
* @return PHPUnit_Framework_MockObject_Matcher
*/
public function getMatcher()
{
return $this->matcher;
}
/**
* @param mixed $id
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function id($id)
{
$this->collection->registerId($id, $this);
return $this;
}
/**
* @param PHPUnit_Framework_MockObject_Stub $stub
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function will(PHPUnit_Framework_MockObject_Stub $stub)
{
$this->matcher->stub = $stub;
return $this;
}
/**
* @param mixed $value
* @param mixed $nextValues, ...
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturn($value, ...$nextValues)
{
$stub = count($nextValues) === 0 ?
new PHPUnit_Framework_MockObject_Stub_Return($value) :
new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls(
array_merge([$value], $nextValues)
);
return $this->will($stub);
}
/**
* @param mixed $reference
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnReference(&$reference)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnReference($reference);
return $this->will($stub);
}
/**
* @param array $valueMap
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnMap(array $valueMap)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap(
$valueMap
);
return $this->will($stub);
}
/**
* @param mixed $argumentIndex
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnArgument($argumentIndex)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
$argumentIndex
);
return $this->will($stub);
}
/**
* @param callable $callback
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnCallback($callback)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback(
$callback
);
return $this->will($stub);
}
/**
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnSelf()
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf;
return $this->will($stub);
}
/**
* @param mixed $values, ...
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnOnConsecutiveCalls(...$values)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($values);
return $this->will($stub);
}
/**
* @param Exception $exception
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willThrowException(Exception $exception)
{
$stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception);
return $this->will($stub);
}
/**
* @param mixed $id
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function after($id)
{
$this->matcher->afterMatchBuilderId = $id;
return $this;
}
/**
* Validate that a parameters matcher can be defined, throw exceptions otherwise.
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
private function canDefineParameters()
{
if ($this->matcher->methodNameMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Method name matcher is not defined, cannot define parameter ' .
'matcher without one'
);
}
if ($this->matcher->parametersMatcher !== null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Parameter matcher is already defined, cannot redefine'
);
}
}
/**
* @param array ...$arguments
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function with(...$arguments)
{
$this->canDefineParameters();
$this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_Parameters($arguments);
return $this;
}
/**
* @param array ...$arguments
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function withConsecutive(...$arguments)
{
$this->canDefineParameters();
$this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters($arguments);
return $this;
}
/**
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function withAnyParameters()
{
$this->canDefineParameters();
$this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
return $this;
}
/**
* @param PHPUnit_Framework_Constraint|string $constraint
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function method($constraint)
{
if ($this->matcher->methodNameMatcher !== null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Method name matcher is already defined, cannot redefine'
);
}
if (is_string($constraint) && !in_array(strtolower($constraint), $this->configurableMethods)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Trying to configure method "%s" which cannot be configured because it does not exist, has not been specified, is final, or is static',
$constraint
)
);
}
$this->matcher->methodNameMatcher = new PHPUnit_Framework_MockObject_Matcher_MethodName($constraint);
return $this;
}
}
PK LK% % src/Builder/Stub.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for stubs which are actions replacing an invocation.
*/
interface PHPUnit_Framework_MockObject_Builder_Stub extends PHPUnit_Framework_MockObject_Builder_Identity
{
/**
* Stubs the matching method with the stub object $stub. Any invocations of
* the matched method will now be handled by the stub instead.
*
* @param PHPUnit_Framework_MockObject_Stub $stub
*
* @return PHPUnit_Framework_MockObject_Builder_Identity
*/
public function will(PHPUnit_Framework_MockObject_Stub $stub);
}
PK LKR src/Builder/Namespace.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for builders which can register builders with a given identification.
*
* This interface relates to PHPUnit_Framework_MockObject_Builder_Identity.
*/
interface PHPUnit_Framework_MockObject_Builder_Namespace
{
/**
* Looks up the match builder with identification $id and returns it.
*
* @param string $id The identification of the match builder
*
* @return PHPUnit_Framework_MockObject_Builder_Match
*/
public function lookupId($id);
/**
* Registers the match builder $builder with the identification $id. The
* builder can later be looked up using lookupId() to figure out if it
* has been invoked.
*
* @param string $id The identification of the match builder
* @param PHPUnit_Framework_MockObject_Builder_Match $builder The builder which is being registered
*/
public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder);
}
PK LKv src/Builder/ParametersMatch.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for parameter matchers.
*/
interface PHPUnit_Framework_MockObject_Builder_ParametersMatch extends PHPUnit_Framework_MockObject_Builder_Match
{
/**
* Sets the parameters to match for, each parameter to this function will
* be part of match. To perform specific matches or constraints create a
* new PHPUnit_Framework_Constraint and use it for the parameter.
* If the parameter value is not a constraint it will use the
* PHPUnit_Framework_Constraint_IsEqual for the value.
*
* Some examples:
*
* // match first parameter with value 2
* $b->with(2);
* // match first parameter with value 'smock' and second identical to 42
* $b->with('smock', new PHPUnit_Framework_Constraint_IsEqual(42));
*
*
* @return PHPUnit_Framework_MockObject_Builder_ParametersMatch
*/
public function with(...$arguments);
/**
* Sets a matcher which allows any kind of parameters.
*
* Some examples:
*
* // match any number of parameters
* $b->withAnyParameters();
*
*
* @return PHPUnit_Framework_MockObject_Matcher_AnyParameters
*/
public function withAnyParameters();
}
PK LK src/Invocation.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for invocations.
*/
interface PHPUnit_Framework_MockObject_Invocation
{
/**
* @return mixed Mocked return value.
*/
public function generateReturnValue();
}
PK LK4nu u src/Stub/Return.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Stubs a method by returning a user-defined value.
*/
class PHPUnit_Framework_MockObject_Stub_Return implements PHPUnit_Framework_MockObject_Stub
{
protected $value;
public function __construct($value)
{
$this->value = $value;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return $this->value;
}
public function toString()
{
$exporter = new Exporter;
return sprintf(
'return user-specified value %s',
$exporter->export($this->value)
);
}
}
PK LK~{d d src/Stub/ConsecutiveCalls.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Stubs a method by returning a user-defined stack of values.
*/
class PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls implements PHPUnit_Framework_MockObject_Stub
{
protected $stack;
protected $value;
public function __construct($stack)
{
$this->stack = $stack;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->value = array_shift($this->stack);
if ($this->value instanceof PHPUnit_Framework_MockObject_Stub) {
$this->value = $this->value->invoke($invocation);
}
return $this->value;
}
public function toString()
{
$exporter = new Exporter;
return sprintf(
'return user-specified value %s',
$exporter->export($this->value)
);
}
}
PK LKͭ src/Stub/ReturnArgument.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning an argument that was passed to the mocked method.
*/
class PHPUnit_Framework_MockObject_Stub_ReturnArgument extends PHPUnit_Framework_MockObject_Stub_Return
{
protected $argumentIndex;
public function __construct($argumentIndex)
{
$this->argumentIndex = $argumentIndex;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
if (isset($invocation->parameters[$this->argumentIndex])) {
return $invocation->parameters[$this->argumentIndex];
} else {
return;
}
}
public function toString()
{
return sprintf('return argument #%d', $this->argumentIndex);
}
}
PK LK src/Stub/ReturnReference.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning a user-defined reference to a value.
*/
class PHPUnit_Framework_MockObject_Stub_ReturnReference extends PHPUnit_Framework_MockObject_Stub_Return
{
public function __construct(&$value)
{
$this->value = &$value;
}
}
PK LK@M@ src/Stub/MatcherCollection.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning a user-defined value.
*/
interface PHPUnit_Framework_MockObject_Stub_MatcherCollection
{
/**
* Adds a new matcher to the collection which can be used as an expectation
* or a stub.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher Matcher for invocations to mock objects
*/
public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher);
}
PK LK src/Stub/Exception.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Stubs a method by raising a user-defined exception.
*/
class PHPUnit_Framework_MockObject_Stub_Exception implements PHPUnit_Framework_MockObject_Stub
{
protected $exception;
public function __construct($exception)
{
// TODO Replace check with type declaration when support for PHP 5 is dropped
if (!$exception instanceof Throwable && !$exception instanceof Exception) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Exception must be an instance of Throwable (PHP 7) or Exception (PHP 5)'
);
}
$this->exception = $exception;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
throw $this->exception;
}
public function toString()
{
$exporter = new Exporter;
return sprintf(
'raise user-specified exception %s',
$exporter->export($this->exception)
);
}
}
PK LK= src/Stub/ReturnSelf.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning the current object.
*/
class PHPUnit_Framework_MockObject_Stub_ReturnSelf implements PHPUnit_Framework_MockObject_Stub
{
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
if (!$invocation instanceof PHPUnit_Framework_MockObject_Invocation_Object) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'The current object can only be returned when mocking an ' .
'object, not a static class.'
);
}
return $invocation->object;
}
public function toString()
{
return 'return the current object';
}
}
PK LK[0a a src/Stub/ReturnCallback.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class PHPUnit_Framework_MockObject_Stub_ReturnCallback implements PHPUnit_Framework_MockObject_Stub
{
protected $callback;
public function __construct($callback)
{
$this->callback = $callback;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return call_user_func_array($this->callback, $invocation->parameters);
}
public function toString()
{
if (is_array($this->callback)) {
if (is_object($this->callback[0])) {
$class = get_class($this->callback[0]);
$type = '->';
} else {
$class = $this->callback[0];
$type = '::';
}
return sprintf(
'return result of user defined callback %s%s%s() with the ' .
'passed arguments',
$class,
$type,
$this->callback[1]
);
} else {
return 'return result of user defined callback ' . $this->callback .
' with the passed arguments';
}
}
}
PK LKM M src/Stub/ReturnValueMap.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning a value from a map.
*/
class PHPUnit_Framework_MockObject_Stub_ReturnValueMap implements PHPUnit_Framework_MockObject_Stub
{
protected $valueMap;
public function __construct(array $valueMap)
{
$this->valueMap = $valueMap;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$parameterCount = count($invocation->parameters);
foreach ($this->valueMap as $map) {
if (!is_array($map) || $parameterCount != count($map) - 1) {
continue;
}
$return = array_pop($map);
if ($invocation->parameters === $map) {
return $return;
}
}
return;
}
public function toString()
{
return 'return value from a map';
}
}
PK LK4 * src/Generator/mocked_class_method.tpl.distnu W+A
public function method()
{
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
$expects = $this->expects($any);
return call_user_func_array(array($expects, 'method'), func_get_args());
}
PK LK[$~7 7 " src/Generator/trait_class.tpl.distnu W+A {prologue}class {class_name}
{
use {trait_name};
}
PK LK8W}؟ % src/Generator/unmocked_clone.tpl.distnu W+A public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
parent::__clone();
}
PK LKgEm * src/Generator/proxied_method_void.tpl.distnu W+A
{modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type}
{
$arguments = array({arguments_call});
$count = func_num_args();
if ($count > {arguments_count}) {
$_arguments = func_get_args();
for ($i = {arguments_count}; $i < $count; $i++) {
$arguments[] = $_arguments[$i];
}
}
$this->__phpunit_getInvocationMocker()->invoke(
new PHPUnit_Framework_MockObject_Invocation_Object(
'{class_name}', '{method_name}', $arguments, '{return_type}', $this, {clone_arguments}
)
);
call_user_func_array(array($this->__phpunit_originalObject, "{method_name}"), $arguments);
}
PK LKO5s; ; " src/Generator/deprecation.tpl.distnu W+A
@trigger_error({deprecation}, E_USER_DEPRECATED);
PK LKzQ # src/Generator/mocked_class.tpl.distnu W+A {prologue}{class_declaration}
{
private $__phpunit_invocationMocker;
private $__phpunit_originalObject;
private $__phpunit_configurable = {configurable};
{clone}{mocked_methods}
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return $this->__phpunit_getInvocationMocker()->expects($matcher);
}
{method}
public function __phpunit_setOriginalObject($originalObject)
{
$this->__phpunit_originalObject = $originalObject;
}
public function __phpunit_getInvocationMocker()
{
if ($this->__phpunit_invocationMocker === null) {
$this->__phpunit_invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($this->__phpunit_configurable);
}
return $this->__phpunit_invocationMocker;
}
public function __phpunit_hasMatchers()
{
return $this->__phpunit_getInvocationMocker()->hasMatchers();
}
public function __phpunit_verify($unsetInvocationMocker = true)
{
$this->__phpunit_getInvocationMocker()->verify();
if ($unsetInvocationMocker) {
$this->__phpunit_invocationMocker = null;
}
}
}{epilogue}
PK LKw&S