Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Testing/Concerns/RunsInParallel.php
Строк: 270
<?php

namespace IlluminateTestingConcerns;

use 
IlluminateContractsConsoleKernel;
use 
IlluminateSupportFacadesParallelTesting;
use 
IlluminateTestingParallelConsoleOutput;
use 
RuntimeException;
use 
SymfonyComponentConsoleOutputConsoleOutput;
use 
SymfonyComponentConsoleOutputOutputInterface;

trait 
RunsInParallel
{
    
/**
     * The application resolver callback.
     *
     * @var Closure|null
     */
    
protected static $applicationResolver;

    
/**
     * The runner resolver callback.
     *
     * @var Closure|null
     */
    
protected static $runnerResolver;

    
/**
     * The original test runner options.
     *
     * @var ParaTestRunnersPHPUnitOptions|ParaTestOptions
     */
    
protected $options;

    
/**
     * The output instance.
     *
     * @var SymfonyComponentConsoleOutputOutputInterface
     */
    
protected $output;

    
/**
     * The original test runner.
     *
     * @var ParaTestRunnersPHPUnitRunnerInterface|ParaTestRunnerInterface
     */
    
protected $runner;

    
/**
     * Creates a new test runner instance.
     *
     * @param  ParaTestRunnersPHPUnitOptions|ParaTestOptions  $options
     * @param  SymfonyComponentConsoleOutputOutputInterface  $output
     * @return void
     */
    
public function __construct($optionsOutputInterface $output)
    {
        
$this->options $options;

        if (
$output instanceof ConsoleOutput) {
            
$output = new ParallelConsoleOutput($output);
        }

        
$runnerResolver = static::$runnerResolver ?: function ($optionsOutputInterface $output) {
            
$wrapperRunnerClass class_exists(ParaTestWrapperRunnerWrapperRunner::class)
                ? 
ParaTestWrapperRunnerWrapperRunner::class
                : 
ParaTestRunnersPHPUnitWrapperRunner::class;

            return new 
$wrapperRunnerClass($options$output);
        };

        
$this->runner $runnerResolver($options$output);
    }

    
/**
     * Set the application resolver callback.
     *
     * @param  Closure|null  $resolver
     * @return void
     */
    
public static function resolveApplicationUsing($resolver)
    {
        static::
$applicationResolver $resolver;
    }

    
/**
     * Set the runner resolver callback.
     *
     * @param  Closure|null  $resolver
     * @return void
     */
    
public static function resolveRunnerUsing($resolver)
    {
        static::
$runnerResolver $resolver;
    }

    
/**
     * Runs the test suite.
     *
     * @return int
     */
    
public function execute(): int
    
{
        
$phpHandlerClass class_exists(PHPUnitTextUIConfigurationPhpHandler::class)
            ? 
PHPUnitTextUIConfigurationPhpHandler::class
            : 
PHPUnitTextUIXmlConfigurationPhpHandler::class;

        
$configuration $this->options instanceof ParaTestOptions
            
$this->options->configuration
            
$this->options->configuration();

        (new 
$phpHandlerClass)->handle($configuration->php());

        
$this->forEachProcess(function () {
            
ParallelTesting::callSetUpProcessCallbacks();
        });

        try {
            
$potentialExitCode $this->runner->run();
        } finally {
            
$this->forEachProcess(function () {
                
ParallelTesting::callTearDownProcessCallbacks();
            });
        }

        return 
$potentialExitCode === null
            
$this->getExitCode()
            : 
$potentialExitCode;
    }

    
/**
     * Returns the highest exit code encountered throughout the course of test execution.
     *
     * @return int
     */
    
public function getExitCode(): int
    
{
        return 
$this->runner->getExitCode();
    }

    
/**
     * Apply the given callback for each process.
     *
     * @param  callable  $callback
     * @return void
     */
    
protected function forEachProcess($callback)
    {
        
$processes $this->options instanceof ParaTestOptions
            
$this->options->processes
            
$this->options->processes();

        
collect(range(1$processes))->each(function ($token) use ($callback) {
            
tap($this->createApplication(), function ($app) use ($callback$token) {
                
ParallelTesting::resolveTokenUsing(fn () => $token);

                
$callback($app);
            })->
flush();
        });
    }

    
/**
     * Creates the application.
     *
     * @return IlluminateContractsFoundationApplication
     *
     * @throws RuntimeException
     */
    
protected function createApplication()
    {
        
$applicationResolver = static::$applicationResolver ?: function () {
            if (
trait_exists(TestsCreatesApplication::class)) {
                
$applicationCreator = new class
                {
                    use 
TestsCreatesApplication;
                };

                return 
$applicationCreator->createApplication();
            } elseif (
file_exists($path getcwd().'/bootstrap/app.php') ||
                      
file_exists($path getcwd().'/.laravel/app.php')) {
                
$app = require $path;

                
$app->make(Kernel::class)->bootstrap();

                return 
$app;
            }

            throw new 
RuntimeException('Parallel Runner unable to resolve application.');
        };

        return 
$applicationResolver();
    }
}
Онлайн: 0
Реклама