-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathApplicationTest.php
More file actions
669 lines (518 loc) · 18.4 KB
/
ApplicationTest.php
File metadata and controls
669 lines (518 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
namespace Rareloop\Lumberjack\Test;
use Mockery;
use Mockery\Matcher\Closure;
use PHPUnit\Framework\TestCase;
use Rareloop\Lumberjack\Application;
use Rareloop\Lumberjack\Providers\ServiceProvider;
use Rareloop\Lumberjack\Test\Unit\BrainMonkeyPHPUnitIntegration;
use phpmock\Mock;
use phpmock\MockBuilder;
use Brain\Monkey;
class ApplicationTest extends TestCase
{
use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
protected function setUp(): void
{
Monkey\setUp();
parent::setUp();
}
protected function tearDown(): void
{
Monkey\tearDown();
Mock::disableAll();
parent::tearDown();
}
/** @test */
public function base_path_is_set_in_container_when_basepath_passed_to_constructor()
{
$app = new Application('/base/path');
$this->assertSame('/base/path', $app->basePath());
$this->assertSame('/base/path', $app->get('path.base'));
}
/** @test */
public function config_path_is_set_in_container_when_basepath_passed_to_constructor()
{
$app = new Application('/base/path');
$this->assertSame('/base/path/config', $app->configPath());
$this->assertSame('/base/path/config', $app->get('path.config'));
}
/** @test */
public function can_bind_a_value()
{
$app = new Application;
$app->bind('app.environment', 'production');
$this->assertSame('production', $app->get('app.environment'));
}
/** @test */
public function can_determine_if_something_has_been_bound()
{
$app = new Application;
$this->assertFalse($app->has('app.environment'));
$app->bind('app.environment', 'production');
$this->assertTrue($app->has('app.environment'));
}
/** @test */
public function can_bind_an_object()
{
$app = new Application;
$object = new TestInterfaceImplementation;
$app->bind('test', $object);
$this->assertSame($object, $app->get('test'));
}
/** @test */
public function can_bind_an_object_and_always_get_the_same_instance_back()
{
$app = new Application;
$object = new TestInterfaceImplementation;
$app->bind(TestInterface::class, $object);
$this->assertSame($app->get(TestInterface::class), $app->get(TestInterface::class));
}
/** @test */
public function can_bind_a_concrete_class_to_an_interface()
{
$app = new Application;
$app->bind(TestInterface::class, TestInterfaceImplementation::class);
$object = $app->get(TestInterface::class);
$this->assertNotNull($object);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object);
}
/** @test */
public function can_bind_using_closure()
{
$app = new Application;
$count = 0;
$app->bind(TestInterface::class, function () use (&$count) {
$count++;
return new TestInterfaceImplementation();
});
$object = $app->get(TestInterface::class);
$this->assertSame(1, $count);
$this->assertNotNull($object);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object);
}
/** @test */
public function can_bind_using_closure_and_get_dependencies_injected()
{
$app = new Application;
$count = 0;
$app->bind(TestSubInterface::class, TestSubInterfaceImplementation::class);
$app->bind(TestInterface::class, function (TestSubInterface $foo) use (&$count) {
$this->assertInstanceOf(TestSubInterfaceImplementation::class, $foo);
$count++;
return new TestInterfaceImplementation();
});
$object = $app->get(TestInterface::class);
$this->assertSame(1, $count);
$this->assertNotNull($object);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object);
}
/** @test */
public function can_bind_a_singleton_concrete_class_to_an_interface()
{
$app = new Application;
$app->singleton(TestInterface::class, TestInterfaceImplementation::class);
$object1 = $app->get(TestInterface::class);
$object2 = $app->get(TestInterface::class);
$this->assertSame($object1, $object2);
$this->assertNotNull($object1);
$this->assertNotNull($object2);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object1);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object2);
}
/** @test */
public function can_bind_a_singleton_concrete_class_with_constructor_params_to_an_interface()
{
$app = new Application;
$app->singleton(TestInterface::class, TestInterfaceImplementationWithConstructorParams::class);
$object1 = $app->get(TestInterface::class);
$object2 = $app->get(TestInterface::class);
$this->assertSame($object1, $object2);
$this->assertNotNull($object1);
$this->assertNotNull($object2);
$this->assertInstanceOf(TestInterfaceImplementationWithConstructorParams::class, $object1);
$this->assertInstanceOf(TestInterfaceImplementationWithConstructorParams::class, $object2);
}
/** @test */
public function can_bind_a_singleton_with_closure()
{
$app = new Application;
$count = 0;
$app->singleton(TestInterface::class, function () use (&$count) {
$count++;
return new TestInterfaceImplementation();
});
$object1 = $app->get(TestInterface::class);
$object2 = $app->get(TestInterface::class);
$this->assertSame(1, $count);
$this->assertSame($object1, $object2);
$this->assertNotNull($object1);
$this->assertNotNull($object2);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object1);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object2);
}
/** @test */
public function can_bind_a_singleton_and_get_dependencies_injected()
{
$app = new Application;
$count = 0;
$app->bind(TestSubInterface::class, TestSubInterfaceImplementation::class);
$app->singleton(TestInterface::class, function (TestSubInterface $foo) use (&$count) {
$this->assertInstanceOf(TestSubInterfaceImplementation::class, $foo);
$count++;
return new TestInterfaceImplementation();
});
$object1 = $app->get(TestInterface::class);
$object2 = $app->get(TestInterface::class);
$this->assertSame(1, $count);
$this->assertEquals($object1, $object2);
$this->assertNotNull($object1);
$this->assertNotNull($object2);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object1);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object2);
}
/** @test */
public function app_should_be_bound_into_the_container_on_construction()
{
$app = new Application;
$this->assertSame($app, $app->get(Application::class));
}
/** @test */
public function can_create_a_class_that_has_not_been_registered()
{
$app = new Application;
$app->bind(TestInterface::class, TestInterfaceImplementation::class);
$object = $app->get(NotRegisteredInContainer::class);
$this->assertInstanceOf(NotRegisteredInContainer::class, $object);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object->param);
}
/** @test */
public function can_make_a_class_with_additional_params_for_the_constructor()
{
$app = new Application;
$app->bind(TestInterface::class, TestInterfaceImplementation::class);
$object = $app->make(RequiresAdditionalConstructorParams::class, [
'param1' => 123,
'param2' => 'abc',
]);
$this->assertInstanceOf(RequiresAdditionalConstructorParams::class, $object);
$this->assertInstanceOf(TestInterfaceImplementation::class, $object->param);
$this->assertSame(123, $object->param1);
$this->assertSame('abc', $object->param2);
}
/** @test */
public function make_produces_unique_instances_of_the_bound_object()
{
$app = new Application;
$app->bind(TestInterface::class, TestInterfaceImplementation::class);
$object1 = $app->make(TestInterface::class);
$object2 = $app->make(TestInterface::class);
$this->assertNotSame($object1, $object2);
}
/** @test */
public function using_bind_does_not_produce_a_singleton()
{
$app = new Application;
$app->bind(TestInterface::class, TestInterfaceImplementation::class);
$object1 = $app->get(TestInterface::class);
$object2 = $app->get(TestInterface::class);
$this->assertNotSame($object1, $object2);
}
/** @test */
public function get_does_not_produce_a_singleton_when_the_key_has_not_been_previously_bound_to_the_container()
{
$app = new Application;
$object1 = $app->get(TestInterfaceImplementation::class);
$object2 = $app->get(TestInterfaceImplementation::class);
$this->assertNotSame($object1, $object2);
}
/** @test */
public function using_bind_with_closure_does_not_produce_a_singleton()
{
$count = 0;
$app = new Application;
$app->bind(TestInterface::class, function () use (&$count) {
$count++;
return new TestInterfaceImplementation;
});
$object1 = $app->get(TestInterface::class);
$object2 = $app->get(TestInterface::class);
$this->assertNotSame($object1, $object2);
$this->assertSame(2, $count);
}
/** @test */
public function can_register_a_service_provider()
{
$app = new Application;
$app->register(TestServiceProvider::class);
$providers = $app->getLoadedProviders();
$this->assertSame(1, count($providers));
$this->assertInstanceOf(TestServiceProvider::class, $providers[0]);
}
/** @test */
public function registered_service_provider_is_returned_by_register()
{
$app = new Application;
$provider = $app->register(TestServiceProvider::class);
$this->assertInstanceOf(TestServiceProvider::class, $provider);
}
/** @test */
public function can_retrieve_a_registered_service_provider()
{
$app = new Application;
$provider = $app->register(TestServiceProvider::class);
$this->assertInstanceOf(TestServiceProvider::class, $app->getProvider(TestServiceProvider::class));
$this->assertSame($provider, $app->getProvider(TestServiceProvider::class));
}
/** @test */
public function can_retrieve_a_registered_service_provider_by_object()
{
$app = new Application;
$provider = $app->register(TestServiceProvider::class);
$this->assertInstanceOf(TestServiceProvider::class, $app->getProvider($provider));
$this->assertSame($provider, $app->getProvider($provider));
}
/** @test */
public function can_not_register_the_same_service_provider_twice()
{
$app = new Application;
$provider1 = $app->register(TestServiceProvider::class);
$provider2 = $app->register(TestServiceProvider::class);
$providers = $app->getLoadedProviders();
$this->assertSame(1, count($providers));
$this->assertInstanceOf(TestServiceProvider::class, $providers[0]);
$this->assertSame($provider1, $provider2);
}
/** @test */
public function service_providers_without_register_functions_dont_cause_an_exception()
{
$app = new Application;
$app->register(EmptyServiceProvider::class);
$this->addToAssertionCount(1); // does not throw an exception
}
/** @test */
public function can_register_service_provider_from_an_object()
{
$app = new Application;
$app->register(new TestServiceProvider($app));
$providers = $app->getLoadedProviders();
$this->assertSame(1, count($providers));
$this->assertInstanceOf(TestServiceProvider::class, $providers[0]);
}
/** @test */
public function registered_service_providers_have_their_register_function_called()
{
$app = new Application;
$provider = Mockery::mock(TestServiceProvider::class, [$app]);
$provider->shouldReceive('register')->once();
$app->register($provider);
}
/** @test */
public function calling_boot_on_app_should_call_boot_on_all_registered_service_providers()
{
$app = new Application;
$provider = Mockery::mock(TestServiceProvider::class, [$app]);
$provider->shouldReceive('register');
$provider->shouldReceive('boot')->once();
$app->register($provider);
$app->boot();
}
/** @test */
public function calling_boot_multiple_times_should_not_fire_boot_on_service_providers_more_than_once()
{
$app = new Application;
$provider = Mockery::mock(TestServiceProvider::class, [$app]);
$provider->shouldReceive('register');
$provider->shouldReceive('boot')->once();
$app->register($provider);
$app->boot();
$app->boot();
}
/** @test */
public function boot_should_resolve_dependencies_from_container_on_service_providers()
{
$app = new Application;
$app->bind(TestInterface::class, TestInterfaceImplementation::class);
$provider = new TestBootServiceProvider($app);
$count = 0;
$provider->addBootCallback(function (array $args) use (&$count, $app) {
$count++;
$this->assertInstanceOf(Application::class, $args[0]);
$this->assertSame($app, $args[0]);
$this->assertInstanceOf(TestInterfaceImplementation::class, $args[1]);
});
$app->register($provider);
$app->boot();
$this->assertSame(1, $count);
}
/** @test */
public function services_registered_after_boot_should_have_their_boot_method_called_straight_away()
{
$app = new Application;
$provider = Mockery::mock(TestServiceProvider::class, [$app]);
$provider->shouldReceive('register');
$provider->shouldReceive('boot')->once();
$app->boot();
$app->register($provider);
}
/** @test */
public function is_booted_returns_false_before_boot_method_has_been_called()
{
$app = new Application;
$this->assertFalse($app->isBooted());
}
/** @test */
public function is_booted_returns_true_after_boot_method_has_been_called()
{
$app = new Application;
$app->boot();
$this->assertTrue($app->isBooted());
}
/** @test */
public function can_bootstrap_the_app_with_an_array_of_bootstrappers()
{
$app = new Application;
$count = 0;
$tester = new BootstrapperBootstrapTester(function () use (&$count) {
$count++;
});
$app->bind(BootstrapperBootstrapTester::class, $tester);
$app->bootstrapWith([TestBootstrapper1::class, TestBootstrapper2::class]);
$this->assertSame(2, $count);
}
private function createPhpSapiNameMock($value, $namespace)
{
$builder = new MockBuilder();
$builder->setNamespace($namespace)
->setName('php_sapi_name')
->setFunction(
function () use ($value) {
return $value;
}
);
return $builder->build();
}
/** @test */
public function running_in_console_returns_true_for_cli()
{
$mock = $this->createPhpSapiNameMock('cli', 'Rareloop\Lumberjack');
$mock->enable();
$app = new Application;
$this->assertTrue($app->runningInConsole());
}
/** @test */
public function running_in_console_returns_true_for_phpdbg()
{
$mock = $this->createPhpSapiNameMock('phpdbg', 'Rareloop\Lumberjack');
$mock->enable();
$app = new Application;
$this->assertTrue($app->runningInConsole());
}
/** @test */
public function can_test_if_request_has_been_handled()
{
$app = new Application;
$this->assertFalse($app->hasRequestBeenHandled());
$app->requestHasBeenHandled();
$this->assertTrue($app->hasRequestBeenHandled());
}
/** @test */
public function calling_detectWhenRequestHasNotBeenHandled_adds_actions()
{
$app = new Application;
$app->detectWhenRequestHasNotBeenHandled();
$this->assertTrue(has_action('wp_footer'));
$this->assertTrue(has_action('shutdown'));
}
}
class BootstrapperBootstrapTester
{
public function __construct($callback)
{
$this->callback = $callback;
}
}
abstract class TestBootstrapperBase
{
public function __construct(BootstrapperBootstrapTester $tester)
{
$this->tester = $tester;
}
public function bootstrap(Application $app)
{
call_user_func($this->tester->callback);
}
}
class TestBootstrapper1 extends TestBootstrapperBase
{
}
class TestBootstrapper2 extends TestBootstrapperBase
{
}
interface TestInterface
{
}
class TestInterfaceImplementation implements TestInterface
{
}
class TestInterfaceImplementationWithConstructorParams implements TestInterface
{
public function __construct(TestServiceProvider $provider)
{
}
}
interface TestSubInterface
{
}
class TestSubInterfaceImplementation implements TestSubInterface
{
}
class TestServiceProvider extends ServiceProvider
{
public function register()
{
}
public function boot()
{
}
}
class EmptyServiceProvider extends ServiceProvider
{
}
class TestBootServiceProvider extends ServiceProvider
{
private $bootCallback;
public function register()
{
}
public function boot(Application $app, TestInterface $test)
{
if (isset($this->bootCallback)) {
call_user_func($this->bootCallback, func_get_args());
}
}
public function addBootCallback(\Closure $callback)
{
$this->bootCallback = $callback;
}
}
class NotRegisteredInContainer
{
public $param;
public function __construct(TestInterface $test)
{
$this->param = $test;
}
}
class RequiresAdditionalConstructorParams
{
public $param;
public $param1;
public $param2;
public function __construct(TestInterface $test, $param1, $param2)
{
$this->param = $test;
$this->param1 = $param1;
$this->param2 = $param2;
}
}