Skip to content

Commit e32b8c0

Browse files
committed
Refactor
1 parent 4a27be4 commit e32b8c0

File tree

5 files changed

+73
-62
lines changed

5 files changed

+73
-62
lines changed

src/Commands/SetupCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public function fire()
2727
{
2828
$composer = new Composer(base_path('composer.json'));
2929

30-
$composer->setRepository(static::REPOSITORY_NAME, [
30+
$composer->addRepository(static::REPOSITORY_NAME, [
3131
'type' => 'composer',
3232
'url' => $this->option('url'),
3333
]);
3434

3535
if (starts_with($this->option('url'), 'http://')) {
36-
$composer->setConfig('secure-http', false);
36+
$composer->addConfig('secure-http', false);
3737
}
3838

3939
$composer->save();

src/Composer.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,81 @@ class Composer
1010
protected $location;
1111
protected $filesystem;
1212
protected $items;
13+
protected $changed = false;
1314

14-
public function __construct($location)
15+
public function __construct($location = null)
1516
{
17+
if (is_null($location)) {
18+
$location = base_path('composer.json');
19+
}
20+
1621
$this->filesystem = app(Filesystem::class);
1722
$this->location = $location;
1823

1924
$this->read();
2025
}
2126

22-
public function setRepository($name, $info)
27+
public function addRepository($name, $info)
2328
{
2429
if (!$this->items->has('repositories')) {
2530
$this->items['repositories'] = [];
2631
}
2732

2833
$this->items->set('repositories.'.$name, $info);
2934

35+
$this->changed = true;
36+
3037
return $this;
3138
}
3239

33-
public function setConfig($key, $value)
40+
public function addConfig($key, $value)
3441
{
3542
if (!$this->items->has('config')) {
3643
$this->items['config'] = [];
3744
}
3845

3946
$this->items->set('config.'.$key, $value);
4047

48+
$this->changed = true;
49+
4150
return $this;
4251
}
4352

44-
public function save()
53+
public function set($key, $value)
54+
{
55+
$this->items->set($key, $value);
56+
57+
$this->changed = true;
58+
59+
return $this;
60+
}
61+
62+
public function get($key, $default = null)
4563
{
46-
$this->filesystem->put($this->location, $this->encode($this->items->all()));
64+
return $this->items->get($key, $default);
4765
}
4866

49-
public function remove($key)
67+
public function has($key)
5068
{
51-
$this->items[$key];
69+
return $this->items->has($key);
70+
}
5271

53-
return $this;
72+
public function save()
73+
{
74+
if ($this->changed) {
75+
$this->filesystem->put($this->location, $this->encode($this->items->all()));
76+
77+
$this->changed = false;
78+
}
5479
}
5580

5681
protected function read()
5782
{
5883
$this->items = new Repository($this->decode(
5984
$this->filesystem->get($this->location)
6085
));
86+
87+
$this->changed = false;
6188
}
6289

6390
protected function decode($string)

src/Hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function install($name, $version = null)
224224

225225
public function prepareLocalInstallation($name)
226226
{
227-
$this->composerJson->setRepository($name, [
227+
$this->composerJson->addRepository($name, [
228228
'type' => 'vcs',
229229
'url' => "hooks/{$name}",
230230
]);

tests/HooksTest.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Filesystem\Filesystem;
66
use Larapack\Hooks\Hook;
77
use Larapack\Hooks\Hooks;
8+
use Larapack\Hooks\Composer;
89

910
class HooksTest extends TestCase
1011
{
@@ -23,16 +24,15 @@ public function setUp()
2324
public function test_repository_set()
2425
{
2526
$filesystem = app(Filesystem::class);
27+
$composer = new Composer;
2628

27-
$composer = json_decode($filesystem->get(base_path('composer.json')), true);
28-
29-
$this->assertTrue(isset($composer['repositories']));
29+
$this->assertTrue($composer->has('repositories'));
3030
$this->assertEquals([
3131
'hooks' => [
3232
'url' => static::COMPOSER_REPOSITORY,
3333
'type' => 'composer',
3434
],
35-
], $composer['repositories']);
35+
], $composer->get('repositories'));
3636
}
3737

3838
public function test_install_hook_from_github()
@@ -45,10 +45,10 @@ public function test_install_hook_from_github()
4545
]);
4646

4747
// Check that hooks folder does exists
48-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
48+
$this->assertDirectoryExists(base_path('hooks'));
4949

5050
// Check that the hook folder exists
51-
$this->assertTrue($filesystem->isDirectory(base_path('vendor/composer-github-hook')));
51+
$this->assertDirectoryExists(base_path('vendor/composer-github-hook'));
5252

5353
// Check that the hook details is correct
5454
$hook = app('hooks')->hook('composer-github-hook');
@@ -88,10 +88,10 @@ public function test_making_local_hook()
8888
]);
8989

9090
// Check that hooks folder does exists
91-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
91+
$this->assertDirectoryExists(base_path('hooks'));
9292

9393
// Check that the hook folder exists
94-
$this->assertTrue($filesystem->isDirectory(base_path('hooks/local-test-hook')));
94+
$this->assertDirectoryExists(base_path('hooks/local-test-hook'));
9595

9696
// Check that hook is not yet installed
9797
$this->assertCount(0, app('hooks')->hooks()->all());
@@ -134,10 +134,10 @@ public function test_enabling_hook()
134134
]);
135135

