Skip to content

Commit bd332d3

Browse files
committed
Create the service provider
Added test for command Add ServiceProvider.stub file
1 parent 060c378 commit bd332d3

File tree

6 files changed

+171
-14
lines changed

6 files changed

+171
-14
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "ambersive/laravel-packagemaker",
2+
"name": "ambersive/packagemaker",
33
"description": "This package helps you to create laravel packages.",
44
"license": "MIT",
55
"authors": [
@@ -21,7 +21,8 @@
2121
},
2222
"autoload": {
2323
"psr-4": {
24-
"AMBERSIVE\\PackageMaker\\": "src/"
24+
"AMBERSIVE\\PackageMaker\\": "src/",
25+
"AMBERSIVE\\PackageMaker\\Tests": "test/"
2526
}
2627
},
2728
"extra": {

src/Console/Commands/Dev/MakePackage.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public function handle()
8282
$this->generatePhpUnitFile() == false ? $this->error("[${name}] phpunit.xml file could not be created.") : $this->line("[${name}] phpunit.xml file has been created.");
8383
$this->generateEmptyFolder("tests") === false ? $this->error("[${name}] test folder could not be created.") : $this->line("[${name}] test folder has been created.");
8484
$this->generateEmptyFolder("docs") === false ? $this->error("[${name}] docs folder could not be created.") : $this->line("[${name}] docs folder has been created.");
85+
$this->generateEmptyFolder("src") === false ? $this->error("[${name}] src folder could not be created.") : $this->line("[${name}] src folder has been created.");
86+
$this->generateServiceProvider() === false ? $this->error("[${name}] service provider coult not be created.") : $this->line("[${name}] service provider has been created.");
8587

8688
$this->info("The package ${name} has been created.");
8789

@@ -93,7 +95,12 @@ public function handle()
9395
}
9496

