Passed
Push — master ( 5fa21f...f08728 )
by Caen
03:34 queued 15s
created

DuskTestCase::setUp()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 46
rs 9.2728
1
<?php
2
3
namespace Hyde\Testing;
4
5
use Facebook\WebDriver\Chrome\ChromeOptions;
6
use Facebook\WebDriver\Remote\DesiredCapabilities;
7
use Facebook\WebDriver\Remote\RemoteWebDriver;
8
use Illuminate\Support\Facades\Facade;
9
use Laravel\Dusk\Browser;
10
use Laravel\Dusk\TestCase as BaseTestCase;
11
use LaravelZero\Framework\Providers\CommandRecorder\CommandRecorderRepository;
12
use NunoMaduro\Collision\ArgumentFormatter;
13
14
abstract class DuskTestCase extends BaseTestCase
15
{
16
    use CreatesApplication;
17
18
    /**
19
     * Setup the test environment.
20
     */
21
    protected function setUp(): void
22
    {
23
        // \LaravelZero\Framework\Testing\TestCase instead \Illuminate\Foundation\Testing\TestCase
24
        if (! $this->app) {
25
            $this->refreshApplication();
26
        }
27
28
        $this->setUpTraits();
29
30
        foreach ($this->afterApplicationCreatedCallbacks as $callback) {
31
            call_user_func($callback);
32
        }
33
34
        Facade::clearResolvedInstances();
35
36
        if (class_exists(\Illuminate\Database\Eloquent\Model::class)) {
37
            \Illuminate\Database\Eloquent\Model::setEventDispatcher($this->app['events']);
38
        }
39
40
        $this->setUpHasRun = true;
41
42
        // \Laravel\Dusk\TestCase
43
44
        Browser::$baseUrl = 'http://localhost:8080';
45
        // Browser::$baseUrl = $this->baseUrl();
46
47
        Browser::$storeScreenshotsAt = base_path('tests/Browser/screenshots');
48
49
        Browser::$storeConsoleLogAt = base_path('tests/Browser/console');
50
51
        Browser::$storeSourceAt = base_path('tests/Browser/source');
52
53
        Browser::$userResolver = function () {
54
            return $this->user();
55
        };
56
57
        Browser::macro('storeSourceAsHtml', function ($name) {
58
            $source = $this->driver->getPageSource();
59
60
            if (! empty($source)) {
61
                file_put_contents(
62
                    sprintf('%s/%s.html', rtrim(static::$storeSourceAt, '/'), $name), $source
63
                );
64
            }
65
66
            return $this;
67
        });
68
    }
69
70
    /**
71
     * Assert that a command was called using the given arguments.
72
     *
73
     * @param  string  $command
74
     * @param  array  $arguments
75
     */
76
    protected function assertCommandCalled(string $command, array $arguments = []): void
77
    {
78
        $argumentsAsString = (new ArgumentFormatter)->format($arguments);
79
        $recorder = app(CommandRecorderRepository::class);
80
81
        static::assertTrue($recorder->exists($command, $arguments),
82
            'Failed asserting that \''.$command.'\' was called with the given arguments: '.$argumentsAsString);
83
    }
84
85
    /**
86
     * Assert that a command was not called using the given arguments.
87
     *
88
     * @param  string  $command
89
     * @param  array  $arguments
90
     */
91
    protected function assertCommandNotCalled(string $command, array $arguments = []): void
92
    {
93
        $argumentsAsString = (new ArgumentFormatter)->format($arguments);
94
        $recorder = app(CommandRecorderRepository::class);
95
96
        static::assertFalse($recorder->exists($command, $arguments),
97
            'Failed asserting that \''.$command.'\' was not called with the given arguments: '.$argumentsAsString);
98
    }
99
100
    /**
101
     * Prepare for Dusk test execution.
102
     *
103
     * @beforeClass
104
     *
105
     * @return void
106
     */
107
    public static function prepare()
108
    {
109
        if (! static::runningInSail()) {
110
            static::startChromeDriver();
111
        }
112
    }
113
114
    /**
115
     * Create the RemoteWebDriver instance.
116
     *
117
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
118
     */
119
    protected function driver()
120
    {
121
        $options = (new ChromeOptions)->addArguments(collect([
122
            $this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
123
        ])->unless($this->hasHeadlessDisabled(), function ($items) {
124
            return $items->merge([
125
                '--disable-gpu',
126
                '--headless',
127
            ]);
128
        })->all());
129
130
        return RemoteWebDriver::create(
131
            $_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
132
            DesiredCapabilities::chrome()->setCapability(
133
                ChromeOptions::CAPABILITY, $options
134
            )
135
        );
136
    }
137
138
    /**
139
     * Determine whether the Dusk command has disabled headless mode.
140
     *
141
     * @return bool
142
     */
143
    protected function hasHeadlessDisabled()
144
    {
145
        return isset($_SERVER['DUSK_HEADLESS_DISABLED']) ||
146
               isset($_ENV['DUSK_HEADLESS_DISABLED']);
147
    }
148
149
    /**
150
     * Determine if the browser window should start maximized.
151
     *
152
     * @return bool
153
     */
154
    protected function shouldStartMaximized()
155
    {
156
        return isset($_SERVER['DUSK_START_MAXIMIZED']) ||
157
               isset($_ENV['DUSK_START_MAXIMIZED']);
158
    }
159
}
160