Skip to content

Commit 391fef3

Browse files
author
Pete Bishop
committed
Reduce complexity, remove Mockery, update test
1 parent 66af0b0 commit 391fef3

File tree

4 files changed

+24
-67
lines changed

4 files changed

+24
-67
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
},
4141
"require-dev": {
4242
"laravel/pint": "^1.0",
43-
"mockery/mockery": "^1.6",
4443
"nunomaduro/collision": "^7.9",
4544
"nunomaduro/larastan": "^2.0.1",
4645
"orchestra/testbench": "^8.18",

src/Traits/HasPreAndPostProcessing.php

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Native\Electron\Traits;
44

5-
use Illuminate\Contracts\Process\ProcessResult;
65
use Illuminate\Support\Collection;
76
use Illuminate\Support\Facades\Process;
87

@@ -15,72 +14,49 @@ trait HasPreAndPostProcessing
1514
{
1615
public function preProcess(): void
1716
{
18-
$config = $this->getPrePostBuildConfig('prebuild');
17+
$config = collect(config('nativephp.prebuild'));
1918

20-
if (! $config instanceof Collection) {
19+
if ($config->isEmpty()) {
2120
return;
2221
}
2322

2423
intro('Running pre-process commands...');
2524

26-
$config->each($this->getProcessCallback());
25+
$this->runProcess($config);
2726

2827
outro('Pre-process commands completed.');
2928
}
3029

3130
public function postProcess(): void
3231
{
33-
$config = $this->getPrePostBuildConfig('postbuild');
32+
$config = collect(config('nativephp.postbuild'));
3433

35-
if (! $config instanceof Collection) {
34+
if ($config->isEmpty()) {
3635
return;
3736
}
3837

3938
intro('Running post-process commands...');
4039

41-
$config->each($this->getProcessCallback());
40+
$this->runProcess($config);
4241

4342
outro('Post-process commands completed.');
4443
}
4544

46-
private function formatConfigKey(string $configKey): string
45+
private function runProcess(Collection $configCommands): void
4746
{
48-
return sprintf('nativephp.%s', $configKey);
49-
}
50-
51-
private function executeCommand(mixed $command): ProcessResult
52-
{
53-
return Process::path(base_path())
54-
->timeout(300)
55-
->tty(\Symfony\Component\Process\Process::isTtySupported())
56-
->run($command, function (string $type, string $output) {
57-
echo $output;
58-
});
59-
}
60-
61-
protected function getPrePostBuildConfig(string $configKey): mixed
62-
{
63-
$config = config($this->formatConfigKey($configKey));
64-
65-
if (is_array($config)) {
66-
// Filter out empty values
67-
return collect($config)
68-
->filter(fn ($value) => ! empty($value));
69-
}
70-
71-
return $config;
72-
}
73-
74-
private function getProcessCallback(): callable
75-
{
76-
return function ($command) {
47+
$configCommands->each(function ($command) {
7748
note("Running command: {$command}");
7849

7950
if (is_array($command)) {
8051
$command = implode(' && ', $command);
8152
}
8253

83-
$result = $this->executeCommand($command);
54+
$result = Process::path(base_path())
55+
->timeout(300)
56+
->tty(\Symfony\Component\Process\Process::isTtySupported())
57+
->run($command, function (string $type, string $output) {
58+
echo $output;
59+
});
8460

8561
if (! $result->successful()) {
8662
error("Command failed: {$command}");
@@ -89,6 +65,6 @@ private function getProcessCallback(): callable
8965
}
9066

9167
note("Command successful: {$command}");
92-
};
68+
});
9369
}
9470
}

tests/Mocks/HasPreAndPostProcessingMock.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/Unit/Traits/HasPreAndPostProcessingTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Native\Electron\Tests\Unit\Traits;
44

55
use Illuminate\Support\Facades\Config;
6-
use Native\Electron\Tests\Mocks\HasPreAndPostProcessingMock;
6+
use Native\Electron\Traits\HasPreAndPostProcessing;
77

8-
it('can run pre and post processing from config', function () {
8+
it('can run pre and post processing from config', function (object $mock) {
99
$tmpDir = sys_get_temp_dir();
1010

1111
Config::set('nativephp.prebuild', [
@@ -18,18 +18,6 @@
1818
'touch '.$tmpDir.'/postbuild2',
1919
]);
2020

21-
$mock = \Mockery::mock(HasPreAndPostProcessingMock::class)
22-
->makePartial();
23-
24-
$mock->shouldAllowMockingProtectedMethods();
25-
$mock->shouldReceive('getPrePostBuildConfig')
26-
->with('prebuild')
27-
->andReturn(collect(Config::get('nativephp.prebuild')));
28-
29-
$mock->shouldReceive('getPrePostBuildConfig')
30-
->with('postbuild')
31-
->andReturn(collect(Config::get('nativephp.postbuild')));
32-
3321
// Verify those files were created in preProcess
3422
$mock->preProcess();
3523

@@ -46,4 +34,10 @@
4634
unlink($tmpDir.'/prebuild2');
4735
unlink($tmpDir.'/postbuild1');
4836
unlink($tmpDir.'/postbuild2');
49-
});
37+
})
38+
->with([
39+
// Empty class with the HasPreAndPostProcessing trait
40+
new class {
41+
use HasPreAndPostProcessing;
42+
}
43+
]);

0 commit comments

Comments
 (0)