9597
}
96-
98+
99+
/**
100+
* Returns list of possible laravel versions.
101+
*
102+
* @return array
103+
*/
97104
protected function getLaravelVersionLists(): array {
98105

99106
$file = json_decode(File::get(__DIR__."/../../../../composer.json"), true);
@@ -189,18 +196,26 @@ protected function generateComposerFile():bool {
189196
$path = $this->getPath($this->packageName."/composer.json");
190197
$stub = $this->files->get($this->getStubFilePath("composer"));
191198

199+
$splittedName = explode("/", $this->packageName);
200+
$name = ucfirst(Str::camel($splittedName[sizeOf($splittedName) - 1]."ServiceProvider"));
201+
$namespace = $this->getNamespace(ucfirst($splittedName[0])."\\".ucfirst(Str::camel($splittedName[1]))."\\".$name);
202+
192203
$content = $this->replaceTokens($stub, [
193204
"{PACKAGE_NAME}",
194205
"{PACKAGE_DESCRIPTION}",
195206
"{EMAIL}",
196207
"{NAME}",
197-
"{LARAVEL_VERSION}"
208+
"{LARAVEL_VERSION}",
209+
"{NAMESPACE}",
210+
"{PROVIDER}"
198211
], [
199212
$this->packageName,
200213
$this->packageDescription,
201214
$this->packageCreatorEmail,
202215
$this->packageCreatorName,
203-
implode("|", $this->packageLaravelVersions)
216+
implode("|", $this->packageLaravelVersions),
217+
str_replace("\\","\\\\",$namespace),
218+
$name
204219
]);
205220

206221
$this->makeDirectory($path);
@@ -253,6 +268,30 @@ protected function generateEmptyFolder(String $folder): bool {
253268
return $success;
254269

255270
}
271+
272+
/**
273+
* Generate the required service provider
274+
*
275+
* @return bool
276+
*/
277+
protected function generateServiceProvider(): bool {
278+
279+
$splittedName = explode("/", $this->packageName);
280+
$name = ucfirst(Str::camel($splittedName[sizeOf($splittedName) - 1]."ServiceProvider"));
281+
282+
$path = $this->getPath("$this->packageName/src/${name}.php");
283+
284+
$this->files->put($path, $this->sortImports($this->buildClassCustom($name, 'ServiceProvider', [
285+
'DummyNamespace'
286+
], [
287+
$this->getNamespace(ucfirst($splittedName[0])."\\".ucfirst(Str::camel($splittedName[1]))."\\".$name)
288+
])));
289+
290+
$success = File::exists($path);
291+
292+
return $success;
293+
294+
}
256295

257296
/**
258297
* Build the class with the given name.
@@ -262,9 +301,9 @@ protected function generateEmptyFolder(String $folder): bool {
262301
*
263302
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
264303
*/
265-
protected function buildClassCustom(String $name, String $stubname) {
304+
protected function buildClassCustom(String $name, String $stubname, array $placeholder = [], array $replaceWith = []) {
266305
$stub = $this->files->get($this->getStubFilePath($stubname));
267-
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
306+
return $this->replaceNamespaceCustom($stub, $placeholder, $replaceWith)->replaceClass($stub, $name);
268307
}
269308

270309
/**
@@ -276,6 +315,16 @@ protected function getStubFilePath(String $stubname):String {
276315
return $this->getStub()."${stubname}.stub";
277316
}
278317

318+
protected function replaceNamespaceCustom(&$stub, array $placeholder = [], array $replaceWith = []) {
319+
$stub = str_replace(
320+
$placeholder,
321+
$replaceWith,
322+
$stub
323+
);
324+
325+
return $this;
326+
}
327+
279328
protected function replaceTokens(&$stub, array $placeholder = [], array $replaceWith = []) {
280329
$stub = str_replace(
281330
$placeholder,

src/Stubs/README.stub

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ Changes between versions will be tracked in the [CHANGELOG](CHANGELOG.md).
1010
composer require {PACKAGE_NAME}
1111
```
1212

13-
## Usage
14-
15-
16-
This command will create a scaffold for your next laravel project.
17-
1813
## Feedback
1914

2015
Please feel free to give us feedback or any improvement suggestions.

src/Stubs/ServiceProvider.stub

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use App;
6+
use Str;
7+
8+
use Illuminate\Foundation\AliasLoader;
9+
10+
use Illuminate\Support\ServiceProvider;
11+
12+
class DummyClass extends ServiceProvider
13+
{
14+
15+
/**
16+
* Register services.
17+
*
18+
* @return void
19+
*/
20+
public function register()
21+
{
22+
23+
}
24+
25+
/**
26+
* Bootstrap services.
27+
*
28+
* @return void
29+
*/
30+
public function boot()
31+
{
32+
33+
}
34+
35+
36+
}

src/Stubs/composer.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
},
2222
"autoload": {
2323
"psr-4": {
24-
"AMBERSIVE\\KeepAChangelog\\": "src/"
24+
"{NAMESPACE}\\": "src/"
2525
}
2626
},
2727
"extra": {
2828
"component": "package",
2929
"laravel": {
3030
"providers": [
31-
"AMBERSIVE\\KeepAChangelog\\ChangelogServiceProvider"
31+
"{NAMESPACE}\\{PROVIDER}"
3232
]
3333
}
3434
},

tests/Unit/MakeCommandTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace AMBERSIVE\PackageMaker\Tests\Unit;
4+
5+
use Tests\TestCase;
6+
7+
use Config;
8+
use File;
9+
10+
use Illuminate\Foundation\Testing\WithFaker;
11+
use Illuminate\Foundation\Testing\RefreshDatabase;
12+
use Illuminate\Foundation\Testing\DatabaseMigrations;
13+
use Illuminate\Foundation\Testing\DatabaseTransactions;
14+
15+
class MakeCommandTest extends TestCase
16+
{
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
Config::set("package-maker.path", "tmp");
22+
23+
shell_exec("rm -rf ".base_path("tmp"));
24+
mkdir(base_path("tmp"));
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
parent::tearDown();
30+
shell_exec("rm -rf ".base_path("tmp"));
31+
}
32+
33+
/**
34+
* Test if the missing name property will throw an exception
35+
*/
36+
public function testIfMakePackageCommandWillHaveAnException():void {
37+
38+
$this->expectException(\Symfony\Component\Console\Exception\RuntimeException::class);
39+
$this->artisan('make:package')->assertExitCode(1);
40+
41+
}
42+
43+
/**
44+
* Test if the name "demo" will throw an error
45+
*/
46+
public function testIfMakePackageCommandWillNotHaveAnException():void {
47+
48+
$this->artisan('make:package', ['name' => 'demo'])->expectsOutput('Invalid package name. Please make sure you choose a valid package name. eg. ambersive/demo.')->assertExitCode(0);
49+
50+
}
51+
52+
/**
53+
* Test if the package with its content will be created
54+
*/
55+
public function testIfMakePackageCommandWillCreatePackageIfAllParamsAreCorrect():void {
56+
57+
$this->artisan('make:package', ['name' => 'ambersive/demo'])
58+
->expectsQuestion('Whats the purpose of this package?', 'Demo Package')
59+
->expectsQuestion('Whats your name?', 'Manuel')
60+
->expectsQuestion('Whats you e-mail address?', 'manuel.pirker-ihl@ambersive.com')
61+
->expectsQuestion('For which laravel versions do you want to create this plugin.', ["dev-master"])
62+
->assertExitCode(0);
63+
64+
$this->assertTrue(File::exists(base_path("tmp/ambersive/demo/README.md")));
65+
$this->assertTrue(File::exists(base_path("tmp/ambersive/demo/CHANGELOG.md")));
66+
$this->assertTrue(File::exists(base_path("tmp/ambersive/demo/composer.json")));
67+
$this->assertTrue(File::exists(base_path("tmp/ambersive/demo/src/DemoServiceProvider.php")));
68+
69+
$content = File::get(base_path("tmp/ambersive/demo/src/DemoServiceProvider.php"));
70+
71+
$this->assertNotFalse(strpos($content, "class DemoServiceProvider"));
72+
$this->assertNotFalse(strpos($content, "namespace Ambersive\Demo;"));
73+
74+
}
75+
76+
}

0 commit comments

Comments
 (0)