Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
199 lars 1
<?php namespace Clockwork\Support\Laravel\Tests;
2
 
3
use Clockwork\Helpers\Serializer;
4
use Clockwork\Helpers\StackFilter;
5
use Clockwork\Helpers\StackTrace;
6
 
7
use PHPUnit\Framework\Constraint\Constraint;
8
use PHPUnit\Runner\BaseTestRunner;
9
 
10
// Trait to include in PHPUnit tests to collect executed tests
11
trait UsesClockwork
12
{
13
	// Clockwork phpunit metadata collected while executing tests
14
	protected static $clockwork = [
15
		'asserts' => []
16
	];
17
 
18
	// Set up Clockwork in this test case, should be called from the PHPUnit setUp method
19
	protected function setUpClockwork()
20
	{
21
		if (! $this->app->make('clockwork.support')->isCollectingTests()) return;
22
 
23
		$this->beforeApplicationDestroyed(function () {
24
			if ($this->app->make('clockwork.support')->isTestFiltered($this->toString())) return;
25
 
26
			$this->app->make('clockwork')
27
				->resolveAsTest(
28
					$this->toString(),
29
					$this->resolveClockworkStatus(),
30
					$this->getStatusMessage(),
31
					$this->resolveClockworkAsserts()
32
				)
33
				->storeRequest();
34
		});
35
	}
36
 
37
	// Resolve Clockwork test status
38
	protected function resolveClockworkStatus()
39
	{
40
		$status = $this->getStatus();
41
 
42
		$statuses = [
43
			BaseTestRunner::STATUS_UNKNOWN    => 'unknown',
44
			BaseTestRunner::STATUS_PASSED     => 'passed',
45
			BaseTestRunner::STATUS_SKIPPED    => 'skipped',
46
			BaseTestRunner::STATUS_INCOMPLETE => 'incomplete',
47
			BaseTestRunner::STATUS_FAILURE    => 'failed',
48
			BaseTestRunner::STATUS_ERROR      => 'error',
49
			BaseTestRunner::STATUS_RISKY      => 'passed',
50
			BaseTestRunner::STATUS_WARNING    => 'warning'
51
		];
52
 
53
		return isset($statuses[$status]) ? $statuses[$status] : null;
54
	}
55
 
56
	// Resolve executed asserts
57
	protected function resolveClockworkAsserts()
58
	{
59
		$asserts = static::$clockwork['asserts'];
60
 
61
		if ($this->getStatus() == BaseTestRunner::STATUS_FAILURE && count($asserts)) {
62
			$asserts[count($asserts) - 1]['passed'] = false;
63
		}
64
 
65
		static::$clockwork['asserts'] = [];
66
 
67
		return $asserts;
68
	}
69
 
70
	// Overload the main PHPUnit assert method to collect executed asserts
71
	public static function assertThat($value, Constraint $constraint, string $message = ''): void
72
	{
73
		$trace = StackTrace::get([ 'arguments' => true, 'limit' => 10 ]);
74
 
75
		$assertFrame = $trace->filter(function ($frame) { return strpos($frame->function, 'assert') === 0; })->last();
76
		$trace = $trace->skip(StackFilter::make()->isNotVendor([ 'itsgoingd', 'phpunit' ]))->limit(3);
77
 
78
		static::$clockwork['asserts'][] = [
79
			'name'      => $assertFrame->function,
80
			'arguments' => $assertFrame->args,
81
			'trace'     => (new Serializer)->trace($trace),
82
			'passed'    => true
83
		];
84
 
85
		parent::assertThat($value, $constraint, $message);
86
	}
87
}