Skip to content

Commit a00653b

Browse files
authored
Merge pull request #91 from loonytoons/addTests
Add tests
2 parents bd08395 + 54806a3 commit a00653b

File tree

7 files changed

+216
-16
lines changed

7 files changed

+216
-16
lines changed

src/AnnotationFinder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(Application $app)
2626
*/
2727
public function routesAreScanned()
2828
{
29-
return $this->app['files']->exists($this->getScannedRoutesPath());
29+
return $this->app->make('files')->exists($this->getScannedRoutesPath());
3030
}
3131

3232
/**
@@ -36,7 +36,7 @@ public function routesAreScanned()
3636
*/
3737
public function getScannedRoutesPath()
3838
{
39-
return $this->app['path.storage'].'/framework/routes.scanned.php';
39+
return $this->app->make('path.storage').'/framework/routes.scanned.php';
4040
}
4141

4242
/**
@@ -46,7 +46,7 @@ public function getScannedRoutesPath()
4646
*/
4747
public function eventsAreScanned()
4848
{
49-
return $this->app['files']->exists($this->getScannedEventsPath());
49+
return $this->app->make('files')->exists($this->getScannedEventsPath());
5050
}
5151

5252
/**
@@ -56,7 +56,7 @@ public function eventsAreScanned()
5656
*/
5757
public function getScannedEventsPath()
5858
{
59-
return $this->app['path.storage'].'/framework/events.scanned.php';
59+
return $this->app->make('path.storage').'/framework/events.scanned.php';
6060
}
6161

6262
/**
@@ -66,7 +66,7 @@ public function getScannedEventsPath()
6666
*/
6767
public function modelsAreScanned()
6868
{
69-
return $this->app['files']->exists($this->getScannedModelsPath());
69+
return $this->app->make('files')->exists($this->getScannedModelsPath());
7070
}
7171

7272
/**
@@ -76,6 +76,6 @@ public function modelsAreScanned()
7676
*/
7777
public function getScannedModelsPath()
7878
{
79-
return $this->app['path.storage'].'/framework/models.scanned.php';
79+
return $this->app->make('path.storage').'/framework/models.scanned.php';
8080
}
8181
}

src/NamespaceToPathConverterTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Collective\Annotations;
44

55
use Illuminate\Console\DetectsApplicationNamespace;
6+
use Illuminate\Support\Facades\App;
67

78
trait NamespaceToPathConverterTrait
89
{
@@ -27,6 +28,6 @@ public function getPathFromNamespace($namespace, $base = null)
2728
$path = str_replace('\\', '/', trim($namespace, ' \\'));
2829

2930
// trim and return the path
30-
return ($base ?: app_path()).'/'.$path;
31+
return ($base ?: App::make('path')).'/'.$path;
3132
}
3233
}

