/** * Configure the engine and start it. * * Compute and register some information such as computed global configuration, * input, output etc to the DI container for easier access later to * lower level components. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * * @return void */ public function configAndStart(InputInterface $input, OutputInterface $output) { $startUpMsg = "\n" . "Starting the test run. Version ( " . Application::APP_VERSION . " )\n" . "Type character h for help when prompted for an input value.\n"; $output->writeln($startUpMsg); $config = $this->configFactory->config($input, $output); $this->container->set('Box\\TestScribe\\Config\\GlobalComputedConfig', $config); App::Init($this->appInstance); $this->engine->start(); }
/** * Execute the method under test and return the execution result. * * @return \Box\TestScribe\Execution\ExecutionResult */ public function run() { // Only inject our own mocks during the execution of the method under test. // This is done to avoid interrupting the bootstrap script. App::$shouldInjectMockObjects = true; $result = $this->executor->runMethod(); $this->resultDisplay->showExecutionResult($result); App::$shouldInjectMockObjects = false; return $result; }
/** * @param string $className * * @return string */ public static function resolveInternal($className) { if (App::$shouldInjectMockObjects) { // It allows the test generator to substitue the real class instance with a // mock object controlled by the test generator so that the test // generator can monitor the creation and execution of the class // being resolved. $mock = App::createMockedClass($className); if ($mock) { return $mock; } // If null is returned by the unit test generator, // it means that the developer has decided not to mock this class. // Proceed with creating the original class name. } return $className; }
/** * Executes the current command. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return null|integer null or 0 if everything went fine, or an error code */ protected function execute(InputInterface $input, OutputInterface $output) { // Initialize dependency injection framework. $builder = new ContainerBuilder(); $container = $builder->build(); $container->set('Symfony\\Component\\Console\\Output\\OutputInterface', $output); // Make sure App class is initialized before calling the bootstrap script. // The presence of this class sends a signal to the code executing later // that this execution is for generating tests. App::setOutput($output); /** * @var EngineStarter $engineStarter */ $engineStarter = $container->get("Box\\TestScribe\\EngineStarter"); $engineStarter->configAndStart($input, $output); return 0; }
/** * @param string $className * @param array $arguments * * @return object */ public static function resolve_internal($className, $arguments = array()) { if (App::$shouldInjectMockObjects) { // It allows the test generator to substitute the real class instance with a // mock object controlled by the test generator so that the test // generator can monitor the creation and execution of the class // being resolved. $mock = App::createMockedInstance($className, $arguments); if ($mock) { return $mock; } // If null is returned by the unit test generator, // it means that the developer has decided not to mock this class. // Proceed with creating the real object. } $singletonOrInstantiator = null; $isOverride = isset(self::$overrides[$className]); if ($isOverride) { return self::$overrides[$className]; } else { return self::createNewInstanceWithArguments($className, $arguments); } }
/** * @param string $docString PHPDocString for a parameter or the return value * @param string $subject description of what the type is for * * @return \Box\TestScribe\PHPDoc\PHPDocType */ private function parsePHPDocString($docString, $subject) { try { $typeInfo = PHPDoc\PHPDocType::lookup($docString); } catch (PHPDocTypeException $e) { $detailedExceptionMsg = $e->getMessage(); $invalidTypeWarningMsg = "Failed to parse PHPDoc type string ( {$docString} ) for the {$subject}.\n" . "Detailed exception message ( {$detailedExceptionMsg} )\n" . "Assume mixed type."; App::writeln($invalidTypeWarningMsg); $typeInfo = new PHPDocMixedType(); } return $typeInfo; }
/** * @return string */ public function getInjectMockedClassMethodName() { $name = App::getInjectMockedClassMethodName(); return $name; }
/** * Checks TestScribe (test generator) to determine whether the real or mock class is required. * If a mock class is needed, it is returned by this method (wrapped in Some), otherwise None is returned. * @param $className * @param $arguments * @return Option */ private static function checkTestScribe($className, $arguments) { /** @noinspection PhpUndefinedNamespaceInspection */ /** @noinspection PhpUndefinedClassInspection */ if (self::isTestGeneratorRun() && \Box\TestScribe\App::$shouldInjectMockObjects) { // This code block is to support PHPUnit test generator. // @see https://github.com/box/TestScribe // The class 'Box\TestScribe\App' should only be loaded // when this method is executed by the test generator. // It allows the test generator to substitute the real class instance with a // mock object controlled by the test generator so that the test // generator can monitor the creation and execution of the class // being resolved. /** @noinspection PhpUndefinedNamespaceInspection */ return Option::fromValue(\Box\TestScribe\App::createMockedInstance($className, $arguments)); } return None::create(); }
<?php /** * */ use Box\TestScribe\_fixture\Directory; require_once __DIR__ . '/../../vendor/autoload.php'; // Instruct the test generator which methods to use to // generate the mock object registration/injection statement. \Box\TestScribe\App::setInjectMockedObjectMethodName('\\Box\\TestScribe\\_fixture\\ServiceLocator::overwrite'); \Box\TestScribe\App::setInjectMockedClassMethodName('\\Box\\TestScribe\\_fixture\\StaticServiceLocator::overwrite'); Directory::init();
/** * Prompt users to select a type from a list of types. * * @param PHPDocType $type * * @return PHPDocType */ public function getSingleTypeFromComposite($type) { $types = $type->getTypes(); $count = count($types); $message = sprintf('Found multiple types ( %s ). Please enter the type desired: ', $type); App::writeln($message); for ($i = 0; $i < $count; $i++) { $message = sprintf("%d %s", $i, $types[$i]->getRepresentation()); App::writeln($message); } $message = sprintf("Enter number 0 - %s representing type intended.", $count - 1); App::writeln($message); $userInputObj = new UserInput(); $input = $userInputObj->getInteger(); return $types[$input]; }