Skip to content

Commit 7554807

Browse files
Fixed bug (wrong method handle in service provider)
Added more tests
1 parent 7cc49e9 commit 7554807

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ class MyConsoleApp extends Command
161161
# Light footprint
162162
163163
- **Zero external dependencies** outside of Laravel contracts
164-
- No external dependencies
165-
- No memory leakage (Not yet proven)
164+
- No memory leakage (_needs validation/tests_)
166165
- One time use console logger is attached & detached alongside command execution
167166
- All references destroyed after command termination _(letting PHP Garbage Collection do its thing)_
168167
- Service Provider lazily works only when running in console mode

src/Providers/ConsoleLoggServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public function register()
5555
LogManager::class,
5656
Closure::fromCallable(
5757
[
58-
$this->app->make(LogManagerResolverListener::class),
59-
'resolve'
58+
$this->app->make(LogManagerResolverListenerInterface::class),
59+
'handle'
6060
]
6161
)
6262
);

tests/Doubles/Fakes/vendor/Illuminate/ApplicationFake.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
use ArrayObject;
88
use Closure;
9+
use DevThis\ConsoleLogg\Interfaces\Listener\LogManagerResolverListenerInterface;
10+
use DevThis\ConsoleLogg\Listeners\LogManagerResolverListener;
911
use Illuminate\Contracts\Foundation\Application;
12+
use Tests\Doubles\Spies\Binder\LogOutputBinderFake;
1013

1114
class ApplicationFake extends ArrayObject implements Application
1215
{
16+
private $registered = [];
17+
1318
public function __construct(?array $array = null)
1419
{
1520
parent::__construct($array ?? []);
@@ -114,6 +119,7 @@ public function flush()
114119

115120
public function get($id)
116121
{
122+
return $this->registered[$id] ?? $id;
117123
}
118124

119125
public function getCachedConfigPath()
@@ -170,6 +176,9 @@ public function loadEnvironmentFrom($file)
170176

171177
public function make($abstract, array $parameters = [])
172178
{
179+
if ($abstract === LogManagerResolverListenerInterface::class) {
180+
return new LogManagerResolverListener(new EventDispatcherFake(), new LogOutputBinderFake());
181+
}
173182
}
174183

175184
public function register($provider, $force = false)
@@ -222,6 +231,7 @@ public function shouldSkipMiddleware()
222231

223232
public function singleton($abstract, $concrete = null)
224233
{
234+
$this->registered[$abstract] = $concrete ?? $abstract;
225235
}
226236

227237
public function singletonIf($abstract, $concrete = null)

tests/Unit/Providers/ConsoleLoggServiceProviderTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
namespace Tests\Unit\Providers;
66

7+
use DevThis\ConsoleLogg\Binder\LogOutputBinder;
8+
use DevThis\ConsoleLogg\Factories\FilterableConsoleLoggerFactory;
9+
use DevThis\ConsoleLogg\Interfaces\Binder\LogOutputBindedInterface;
10+
use DevThis\ConsoleLogg\Interfaces\Factories\FilterableConsoleLoggerFactoryInterface;
11+
use DevThis\ConsoleLogg\Interfaces\Listener\LogManagerResolverListenerInterface;
12+
use DevThis\ConsoleLogg\Listeners\LogManagerResolverListener;
713
use DevThis\ConsoleLogg\Providers\ConsoleLoggServiceProvider;
814
use PHPUnit\Framework\TestCase;
915
use ReflectionClass;
@@ -24,7 +30,7 @@ public function testPublishedPath(): void
2430
$app = new ApplicationFake();
2531
// if we don't use reflection to determine file location we would need fuzzy asserting
2632
$reflection = new ReflectionClass(ConsoleLoggServiceProvider::class);
27-
$s = new ConsoleLoggServiceProvider($app);
33+
$serviceProvider = new ConsoleLoggServiceProvider($app);
2834
$expectedPublishedPathsBefore = [];
2935
$actualPublishedPathsBefore = ConsoleLoggServiceProvider::pathsToPublish();
3036
$expectedPublishedPaths = [
@@ -35,10 +41,25 @@ public function testPublishedPath(): void
3541
) => '%APP%/' . 'console-logg.php'
3642
];
3743

38-
$s->boot();
44+
$serviceProvider->boot();
3945
$publishedPaths = ConsoleLoggServiceProvider::pathsToPublish();
4046

4147
self::assertSame($expectedPublishedPaths, $publishedPaths);
4248
self::assertSame($expectedPublishedPathsBefore, $actualPublishedPathsBefore);
4349
}
50+
51+
public function testRegisterBindings(): void
52+
{
53+
$app = new ApplicationFake();
54+
$serviceProvider = new ConsoleLoggServiceProvider($app);
55+
56+
$serviceProvider->register();
57+
58+
self::assertSame(LogOutputBinder::class, $app->get(LogOutputBindedInterface::class));
59+
self::assertSame(LogManagerResolverListener::class, $app->get(LogManagerResolverListenerInterface::class));
60+
self::assertSame(
61+
FilterableConsoleLoggerFactory::class,
62+
$app->get(FilterableConsoleLoggerFactoryInterface::class)
63+
);
64+
}
4465
}

0 commit comments

Comments
 (0)