tests/AnnotationFinderTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
use Collective\Annotations\AnnotationFinder;
4+
use PHPUnit\Framework\TestCase;
5+
use Mockery as m;
6+
7+
class AnnotationFinderTest extends TestCase
8+
{
9+
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;
10+
11+
/**
12+
* @var m\MockInterface|Illuminate\Contracts\Foundation\Application
13+
*/
14+
protected $app;
15+
16+
public function setUp(): void
17+
{
18+
$this->app = m::mock('Illuminate\Contracts\Foundation\Application');
19+
}
20+
21+
/**
22+
* @dataProvider scannedChecksDataProvider
23+
*
24+
* @covers \Collective\Annotations\AnnotationFinder::routesAreScanned
25+
* @covers \Collective\Annotations\AnnotationFinder::getScannedRoutesPath
26+
* @covers \Collective\Annotations\AnnotationFinder::eventsAreScanned
27+
* @covers \Collective\Annotations\AnnotationFinder::getScannedEventsPath
28+
* @covers \Collective\Annotations\AnnotationFinder::modelsAreScanned
29+
* @covers \Collective\Annotations\AnnotationFinder::getScannedModelsPath
30+
*/
31+
public function testRoutesAreScannedReturnsTrue($filepath, $found, $method, $expected)
32+
{
33+
$files = m::mock('Illuminate\Filesystem\Filesystem');
34+
$files->shouldReceive('exists')
35+
->with($filepath)->once()
36+
->andReturn($found);
37+
38+
$this->app->shouldReceive('make')
39+
->with('files')->once()
40+
->andReturn($files);
41+
42+
$this->app->shouldReceive('make')
43+
->with('path.storage')->once()
44+
->andReturn('storage');
45+
46+
$this->assertEquals($expected, (new AnnotationFinder($this->app))->$method());
47+
}
48+
49+
public function scannedChecksDataProvider()
50+
{
51+
return [
52+
'routesScanned' => [
53+
'storage/framework/routes.scanned.php',
54+
true,
55+
'routesAreScanned',
56+
true,
57+
],
58+
'routesNotScanned' => [
59+
'storage/framework/routes.scanned.php',
60+
false,
61+
'routesAreScanned',
62+
false,
63+
],
64+
'eventsScanned' => [
65+
'storage/framework/events.scanned.php',
66+
true,
67+
'eventsAreScanned',
68+
true,
69+
],
70+
'eventsNotScanned' => [
71+
'storage/framework/events.scanned.php',
72+
false,
73+
'eventsAreScanned',
74+
false,
75+
],
76+
'modelsScanned' => [
77+
'storage/framework/models.scanned.php',
78+
true,
79+
'modelsAreScanned',
80+
true,
81+
],
82+
'modelsNotScanned' => [
83+
'storage/framework/models.scanned.php',
84+
false,
85+
'modelsAreScanned',
86+
false,
87+
],
88+
];
89+
}
90+
}

tests/AnnotationScannerTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
use Collective\Annotations\AnnotationScanner;
4+
use Collective\Annotations\Events\Annotations\Scanner;
5+
use Doctrine\Common\Annotations\AnnotationRegistry;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Support\Facades\App;
8+
use PHPUnit\Framework\TestCase;
9+
use Mockery as m;
10+
11+
class AnnotationScannerTest extends TestCase
12+
{
13+
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;
14+
15+
/**
16+
* @var m\MockInterface|Illuminate\Contracts\Foundation\Application
17+
*/
18+
protected $app;
19+
20+
protected function setUp(): void
21+
{
22+
$this->app = m::mock('Illuminate\Contracts\Foundation\Application');
23+
}
24+
25+
/**
26+
* Test that the scanner finds the events annotations directory as expected
27+
* Ensure that the Hears class is loaded into the scanner
28+
*
29+
* @covers \Collective\Annotations\AnnotationScanner::addAnnotationNamespace
30+
* @covers \Collective\Annotations\AnnotationScanner::registerAnnotationsPathWithRegistry
31+
* @covers \Collective\Annotations\NamespaceToPathConverterTrait::getPathFromNamespace
32+
*/
33+
public function testAddAnnotationNamespaceWithoutSpecifiedPath()
34+
{
35+
//update the app namespace to be App
36+
$this->app->shouldReceive('getNamespace')->once()
37+
->andReturn('App\\');
38+
Container::setInstance($this->app);
39+
40+
//update the app path in the facade to use `src` which is where the event scanner lives in this project
41+
App::shouldReceive('make')
42+
->with('path')->once()
43+
->andReturn('src');
44+
45+
$scanner = new Scanner(['App\Handlers\Events\BasicEventHandler']);
46+
47+
//underneath the scanner converts the App namespace to the src dir, based on what we set into the app above
48+
$return = $scanner->addAnnotationNamespace(
49+
'App\Events\Annotations\Annotations'
50+
);
51+
52+
$this->assertInstanceOf(Scanner::class, $return);
53+
$this->assertTrue(
54+
AnnotationRegistry::loadAnnotationClass('Collective\Annotations\Events\Annotations\Annotations\Hears')
55+
);
56+
}
57+
58+
/**
59+
* Ensures that the class that doesn't exist is ignored by the scanner
60+
* And that the existing class is loaded in as expected
61+
*/
62+
public function testSetClassesToScan()
63+
{
64+
require_once __DIR__.'/Events/fixtures/annotations/BasicEventHandler.php';
65+
66+
$scanner = new TestAnnotationScanner([]);
67+
$scanner->setClassesToScan([
68+
'App\Handlers\Events\BasicEventHandler',
69+
'App\Handlers\Events\NonExistentHandler',
70+
]);
71+
72+
$classesToScan = $scanner->getClassesToScan();
73+
$this->assertCount(1, $classesToScan);
74+
$this->assertEquals('App\Handlers\Events\BasicEventHandler', reset($classesToScan)->name);
75+
}
76+
}
77+
78+
/**
79+
* Class TestAnnotationScanner
80+
*
81+
* Override class to expose the protected getClassesToScan method
82+
*/
83+
class TestAnnotationScanner extends AnnotationScanner
84+
{
85+
public function getClassesToScan()
86+
{
87+
return parent::getClassesToScan();
88+
}
89+
}

