signed

QiShunwang

“诚信为本、客户至上”

【Laravel3.0.0源码阅读分析】单元测试运行类runner.php

2021/6/9 7:46:04   来源:
<?php namespace Laravel\CLI\Tasks\Test;

use Laravel\File;
use Laravel\Bundle;
use Laravel\Request;
use Laravel\CLI\Tasks\Task;

class Runner extends Task {

	/**
	 * Run all of the unit tests for the application.
	 * 运行应用程序的所有单元测试。
	 * @return void
	 */
	public function run($bundles = array())
	{
		if (count($bundles) == 0) $bundles = array(DEFAULT_BUNDLE);

		$this->bundle($bundles);
	}

	/**
	 * Run the tests for the Laravel framework.
	 * 运行 Laravel 框架的测试。
	 * @return void
	 */
	public function core()
	{
		if ( ! is_dir(path('bundle').'laravel-tests'))
		{
			throw new \Exception("The bundle [laravel-tests] has not been installed!");
		}

		// When testing the Laravel core, we will just stub the path directly
		// so the test bundle is not required to be registered in the bundle
		// configuration, as it is kind of a unique bundle.
        // 在测试 Laravel 核心时,我们将直接存根路径,因此测试包不需要在包配置中注册,因为它是一种独特的包。
		$this->stub(path('bundle').'laravel-tests/cases');

		$path = path('bundle').'laravel-tests/';

		$this->test();
	}

	/**
	 * Run the tests for a given bundle.
	 * 为给定的包运行测试。
	 * @param  array  $bundles
	 * @return void
	 */
	public function bundle($bundles = array())
	{
		if (count($bundles) == 0)
		{
			$bundles = Bundle::names();
		}

		foreach ($bundles as $bundle)
		{
			// To run PHPUnit for the application, bundles, and the framework
			// from one task, we'll dynamically stub PHPUnit.xml files via
			// the task and point the test suite to the correct directory
			// based on what was requested.
            // 要从一项任务中为应用程序、捆绑包和框架运行 PHPUnit,
            // 我们将通过任务动态存根 PHPUnit.xml 文件,并根据请求将测试套件指向正确的目录。
			if (is_dir($path = Bundle::path($bundle).'tests'))
			{
				$this->stub($path);

				$this->test();				
			}
		}
	}

	/**
	 * Run PHPUnit with the temporary XML configuration.
	 * 使用临时 XML 配置运行 PHPUnit。
	 * @return void
	 */
	protected function test()
	{
		// We'll simply fire off PHPUnit with the configuration switch
		// pointing to our temporary configuration file. This allows
		// us to flexibly run tests for any setup.
        // 我们将使用指向临时配置文件的配置开关简单地启动 PHPUnit。 这使我们可以灵活地为任何设置运行测试。
		$path = path('base').'phpunit.xml';

		passthru('phpunit --configuration '.$path);

		@unlink($path);
	}

	/**
	 * Write a stub phpunit.xml file to the base directory.
	 * 将存根 phpunit.xml 文件写入基本目录。
	 * @param  string  $directory
	 * @return void
	 */
	protected function stub($directory)
	{
		$path = path('sys').'cli/tasks/test/';

		$stub = File::get($path.'stub.xml');

		// The PHPUnit bootstrap file contains several items that are swapped
		// at test time. This allows us to point PHPUnit at a few different
		// locations depending on what the developer wants to test.
        // PHPUnit 引导文件包含几个在测试时交换的项目。 这允许我们根据开发人员想要测试的内容将 PHPUnit 指向几个不同的位置。
		foreach (array('bootstrap', 'directory') as $item)
		{
			$stub = $this->{"swap_{$item}"}($stub, $path, $directory);
		}

		File::put(path('base').'phpunit.xml', $stub);
	}

	/**
	 * Swap the bootstrap file in the stub.
	 * 交换存根中的引导程序文件。
	 * @param  string  $stub
	 * @param  string  $path
	 * @param  string  $directory
	 * @return string
	 */
	protected function swap_bootstrap($stub, $path, $directory)
	{
		return str_replace('{{bootstrap}}', $path.'phpunit.php', $stub);
	}

	/**
	 * Swap the directory in the stub.
	 * 交换存根中的目录。
	 * @param  string  $stub
	 * @param  string  $path
	 * @param  string  $directory
	 * @return string
	 */
	protected function swap_directory($stub, $path, $directory)
	{
		return str_replace('{{directory}}', $directory, $stub);
	}

}