Skip to content

Commit cecc06b

Browse files
authored
Merge pull request #14 from hypervel/feature/testbench
Feat: Implement testbench package
2 parents 7866715 + e976b68 commit cecc06b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+614
-145
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ composer.lock
44
/phpunit.xml
55
.phpunit.result.cache
66
!tests/Foundation/fixtures/hyperf1/composer.lock
7-
tests/Foundation/fixtures/hyperf/runtime
87
tests/Http/fixtures

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"autoload": {
2525
"psr-4": {
2626
"Illuminate\\Events\\": "src/event/illuminate/",
27+
"Workbench\\App\\": "src/testbench/workbench/app/",
2728
"Hypervel\\": "src/core/src/",
2829
"Hypervel\\Auth\\": "src/auth/src/",
2930
"Hypervel\\Broadcasting\\": "src/broadcasting/src/",
@@ -54,7 +55,8 @@
5455
"Hypervel\\Router\\": "src/router/src/",
5556
"Hypervel\\Session\\": "src/session/src/",
5657
"Hypervel\\Support\\": "src/support/src/",
57-
"Hypervel\\Telescope\\": "src/telescope/src/"
58+
"Hypervel\\Telescope\\": "src/telescope/src/",
59+
"Hypervel\\Testbench\\": "src/testbench/src/"
5860
},
5961
"files": [
6062
"src/auth/src/Functions.php",
@@ -152,7 +154,8 @@
152154
"hypervel/router": "self.version",
153155
"hypervel/session": "self.version",
154156
"hypervel/support": "self.version",
155-
"hypervel/telescope": "self.version"
157+
"hypervel/telescope": "self.version",
158+
"hypervel/testbench": "self.version"
156159
},
157160
"suggest": {
158161
"hyperf/redis": "Required to use redis driver. (^3.1).",
@@ -186,7 +189,8 @@
186189
"phpstan/phpstan": "^1.11.5",
187190
"phpunit/phpunit": "10.5.45",
188191
"pusher/pusher-php-server": "^7.2",
189-
"swoole/ide-helper": "~5.1.0"
192+
"swoole/ide-helper": "~5.1.0",
193+
"symfony/yaml": "^7.3"
190194
},
191195
"config": {
192196
"sort-packages": true

src/container/src/ScanConfig.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Container;
6+
7+
use Hyperf\Di\Annotation\ScanConfig as HyperfScanConfig;
8+
use Hypervel\Config\ProviderConfig;
9+
10+
class ScanConfig extends HyperfScanConfig
11+
{
12+
protected static function initConfigByFile(string $configDir): array
13+
{
14+
$config = [];
15+
$configFromProviders = [];
16+
$cacheable = false;
17+
if (class_exists(ProviderConfig::class)) {
18+
$configFromProviders = ProviderConfig::load();
19+
}
20+
21+
$serverDependencies = $configFromProviders['dependencies'] ?? [];
22+
if (file_exists($dependenciesFile = "{$configDir}/dependencies.php")) {
23+
$definitions = include $dependenciesFile;
24+
$serverDependencies = array_replace($serverDependencies, $definitions ?? []);
25+
}
26+
27+
$config = static::allocateConfigValue($configFromProviders['annotations'] ?? [], $config);
28+
29+
// Load the config/annotations.php and merge the config
30+
if (file_exists($annotationsFile = "{$configDir}/annotations.php")) {
31+
$annotations = include $annotationsFile;
32+
$config = static::allocateConfigValue($annotations, $config);
33+
}
34+
35+
// Load the config/app.php and merge the config
36+
if (file_exists($appFile = "{$configDir}/app.php")) {
37+
$configContent = include $appFile;
38+
$environment = $configContent['env'] ?? 'dev';
39+
$cacheable = value($configContent['scan_cacheable'] ?? $environment === 'production');
40+
if (isset($configContent['annotations'])) {
41+
$config = static::allocateConfigValue($configContent['annotations'], $config);
42+
}
43+
}
44+
45+
return [$config, $serverDependencies, $cacheable];
46+
}
47+
48+
protected static function allocateConfigValue(array $content, array $config): array
49+
{
50+
if (! isset($content['scan'])) {
51+
return $config;
52+
}
53+
54+
foreach ($content['scan'] as $key => $value) {
55+
if (! isset($config[$key])) {
56+
$config[$key] = [];
57+
}
58+
if (! is_array($value)) {
59+
$value = [$value];
60+
}
61+
$config[$key] = array_merge($config[$key], $value);
62+
}
63+
64+
return $config;
65+
}
66+
}

src/filesystem/src/Filesystem.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@
88

99
class Filesystem extends HyperfFilesystem
1010
{
11+
/**
12+
* Ensure a directory exists.
13+
*/
14+
public function ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true): void
15+
{
16+
if (! $this->isDirectory($path)) {
17+
$this->makeDirectory($path, $mode, $recursive);
18+
}
19+
}
1120
}

src/foundation/src/ClassLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Hypervel\Foundation;
66

7-
use Hyperf\Di\Annotation\ScanConfig;
87
use Hyperf\Di\Annotation\Scanner as AnnotationScanner;
98
use Hyperf\Di\LazyLoader\LazyLoader;
109
use Hyperf\Di\ScanHandler\PcntlScanHandler;
1110
use Hyperf\Di\ScanHandler\ScanHandlerInterface;
1211
use Hyperf\Support\DotenvManager;
12+
use Hypervel\Container\ScanConfig;
1313
use Hypervel\Support\Composer;
1414

1515
class ClassLoader

src/foundation/src/Providers/FoundationServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ protected function overrideHyperfConfigs(): void
9797
$configs = [
9898
'app_name' => $this->config->get('app.name'),
9999
'app_env' => $this->config->get('app.env'),
100-
'scan_cacheable' => $this->config->get('app.scan_cacheable'),
101100
StdoutLoggerInterface::class . '.log_level' => $this->config->get('app.stdout_log_level'),
102101
'translation.locale' => $this->config->get('app.locale'),
103102
'translation.fallback_locale' => $this->config->get('app.fallback_locale'),

src/foundation/src/Testing/Traits/CanConfigureMigrationCommands.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ trait CanConfigureMigrationCommands
1010
{
1111
/**
1212
* The parameters that should be used when running "migrate:fresh".
13-
*
14-
* @return array
1513
*/
16-
protected function migrateFreshUsing()
14+
protected function migrateFreshUsing(): array
1715
{
1816
$seeder = $this->seeder();
1917
$connection = $this->app
@@ -31,30 +29,24 @@ protected function migrateFreshUsing()
3129

3230
/**
3331
* Determine if views should be dropped when refreshing the database.
34-
*
35-
* @return bool
3632
*/
37-
protected function shouldDropViews()
33+
protected function shouldDropViews(): bool
3834
{
3935
return property_exists($this, 'dropViews') ? $this->dropViews : false;
4036
}
4137

4238
/**
4339
* Determine if the seed task should be run when refreshing the database.
44-
*
45-
* @return bool
4640
*/
47-
protected function shouldSeed()
41+
protected function shouldSeed(): bool
4842
{
4943
return property_exists($this, 'seed') ? $this->seed : false;
5044
}
5145

5246
/**
5347
* Determine the specific seeder class that should be used when refreshing the database.
54-
*
55-
* @return mixed
5648
*/
57-
protected function seeder()
49+
protected function seeder(): mixed
5850
{
5951
return property_exists($this, 'seeder') ? $this->seeder : false;
6052
}

src/testbench/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
workbench/.env
2+
workbench/composer.lock
3+
workbench/runtime

src/testbench/bin/testbench-sync

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$workingPath = getcwd();
5+
6+
require "{$workingPath}/vendor/autoload.php";
7+
8+
$files = new Hypervel\Filesystem\Filesystem();
9+
$files->copy("{$workingPath}/vendor/hypervel/testbench/testbench.yaml", "{$workingPath}/testbench.yaml");
10+
11+
Hypervel\Support\Collection::make([
12+
...$files->allFiles("{$workingPath}/vendor/hypervel/testbench/workbench/app/"),
13+
])->flatten()
14+
->filter(static fn ($file) => is_file($file))
15+
->each(static function ($file) use ($files, $workingPath) {
16+
$filename = $workingPath . Hypervel\Support\Str::after((string) $file, "{$workingPath}/vendor/hypervel/testbench/workbench");
17+
$files->ensureDirectoryExists(Hypervel\Support\Str::before($filename, basename($filename)));
18+
$files->copy($file, $filename);
19+
});
20+
21+
Hypervel\Support\Collection::make([
22+
...$files->allFiles("{$workingPath}/vendor/hypervel/testbench/workbench/config/"),
23+
...$files->allFiles("{$workingPath}/vendor/hypervel/testbench/workbench/database/"),
24+
...$files->allFiles("{$workingPath}/vendor/hypervel/testbench/workbench/lang/"),
25+
...$files->allFiles("{$workingPath}/vendor/hypervel/testbench/workbench/routes/"),
26+
...$files->allFiles("{$workingPath}/vendor/hypervel/testbench/workbench/resources/"),
27+
])->flatten()
28+
->filter(static fn ($file) => is_file($file))
29+
->each(static function ($file) use ($files, $workingPath) {
30+
$filename = $workingPath . Hypervel\Support\Str::after((string) $file, "{$workingPath}/vendor/hypervel/testbench/workbench");
31+
$files->ensureDirectoryExists(Hypervel\Support\Str::before($filename, basename($filename)));
32+
$files->copy($file, $filename);
33+
});

src/testbench/composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "hypervel/testbench",
3+
"description": "The testbench package for Hypervel.",
4+
"license": "MIT",
5+
"keywords": [
6+
"php",
7+
"hyperf",
8+
"testbench",
9+
"swoole",
10+
"hypervel"
11+
],
12+
"support": {
13+
"issues": "https://github.com/hypervel/components/issues",
14+
"source": "https://github.com/hypervel/components"
15+
},
16+
"authors": [
17+
{
18+
"name": "Albert Chen",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"require": {
23+
"php": "^8.2",
24+
"hypervel/framework": "^0.1",
25+
"mockery/mockery": "^1.6.10",
26+
"phpunit/phpunit": "^10.0.7",
27+
"symfony/yaml": "^7.3",
28+
"vlucas/phpdotenv": "^5.6.1"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Hypervel\\Testbench\\": "src/",
33+
"Workbench\\App\\": "workbench/app/"
34+
}
35+
},
36+
"extra": {
37+
"branch-alias": {
38+
"dev-main": "0.1-dev"
39+
}
40+
},
41+
"config": {
42+
"sort-packages": true
43+
},
44+
"bin": [
45+
"bin/testbench-sync"
46+
],
47+
"scripts": {
48+
"sync": "@php bin/testbench-sync"
49+
},
50+
"minimum-stability": "dev"
51+
}

0 commit comments

Comments
 (0)