tests/AnnotationsServiceProviderTest.php

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

77
class AnnotationsServiceProviderTest extends TestCase
88
{
9+
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;
10+
911
/**
10-
* @var m\LegacyMockInterface|m\MockInterface|Illuminate\Contracts\Foundation\Application
12+
* @var m\MockInterface|Illuminate\Contracts\Foundation\Application
1113
*/
1214
protected $app;
1315

@@ -22,11 +24,6 @@ public function setUp(): void
2224
$this->provider = new AnnotationsServiceProvider($this->app);
2325
}
2426

25-
public function tearDown(): void
26-
{
27-
m::close();
28-
}
29-
3027
public function testConvertNamespaceToPath()
3128
{
3229
$this->provider = new AnnotationsServiceProviderAppNamespaceStub($this->app);

tests/Models/ModelAnnotationScannerTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22

33
use Collective\Annotations\Database\Eloquent\Annotations\Scanner;
4+
use Collective\Annotations\Database\Eloquent\Annotations\InvalidBindingResolverException;
45
use PHPUnit\Framework\TestCase;
56

67
class ModelAnnotationScannerTest extends TestCase
78
{
8-
public function testProperRouteDefinitionsAreGenerated()
9+
public function testProperModelDefinitionsAreGenerated()
910
{
1011
require_once __DIR__.'/fixtures/annotations/AnyModel.php';
1112
$scanner = $this->makeScanner(['App\User']);
@@ -14,8 +15,20 @@ public function testProperRouteDefinitionsAreGenerated()
1415
$this->assertEquals(trim(file_get_contents(__DIR__.'/results/annotation.php')), $definition);
1516
}
1617

18+
public function testNonEloquentModelThrowsException()
19+
{
20+
$this->expectException(InvalidBindingResolverException::class);
21+
$this->expectExceptionMessage('Class [App\NonEloquentModel] is not a subclass of [Illuminate\Database\Eloquent\Model]');
22+
23+
require_once __DIR__.'/fixtures/annotations/NonEloquentModel.php';
24+
$scanner = $this->makeScanner(['App\NonEloquentModel']);
25+
26+
$definition = str_replace(PHP_EOL, "\n", $scanner->getModelDefinitions());
27+
$this->assertEquals(trim(file_get_contents(__DIR__.'/results/annotation.php')), $definition);
28+
}
29+
1730
/**
18-
* Construct a route annotation scanner.
31+
* Construct a model annotation scanner.
1932
*
2033
* @param array $paths
2134
*
@@ -27,7 +40,7 @@ protected function makeScanner($paths)
2740

2841
$scanner->addAnnotationNamespace(
2942
'Collective\Annotations\Database\Eloquent\Annotations\Annotations',
30-
realpath(__DIR__.'/../../src/Database/Eloquent//Annotations/Annotations')
43+
realpath(__DIR__.'/../../src/Database/Eloquent/Annotations/Annotations')
3144
);
3245

3346
return $scanner;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App;
4+
5+
/**
6+
* @Bind("systems")
7+
*/
8+
class NonEloquentModel
9+
{
10+
}

0 commit comments

Comments
 (0)