136136
// Check that hooks folder does exists
137-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
137+
$this->assertDirectoryExists(base_path('hooks'));
138138

139139
// Check that the hook folder exists
140-
$this->assertTrue($filesystem->isDirectory(base_path('hooks/local-test-hook')));
140+
$this->assertDirectoryExists(base_path('hooks/local-test-hook'));
141141

142142
// Check that hook is not yet installed
143143
$hooks = app('hooks')->hooks()->all();
@@ -192,10 +192,10 @@ public function test_disabling_hook()
192192
]);
193193

194194
// Check that hooks folder does exists
195-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
195+
$this->assertDirectoryExists(base_path('hooks'));
196196

197197
// Check that the hook folder exists
198-
$this->assertTrue($filesystem->isDirectory(base_path('hooks/local-test-hook')));
198+
$this->assertDirectoryExists(base_path('hooks/local-test-hook'));
199199

200200
// Check that hook is not yet installed
201201
$hooks = app('hooks')->hooks()->all();
@@ -269,10 +269,10 @@ public function test_uninstall_hook()
269269
]);
270270

271271
// Check that hooks folder does exists
272-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
272+
$this->assertDirectoryExists(base_path('hooks'));
273273

274274
// Check that the hook folder exists
275-
$this->assertTrue($filesystem->isDirectory(base_path('hooks/local-test-hook')));
275+
$this->assertDirectoryExists(base_path('hooks/local-test-hook'));
276276

277277
// Check that hook is not yet installed
278278
$hooks = app('hooks')->hooks()->all();
@@ -303,8 +303,8 @@ public function test_uninstall_hook()
303303
'--keep' => true,
304304
]);
305305

306-
// Check that the hook folder still exists
307-
$this->assertTrue($filesystem->isDirectory(base_path('hooks/local-test-hook')));
306+
// Check that the hook folder exists
307+
$this->assertDirectoryExists(base_path('hooks/local-test-hook'));
308308
}
309309

310310
public function test_uninstall_hook_without_keep_parameter()
@@ -317,10 +317,10 @@ public function test_uninstall_hook_without_keep_parameter()
317317
]);
318318

319319
// Check that hooks folder does exists
320-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
320+
$this->assertDirectoryExists(base_path('hooks'));
321321

322322
// Check that the hook folder exists
323-
$this->assertTrue($filesystem->isDirectory(base_path('hooks/local-test-hook')));
323+
$this->assertDirectoryExists(base_path('hooks/local-test-hook'));
324324

325325
// Check that hook is not yet installed
326326
$hooks = app('hooks')->hooks()->all();
@@ -351,7 +351,7 @@ public function test_uninstall_hook_without_keep_parameter()
351351
]);
352352

353353
// Check that the hook no longer folder exists
354-
$this->assertFalse($filesystem->isDirectory(base_path('hooks/local-test-hook')));
354+
$this->assertDirectoryNotExists(base_path('hooks/local-test-hook'));
355355
}
356356

357357
public function test_installing_specific_version()
@@ -364,11 +364,8 @@ public function test_installing_specific_version()
364364
'version' => 'v1.0.0',
365365
]);
366366

