/usr/share/php/Mockery.php is in php-mockery 1.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | <?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use Mockery\ExpectationInterface;
use Mockery\Generator\CachingGenerator;
use Mockery\Generator\Generator;
use Mockery\Generator\MockConfigurationBuilder;
use Mockery\Generator\StringManipulationGenerator;
use Mockery\Loader\EvalLoader;
use Mockery\Loader\Loader;
use Mockery\Matcher\MatcherAbstract;
class Mockery
{
const BLOCKS = 'Mockery_Forward_Blocks';
/**
* Global container to hold all mocks for the current unit test running.
*
* @var \Mockery\Container
*/
protected static $_container = null;
/**
* Global configuration handler containing configuration options.
*
* @var \Mockery\Configuration
*/
protected static $_config = null;
/**
* @var \Mockery\Generator\Generator
*/
protected static $_generator;
/**
* @var \Mockery\Loader\Loader
*/
protected static $_loader;
/**
* @var array
*/
private static $_filesToCleanUp = [];
/**
* Defines the global helper functions
*
* @return void
*/
public static function globalHelpers()
{
require_once __DIR__.'/helpers.php';
}
/**
* Static shortcut to \Mockery\Container::mock().
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function mock(...$args)
{
return call_user_func_array(array(self::getContainer(), 'mock'), $args);
}
/**
* Static and semantic shortcut for getting a mock from the container
* and applying the spy's expected behavior into it.
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function spy(...$args)
{
return call_user_func_array(array(self::getContainer(), 'mock'), $args)->shouldIgnoreMissing();
}
/**
* Static and Semantic shortcut to \Mockery\Container::mock().
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function instanceMock(...$args)
{
return call_user_func_array(array(self::getContainer(), 'mock'), $args);
}
/**
* Static shortcut to \Mockery\Container::mock(), first argument names the mock.
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function namedMock(...$args)
{
$name = array_shift($args);
$builder = new MockConfigurationBuilder();
$builder->setName($name);
array_unshift($args, $builder);
return call_user_func_array(array(self::getContainer(), 'mock'), $args);
}
/**
* Static shortcut to \Mockery\Container::self().
*
* @throws LogicException
*
* @return \Mockery\MockInterface
*/
public static function self()
{
if (is_null(self::$_container)) {
throw new \LogicException('You have not declared any mocks yet');
}
return self::$_container->self();
}
/**
* Static shortcut to closing up and verifying all mocks in the global
* container, and resetting the container static variable to null.
*
* @return void
*/
public static function close()
{
foreach (self::$_filesToCleanUp as $fileName) {
@unlink($fileName);
}
self::$_filesToCleanUp = [];
if (is_null(self::$_container)) {
return;
}
$container = self::$_container;
self::$_container = null;
$container->mockery_teardown();
$container->mockery_close();
}
/**
* Static fetching of a mock associated with a name or explicit class poser.
*
* @param $name
*
* @return \Mockery\Mock
*/
public static function fetchMock($name)
{
return self::$_container->fetchMock($name);
}
/**
* Lazy loader and getter for
* the container property.
*
* @return Mockery\Container
*/
public static function getContainer()
{
if (is_null(self::$_container)) {
self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader());
}
return self::$_container;
}
/**
* Setter for the $_generator static propery.
*
* @param \Mockery\Generator\Generator $generator
*/
public static function setGenerator(Generator $generator)
{
self::$_generator = $generator;
}
/**
* Lazy loader method and getter for
* the generator property.
*
* @return Generator
*/
public static function getGenerator()
{
if (is_null(self::$_generator)) {
self::$_generator = self::getDefaultGenerator();
}
return self::$_generator;
}
/**
* Creates and returns a default generator
* used inside this class.
*
* @return CachingGenerator
*/
public static function getDefaultGenerator()
{
return new CachingGenerator(StringManipulationGenerator::withDefaultPasses());
}
/**
* Setter for the $_loader static property.
*
* @param Loader $loader
*/
public static function setLoader(Loader $loader)
{
self::$_loader = $loader;
}
/**
* Lazy loader method and getter for
* the $_loader property.
*
* @return Loader
*/
public static function getLoader()
{
if (is_null(self::$_loader)) {
self::$_loader = self::getDefaultLoader();
}
return self::$_loader;
}
/**
* Gets an EvalLoader to be used as default.
*
* @return EvalLoader
*/
public static function getDefaultLoader()
{
return new EvalLoader();
}
/**
* Set the container.
*
* @param \Mockery\Container $container
*
* @return \Mockery\Container
*/
public static function setContainer(Mockery\Container $container)
{
return self::$_container = $container;
}
/**
* Reset the container to null.
*
* @return void
*/
public static function resetContainer()
{
self::$_container = null;
}
/**
* Return instance of ANY matcher.
*
* @return \Mockery\Matcher\Any
*/
public static function any()
{
return new \Mockery\Matcher\Any();
}
/**
* Return instance of TYPE matcher.
*
* @param $expected
*
* @return \Mockery\Matcher\Type
*/
public static function type($expected)
{
return new \Mockery\Matcher\Type($expected);
}
/**
* Return instance of DUCKTYPE matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\Ducktype
*/
public static function ducktype(...$args)
{
return new \Mockery\Matcher\Ducktype($args);
}
/**
* Return instance of SUBSET matcher.
*
* @param array $part
* @param bool $strict - (Optional) True for strict comparison, false for loose
*
* @return \Mockery\Matcher\Subset
*/
public static function subset(array $part, $strict = true)
{
return new \Mockery\Matcher\Subset($part, $strict);
}
/**
* Return instance of CONTAINS matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\Contains
*/
public static function contains(...$args)
{
return new \Mockery\Matcher\Contains($args);
}
/**
* Return instance of HASKEY matcher.
*
* @param $key
*
* @return \Mockery\Matcher\HasKey
*/
public static function hasKey($key)
{
return new \Mockery\Matcher\HasKey($key);
}
/**
* Return instance of HASVALUE matcher.
*
* @param $val
*
* @return \Mockery\Matcher\HasValue
*/
public static function hasValue($val)
{
return new \Mockery\Matcher\HasValue($val);
}
/**
* Return instance of CLOSURE matcher.
*
* @param $closure
*
* @return \Mockery\Matcher\Closure
*/
public static function on($closure)
{
return new \Mockery\Matcher\Closure($closure);
}
/**
* Return instance of MUSTBE matcher.
*
* @param $expected
*
* @return \Mockery\Matcher\MustBe
*/
public static function mustBe($expected)
{
return new \Mockery\Matcher\MustBe($expected);
}
/**
* Return instance of NOT matcher.
*
* @param $expected
*
* @return \Mockery\Matcher\Not
*/
public static function not($expected)
{
return new \Mockery\Matcher\Not($expected);
}
/**
* Return instance of ANYOF matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\AnyOf
*/
public static function anyOf(...$args)
{
return new \Mockery\Matcher\AnyOf($args);
}
/**
* Return instance of NOTANYOF matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\NotAnyOf
*/
public static function notAnyOf(...$args)
{
return new \Mockery\Matcher\NotAnyOf($args);
}
/**
* Return instance of PATTERN matcher.
*
* @param $expected
*
* @return \Mockery\Matcher\Pattern
*/
public static function pattern($expected)
{
return new \Mockery\Matcher\Pattern($expected);
}
/**
* Lazy loader and Getter for the global
* configuration container.
*
* @return \Mockery\Configuration
*/
public static function getConfiguration()
{
if (is_null(self::$_config)) {
self::$_config = new \Mockery\Configuration();
}
return self::$_config;
}
/**
* Utility method to format method name and arguments into a string.
*
* @param string $method
* @param array $arguments
*
* @return string
*/
public static function formatArgs($method, array $arguments = null)
{
if (is_null($arguments)) {
return $method . '()';
}
$formattedArguments = array();
foreach ($arguments as $argument) {
$formattedArguments[] = self::formatArgument($argument);
}
return $method . '(' . implode(', ', $formattedArguments) . ')';
}
/**
* Gets the string representation
* of any passed argument.
*
* @param $argument
* @param $depth
*
* @return string
*/
private static function formatArgument($argument, $depth = 0)
{
if ($argument instanceof MatcherAbstract) {
return (string) $argument;
}
if (is_object($argument)) {
return 'object(' . get_class($argument) . ')';
}
if (is_int($argument) || is_float($argument)) {
return $argument;
}
if (is_array($argument)) {
if ($depth === 1) {
$argument = '[...]';
} else {
$sample = array();
foreach ($argument as $key => $value) {
$key = is_int($key) ? $key : "'$key'";
$value = self::formatArgument($value, $depth + 1);
$sample[] = "$key => $value";
}
$argument = "[".implode(", ", $sample)."]";
}
return ((strlen($argument) > 1000) ? substr($argument, 0, 1000).'...]' : $argument);
}
if (is_bool($argument)) {
return $argument ? 'true' : 'false';
}
if (is_resource($argument)) {
return 'resource(...)';
}
if (is_null($argument)) {
return 'NULL';
}
return "'".(string) $argument."'";
}
/**
* Utility function to format objects to printable arrays.
*
* @param array $objects
*
* @return string
*/
public static function formatObjects(array $objects = null)
{
static $formatting;
if ($formatting) {
return '[Recursion]';
}
if (is_null($objects)) {
return '';
}
$objects = array_filter($objects, 'is_object');
if (empty($objects)) {
return '';
}
$formatting = true;
$parts = array();
foreach ($objects as $object) {
$parts[get_class($object)] = self::objectToArray($object);
}
$formatting = false;
return 'Objects: ( ' . var_export($parts, true) . ')';
}
/**
* Utility function to turn public properties and public get* and is* method values into an array.
*
* @param $object
* @param int $nesting
*
* @return array
*/
private static function objectToArray($object, $nesting = 3)
{
if ($nesting == 0) {
return array('...');
}
return array(
'class' => get_class($object),
'properties' => self::extractInstancePublicProperties($object, $nesting)
);
}
/**
* Returns all public instance properties.
*
* @param $object
* @param $nesting
*
* @return array
*/
private static function extractInstancePublicProperties($object, $nesting)
{
$reflection = new \ReflectionClass(get_class($object));
$properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
$cleanedProperties = array();
foreach ($properties as $publicProperty) {
if (!$publicProperty->isStatic()) {
$name = $publicProperty->getName();
$cleanedProperties[$name] = self::cleanupNesting($object->$name, $nesting);
}
}
return $cleanedProperties;
}
/**
* Utility method used for recursively generating
* an object or array representation.
*
* @param $argument
* @param $nesting
*
* @return mixed
*/
private static function cleanupNesting($argument, $nesting)
{
if (is_object($argument)) {
$object = self::objectToArray($argument, $nesting - 1);
$object['class'] = get_class($argument);
return $object;
}
if (is_array($argument)) {
return self::cleanupArray($argument, $nesting - 1);
}
return $argument;
}
/**
* Utility method for recursively
* gerating a representation
* of the given array.
*
* @param array $argument
* @param int $nesting
*
* @return mixed
*/
private static function cleanupArray($argument, $nesting = 3)
{
if ($nesting == 0) {
return '...';
}
foreach ($argument as $key => $value) {
if (is_array($value)) {
$argument[$key] = self::cleanupArray($value, $nesting - 1);
} elseif (is_object($value)) {
$argument[$key] = self::objectToArray($value, $nesting - 1);
}
}
return $argument;
}
/**
* Utility function to parse shouldReceive() arguments and generate
* expectations from such as needed.
*
* @param Mockery\MockInterface $mock
* @param array $args
* @param callable $add
* @return \Mockery\CompositeExpectation
*/
public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add)
{
$composite = new \Mockery\CompositeExpectation();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $k => $v) {
$expectation = self::buildDemeterChain($mock, $k, $add)->andReturn($v);
$composite->add($expectation);
}
} elseif (is_string($arg)) {
$expectation = self::buildDemeterChain($mock, $arg, $add);
$composite->add($expectation);
}
}
return $composite;
}
/**
* Sets up expectations on the members of the CompositeExpectation and
* builds up any demeter chain that was passed to shouldReceive.
*
* @param \Mockery\MockInterface $mock
* @param string $arg
* @param callable $add
* @throws Mockery\Exception
* @return \Mockery\ExpectationInterface
*/
protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add)
{
/** @var Mockery\Container $container */
$container = $mock->mockery_getContainer();
$methodNames = explode('->', $arg);
reset($methodNames);
if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed()
&& !$mock->mockery_isAnonymous()
&& !in_array(current($methodNames), $mock->mockery_getMockableMethods())
) {
throw new \Mockery\Exception(
'Mockery\'s configuration currently forbids mocking the method '
. current($methodNames) . ' as it does not exist on the class or object '
. 'being mocked'
);
}
/** @var ExpectationInterface|null $expectations */
$expectations = null;
/** @var Callable $nextExp */
$nextExp = function ($method) use ($add) {
return $add($method);
};
while (true) {
$method = array_shift($methodNames);
$expectations = $mock->mockery_getExpectationsFor($method);
if (is_null($expectations) || self::noMoreElementsInChain($methodNames)) {
$expectations = $nextExp($method);
if (self::noMoreElementsInChain($methodNames)) {
break;
}
$mock = self::getNewDemeterMock($container, $method, $expectations);
} else {
$demeterMockKey = $container->getKeyOfDemeterMockFor($method);
if ($demeterMockKey) {
$mock = self::getExistingDemeterMock($container, $demeterMockKey);
}
}
$nextExp = function ($n) use ($mock) {
return $mock->shouldReceive($n);
};
}
return $expectations;
}
/**
* Gets a new demeter configured
* mock from the container.
*
* @param \Mockery\Container $container
* @param string $method
* @param Mockery\ExpectationInterface $exp
*
* @return \Mockery\Mock
*/
private static function getNewDemeterMock(
Mockery\Container $container,
$method,
Mockery\ExpectationInterface $exp
) {
$mock = $container->mock('demeter_' . $method);
$exp->andReturn($mock);
return $mock;
}
/**
* Gets an specific demeter mock from
* the ones kept by the container.
*
* @param \Mockery\Container $container
* @param string $demeterMockKey
*
* @return mixed
*/
private static function getExistingDemeterMock(
Mockery\Container $container,
$demeterMockKey
) {
$mocks = $container->getMocks();
$mock = $mocks[$demeterMockKey];
return $mock;
}
/**
* Checks if the passed array representing a demeter
* chain with the method names is empty.
*
* @param array $methodNames
*
* @return bool
*/
private static function noMoreElementsInChain(array $methodNames)
{
return empty($methodNames);
}
public static function declareClass($fqn)
{
return static::declareType($fqn, "class");
}
public static function declareInterface($fqn)
{
return static::declareType($fqn, "interface");
}
private static function declareType($fqn, $type)
{
$targetCode = "<?php ";
$shortName = $fqn;
if (strpos($fqn, "\\")) {
$parts = explode("\\", $fqn);
$shortName = trim(array_pop($parts));
$namespace = implode("\\", $parts);
$targetCode.= "namespace $namespace;\n";
}
$targetCode.= "$type $shortName {} ";
/*
* We could eval here, but it doesn't play well with the way
* PHPUnit tries to backup global state and the require definition
* loader
*/
$tmpfname = tempnam(sys_get_temp_dir(), "Mockery");
file_put_contents($tmpfname, $targetCode);
require $tmpfname;
\Mockery::registerFileForCleanUp($tmpfname);
}
/**
* Register a file to be deleted on tearDown.
*
* @param string $fileName
*/
public static function registerFileForCleanUp($fileName)
{
self::$_filesToCleanUp[] = $fileName;
}
}
|