예제 #1
0
 /**
  * Create a mock object of the given class.
  *
  * Return null if users decide not to mock this object.
  *
  * @param       $className
  * @param array $arguments
  *
  * @return null|object
  */
 public function createMockInstance($className, array $arguments)
 {
     // Make sure to update the index if the caller hierarchy changes.
     // #1  Box\TestScribe\Mock\InjectedMockMgr->createMockInstance()
     // #2  Box\TestScribe\App::createMockedInstance()
     // #3  Box\TestScribe\_fixture\ServiceLocator::resolve_internal()
     // #4  Box\TestScribe\_fixture\ServiceLocator::resolve()
     // #5  Box\TestScribe\_fixture\_input\CalculatorViaLocator->calculateWithACalculator()
     $isTheCallFromTheClassBeingTested = $this->callOriginatorChecker->isCallFromTheClassBeingTested(5);
     if (!$isTheCallFromTheClassBeingTested) {
         return null;
     }
     if (array_key_exists($className, $this->injectedMockedObjects)) {
         // @TODO (ryang 1/27/15) : in Box webapp the diesel system will return
         // the same mock object if Diesel::Foo is called multiple times in
         // the same test and a mock for Foo is registered.
         // research if this behavior should be assumed for all
         // service locator systems.
         $msg = "Instantiating class ( {$className} ) which was mocked." . " Return the same mock object.";
         $this->output->writeln($msg);
         /**
          * @var MockClass $mockClass
          */
         $mockClass = $this->injectedMockedObjects[$className];
         return $mockClass->getMockedDynamicClassObj();
     }
     $mockClass = $this->fullMockObjectFactory->createMockObject($className);
     $this->injectedMockedObjects[$className] = $mockClass;
     $mockedDynamicClassObj = $mockClass->getMockedDynamicClassObj();
     return $mockedDynamicClassObj;
 }
 /**
  * The dependency management system used by the method under test should call this method
  * to return a mocked class name when a class mock is requested
  * to be used to invoke a static method.
  *
  * Return null if users decide not to mock this object and have the dependency management
  * system use the real class.
  *
  * @param string $className
  *
  * @return string|null
  */
 public function createMockedClass($className)
 {
     // Make sure to update the index if the caller hierarchy changes.
     // #1  Box\TestScribe\Mock\InjectedMockClassMgr->createMockInstance()
     // #2  Box\TestScribe\App::createMockedClass()
     // #3  Box\TestScribe\_fixture\StaticServiceLocator::resolveInternal()
     // #4  Box\TestScribe\_fixture\StaticServiceLocator::resolve()
     // #5  Box\TestScribe\_fixture\_input\StaticCalculatorViaLocator->calculateWithACalculator()
     $isTheCallFromTheClassBeingTested = $this->callOriginatorChecker->isCallFromTheClassBeingTested(5);
     if (!$isTheCallFromTheClassBeingTested) {
         return null;
     }
     if (array_key_exists($className, self::$injectedMockedClass)) {
         $this->output->writeln("Requesting class ( {$className} ) for static method calls which was mocked." . " Return the same mock object.");
         /**
          * @var MockClass $mock
          */
         $mock = self::$injectedMockedClass[$className];
         return $mock->getMockObjectName();
     }
     $mock = $this->staticMockClassFactory->createAndLoadStaticMockClass($className);
     self::$injectedMockedClass[$className] = $mock;
     return $mock->getMockObjectName();
 }