367-
// Check that hooks folder does exists
368-
$this->assertTrue($filesystem->isDirectory(base_path('hooks')));
369-
370367
// Check that the hook folder exists
371-
$this->assertTrue($filesystem->isDirectory(base_path('vendor/composer-github-hook')));
368+
$this->assertDirectoryExists(base_path('vendor/composer-github-hook'));
372369

373370
// Check that the hook details is correct
374371
$hook = app('hooks')->hook('composer-github-hook');
@@ -548,24 +545,24 @@ public function test_dependencies_are_downloaded()
548545
$filesystem = app(Filesystem::class);
549546

550547
// Make sure dependency not already exists
551-
$this->assertFalse($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-1')));
548+
$this->assertDirectoryNotExists(base_path('vendor/marktopper/composer-hook-dependency-1'));
552549

553550
// Install hook
554551
$this->artisan('hook:install', [
555552
'name' => 'composer-github-hook',
556553
]);
557554

558555
// Make sure dependency is now downloaded
559-
$this->assertTrue($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-1')));
556+
$this->assertDirectoryExists(base_path('vendor/marktopper/composer-hook-dependency-1'));
560557
}
561558

562559
public function test_updating_updates_dependencies()
563560
{
564561
$filesystem = app(Filesystem::class);
565562

566563
// Make sure dependency not already exists
567-
$this->assertFalse($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-1')));
568-
$this->assertFalse($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-2')));
564+
$this->assertDirectoryNotExists(base_path('vendor/marktopper/composer-hook-dependency-1'));
565+
$this->assertDirectoryNotExists(base_path('vendor/marktopper/composer-hook-dependency-2'));
569566

570567
// Install hook
571568
$this->artisan('hook:install', [
@@ -574,8 +571,8 @@ public function test_updating_updates_dependencies()
574571
]);
575572

576573
// Make sure dependency is now downloaded
577-
$this->assertTrue($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-1')));
578-
$this->assertFalse($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-2')));
574+
$this->assertDirectoryExists(base_path('vendor/marktopper/composer-hook-dependency-1'));
575+
$this->assertDirectoryNotExists(base_path('vendor/marktopper/composer-hook-dependency-2'));
579576

580577
Hooks::useVersionWildcardOnUpdate();
581578

@@ -585,8 +582,8 @@ public function test_updating_updates_dependencies()
585582
]);
586583

587584
// Make sure dependency is now downloaded
588-
$this->assertTrue($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-1')));
589-
$this->assertTrue($filesystem->isDirectory(base_path('vendor/marktopper/composer-hook-dependency-2')));
585+
$this->assertDirectoryExists(base_path('vendor/marktopper/composer-hook-dependency-1'));
586+
$this->assertDirectoryExists(base_path('vendor/marktopper/composer-hook-dependency-2'));
590587
}
591588

592589
// TODO: Test that if a hook requires another hook, that hook should be loaded as well

tests/TestCase.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,14 @@ public function setUp()
3636
$filesystem->makeDirectory(base_path('tests'));
3737
file_put_contents(base_path('tests/TestCase.php'), '<?php ');
3838

39-
// Remove repository section from composer file.
40-
$composer = new Composer(base_path('composer.json'));
41-
$composer->remove('repositories');
42-
$composer->save();
43-
44-
// Remove the minimum stability.
45-
$composer->remove('minimum-stability');
46-
$composer->save();
47-
4839
// Cleanup Composer
49-
$composer = json_decode($filesystem->get(base_path('composer.json')), true);
50-
$composer['require'] = [
51-
'laravel/framework' => $composer['require']['laravel/framework'],
52-
];
53-
if (isset($composer['repositories'])) {
54-
unset($composer['repositories']);
55-
}
56-
if (isset($composer['minimum-stability'])) {
57-
unset($composer['minimum-stability']);
58-
}
59-
$filesystem->put(base_path('composer.json'), json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
40+
$composer = new Composer();
41+
$composer->set('repositories', []);
42+
$composer->set('minimum-stability', 'stable');
43+
$composer->set('require', [
44+
'laravel/framework' => $composer->get('require.laravel/framework')
45+
]);
46+
$composer->save();
6047
$filesystem->delete(base_path('composer.lock'));
6148

6249
// Cleanup vendor

0 commit comments

Comments
 (0)