forked from auth0/laravel-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
94 lines (77 loc) · 2.87 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
declare(strict_types=1);
namespace Auth0\Laravel\Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Auth0\Laravel\ServiceProvider;
use Orchestra\Testbench\Concerns\CreatesApplication;
use Spatie\LaravelRay\RayServiceProvider;
class TestCase extends BaseTestCase
{
use CreatesApplication;
protected $enablesPackageDiscoveries = true;
protected $events = [];
protected function getPackageProviders($app)
{
return [
RayServiceProvider::class,
ServiceProvider::class,
];
}
protected function getEnvironmentSetUp($app): void
{
// Set a random key for testing
$_ENV['APP_KEY'] = 'base64:' . base64_encode(random_bytes(32));
// Setup database for testing (currently unused)
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
/**
* Asserts that an event was dispatched. Optionally assert the number of times it was dispatched and/or that it was dispatched after another event.
*
* @param string $expectedEvent The event to assert was dispatched.
* @param int $times The number of times the event was expected to be dispatched.
* @param string|null $followingEvent The event that was expected to be dispatched before this event.
*/
protected function assertDispatched(string $expectedEvent, int $times = 0, ?string $followingEvent = null)
{
expect($this->events)
->toBeArray()
->toContain($expectedEvent);
if ($times > 0) {
expect(array_count_values($this->events)[$expectedEvent])
->toBeInt()
->toBe($times);
}
if (null !== $followingEvent) {
expect($this->events)
->toContain($followingEvent);
$indexExpected = array_search($expectedEvent, $this->events);
$indexFollowing = array_search($followingEvent, $this->events);
if ($indexExpected !== false && $indexFollowing !== false) {
expect($indexExpected)
->toBeInt()
->toBeGreaterThan($indexFollowing);
}
}
}
/**
* Asserts that events were dispatched in the order provided. Events not in the array are ignored.
*
* @param array<string> $events Array of events to assert were dispatched in order.
*/
protected function assertDispatchedOrdered(array $events)
{
$previousIndex = -1;
foreach ($events as $event) {
$index = array_search($event, $this->events);
expect($index)
->toBeInt()
->toBeGreaterThan($previousIndex);
$previousIndex = $index;
}
}
}