From 985e1740bef5596be387c55a208fd7ee6e58f99c Mon Sep 17 00:00:00 2001 From: Jergus Lejko Date: Mon, 16 Oct 2017 00:18:13 +0100 Subject: [PATCH 1/3] typo --- tests/ClassFinderTest.php | 6 +++--- tests/Stubs/Mails/DemoMail.php | 2 +- tests/Stubs/Seeders/DemoSeeder.php | 2 +- tests/Stubs/ServiceProviders/DemoProvider.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/ClassFinderTest.php b/tests/ClassFinderTest.php index b53e8e04..72dad9ab 100644 --- a/tests/ClassFinderTest.php +++ b/tests/ClassFinderTest.php @@ -50,7 +50,7 @@ public function it_finds_jobs() /** @test */ public function it_finds_mails() { - $this->assertTrue($this->classes->contains(\Wnx\LaravelStats\Tests\Stubs\Mail\DemoMail::class)); + $this->assertTrue($this->classes->contains(\Wnx\LaravelStats\Tests\Stubs\Mails\DemoMail::class)); } /** @test */ @@ -104,13 +104,13 @@ public function it_finds_rules() /** @test */ public function it_finds_seeders() { - $this->assertTrue($this->classes->contains(\Wnx\LaravelStats\Tests\Stubs\Seeder\DemoSeeder::class)); + $this->assertTrue($this->classes->contains(\Wnx\LaravelStats\Tests\Stubs\Seeders\DemoSeeder::class)); } /** @test */ public function it_finds_service_providers() { - $this->assertTrue($this->classes->contains(\Wnx\LaravelStats\Tests\Stubs\Providers\DemoProvider::class)); + $this->assertTrue($this->classes->contains(\Wnx\LaravelStats\Tests\Stubs\ServiceProviders\DemoProvider::class)); } /** @test */ diff --git a/tests/Stubs/Mails/DemoMail.php b/tests/Stubs/Mails/DemoMail.php index 9b1080f9..59c60ef7 100644 --- a/tests/Stubs/Mails/DemoMail.php +++ b/tests/Stubs/Mails/DemoMail.php @@ -1,6 +1,6 @@ Date: Mon, 16 Oct 2017 00:28:44 +0100 Subject: [PATCH 2/3] Introduce Classifiers --- src/Classifiers/Classifier.php | 37 ++++ src/Classifiers/CommandClassifier.php | 18 ++ src/Classifiers/ControllerClassifier.php | 18 ++ src/Classifiers/EventClassifier.php | 24 +++ src/Classifiers/JobClassifier.php | 24 +++ src/Classifiers/MailClassifier.php | 18 ++ src/Classifiers/MiddlewareClassifier.php | 30 +++ src/Classifiers/MigrationClassifier.php | 18 ++ src/Classifiers/ModelClassifier.php | 18 ++ src/Classifiers/NotificationClassifier.php | 18 ++ src/Classifiers/PolicyClassifier.php | 21 ++ src/Classifiers/RequestClassifier.php | 18 ++ src/Classifiers/ResourceClassifier.php | 18 ++ src/Classifiers/RuleClassifier.php | 18 ++ src/Classifiers/SeederClassifier.php | 18 ++ src/Classifiers/ServiceProviderClassifier.php | 18 ++ src/ComponentConfiguration.php | 89 --------- src/ReflectionClass.php | 188 +----------------- tests/ClassifierTest.php | 145 ++++++++++++++ tests/ComponentConfigurationTest.php | 99 --------- tests/ReflectionClassTest.php | 72 +------ 21 files changed, 482 insertions(+), 445 deletions(-) create mode 100644 src/Classifiers/Classifier.php create mode 100644 src/Classifiers/CommandClassifier.php create mode 100644 src/Classifiers/ControllerClassifier.php create mode 100644 src/Classifiers/EventClassifier.php create mode 100644 src/Classifiers/JobClassifier.php create mode 100644 src/Classifiers/MailClassifier.php create mode 100644 src/Classifiers/MiddlewareClassifier.php create mode 100644 src/Classifiers/MigrationClassifier.php create mode 100644 src/Classifiers/ModelClassifier.php create mode 100644 src/Classifiers/NotificationClassifier.php create mode 100644 src/Classifiers/PolicyClassifier.php create mode 100644 src/Classifiers/RequestClassifier.php create mode 100644 src/Classifiers/ResourceClassifier.php create mode 100644 src/Classifiers/RuleClassifier.php create mode 100644 src/Classifiers/SeederClassifier.php create mode 100644 src/Classifiers/ServiceProviderClassifier.php delete mode 100644 src/ComponentConfiguration.php create mode 100644 tests/ClassifierTest.php delete mode 100644 tests/ComponentConfigurationTest.php diff --git a/src/Classifiers/Classifier.php b/src/Classifiers/Classifier.php new file mode 100644 index 00000000..8c96621a --- /dev/null +++ b/src/Classifiers/Classifier.php @@ -0,0 +1,37 @@ +satisfies($class)) { + return $c->getName(); + } + } + } +} diff --git a/src/Classifiers/CommandClassifier.php b/src/Classifiers/CommandClassifier.php new file mode 100644 index 00000000..c51a0260 --- /dev/null +++ b/src/Classifiers/CommandClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Console\Command::class); + } +} diff --git a/src/Classifiers/ControllerClassifier.php b/src/Classifiers/ControllerClassifier.php new file mode 100644 index 00000000..8076f79e --- /dev/null +++ b/src/Classifiers/ControllerClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Routing\Controller::class); + } +} diff --git a/src/Classifiers/EventClassifier.php b/src/Classifiers/EventClassifier.php new file mode 100644 index 00000000..01e09e9c --- /dev/null +++ b/src/Classifiers/EventClassifier.php @@ -0,0 +1,24 @@ +getTraits() as $trait) { + if ($trait->name == \Illuminate\Foundation\Events\Dispatchable::class) { + return true; + } + } + + return false; + } +} diff --git a/src/Classifiers/JobClassifier.php b/src/Classifiers/JobClassifier.php new file mode 100644 index 00000000..0a2e7623 --- /dev/null +++ b/src/Classifiers/JobClassifier.php @@ -0,0 +1,24 @@ +getTraits() as $trait) { + if ($trait->name == \Illuminate\Foundation\Bus\Dispatchable::class) { + return true; + } + } + + return false; + } +} diff --git a/src/Classifiers/MailClassifier.php b/src/Classifiers/MailClassifier.php new file mode 100644 index 00000000..85670ead --- /dev/null +++ b/src/Classifiers/MailClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Mail\Mailable::class); + } +} diff --git a/src/Classifiers/MiddlewareClassifier.php b/src/Classifiers/MiddlewareClassifier.php new file mode 100644 index 00000000..baa3fe0c --- /dev/null +++ b/src/Classifiers/MiddlewareClassifier.php @@ -0,0 +1,30 @@ +hasMiddleware($class->getName())) { + return true; + } + + $router = resolve('router'); + $middlewares = collect($router->getMiddleware())->flatten(); + $groupMiddlewares = collect($router->getMiddlewareGroups())->flatten(); + $mergedMiddlewares = $middlewares->merge($groupMiddlewares); + + return $mergedMiddlewares->contains($class->getName()); + } +} diff --git a/src/Classifiers/MigrationClassifier.php b/src/Classifiers/MigrationClassifier.php new file mode 100644 index 00000000..755f2ef3 --- /dev/null +++ b/src/Classifiers/MigrationClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Database\Migrations\Migration::class); + } +} diff --git a/src/Classifiers/ModelClassifier.php b/src/Classifiers/ModelClassifier.php new file mode 100644 index 00000000..42171891 --- /dev/null +++ b/src/Classifiers/ModelClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Database\Eloquent\Model::class); + } +} diff --git a/src/Classifiers/NotificationClassifier.php b/src/Classifiers/NotificationClassifier.php new file mode 100644 index 00000000..c5c04638 --- /dev/null +++ b/src/Classifiers/NotificationClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Notifications\Notification::class); + } +} diff --git a/src/Classifiers/PolicyClassifier.php b/src/Classifiers/PolicyClassifier.php new file mode 100644 index 00000000..30301edf --- /dev/null +++ b/src/Classifiers/PolicyClassifier.php @@ -0,0 +1,21 @@ +getName(), resolve(Gate::class)->policies() + ); + } +} diff --git a/src/Classifiers/RequestClassifier.php b/src/Classifiers/RequestClassifier.php new file mode 100644 index 00000000..9fb0162e --- /dev/null +++ b/src/Classifiers/RequestClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Foundation\Http\FormRequest::class); + } +} diff --git a/src/Classifiers/ResourceClassifier.php b/src/Classifiers/ResourceClassifier.php new file mode 100644 index 00000000..8be36fc7 --- /dev/null +++ b/src/Classifiers/ResourceClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Http\Resources\Json\Resource::class); + } +} diff --git a/src/Classifiers/RuleClassifier.php b/src/Classifiers/RuleClassifier.php new file mode 100644 index 00000000..2ff027d4 --- /dev/null +++ b/src/Classifiers/RuleClassifier.php @@ -0,0 +1,18 @@ +implementsInterface(\Illuminate\Contracts\Validation\Rule::class); + } +} diff --git a/src/Classifiers/SeederClassifier.php b/src/Classifiers/SeederClassifier.php new file mode 100644 index 00000000..3952b8de --- /dev/null +++ b/src/Classifiers/SeederClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Database\Seeder::class); + } +} diff --git a/src/Classifiers/ServiceProviderClassifier.php b/src/Classifiers/ServiceProviderClassifier.php new file mode 100644 index 00000000..07620e97 --- /dev/null +++ b/src/Classifiers/ServiceProviderClassifier.php @@ -0,0 +1,18 @@ +isSubclassOf(\Illuminate\Support\ServiceProvider::class); + } +} diff --git a/src/ComponentConfiguration.php b/src/ComponentConfiguration.php deleted file mode 100644 index 1a240946..00000000 --- a/src/ComponentConfiguration.php +++ /dev/null @@ -1,89 +0,0 @@ - 'Controllers', - 'extends' => \Illuminate\Routing\Controller::class, - ], - [ - 'name' => 'Models', - 'extends' => \Illuminate\Database\Eloquent\Model::class, - ], - [ - 'name' => 'Commands', - 'extends' => \Illuminate\Console\Command::class, - ], - [ - 'name' => 'Events', - 'uses' => \Illuminate\Foundation\Events\Dispatchable::class, - ], - [ - 'name' => 'Mails', - 'extends' => \Illuminate\Mail\Mailable::class, - ], - [ - 'name' => 'Notifications', - 'extends' => \Illuminate\Notifications\Notification::class, - ], - [ - 'name' => 'Jobs', - 'uses' => \Illuminate\Foundation\Bus\Dispatchable::class, - ], - [ - 'name' => 'Migrations', - 'extends' => \Illuminate\Database\Migrations\Migration::class, - ], - [ - 'name' => 'Seeders', - 'extends' => \Illuminate\Database\Seeder::class, - ], - [ - 'name' => 'Resources', - 'extends' => \Illuminate\Http\Resources\Json\Resource::class, - ], - [ - 'name' => 'Rules', - 'implements' => \Illuminate\Contracts\Validation\Rule::class, - ], - [ - 'name' => 'Requests', - 'extends' => \Illuminate\Foundation\Http\FormRequest::class, - ], - [ - 'name' => 'Service Providers', - 'extends' => \Illuminate\Support\ServiceProvider::class, - ], - [ - 'name' => 'Policies', - ], - [ - 'name' => 'Middlewares', - ], - [ - 'name' => 'PHPUnit Tests', - // TODO - ], - [ - 'name' => 'phpspec Tests', - // TODO - ], - [ - 'name' => 'Dusk Tests', - // TODO - ], - ]); - } -} diff --git a/src/ReflectionClass.php b/src/ReflectionClass.php index e369e95d..1afdd7dc 100644 --- a/src/ReflectionClass.php +++ b/src/ReflectionClass.php @@ -2,8 +2,8 @@ namespace Wnx\LaravelStats; -use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Support\Collection; +use Wnx\LaravelStats\Classifiers\Classifier; use ReflectionClass as NativeReflectionClass; class ReflectionClass extends NativeReflectionClass @@ -21,195 +21,11 @@ public function isVendorProvided() public function getLaravelComponentName() { - if ($componentName = $this->extendsLaravelComponentClass($this)) { - return $componentName; - } elseif ($componentName = $this->usesLaravelComponentTrait($this)) { - return $componentName; - } elseif ($componentName = $this->implementsLaravelComponentInterface($this)) { - return $componentName; - } elseif ($componentName = $this->isRegisteredPolicy($this)) { - return $componentName; - } elseif ($componentName = $this->isRegisteredMiddleware($this)) { - return $componentName; - } + return (new Classifier)->classify($this); } public function isLaravelComponent() { return (bool) $this->getLaravelComponentName(); - - } - - /** - * Get Component Configuration. - * - * @return Collection - */ - protected function componentConfiguration() : Collection - { - return resolve(ComponentConfiguration::class)->get(); - } - - /** - * Determine if given Class extends from a Core Laravel Class. - * - * @param ReflectionClass $reflection - * - * @return mixed (string|boolean) - */ - protected function extendsLaravelComponentClass(NativeReflectionClass $reflection) - { - // Check if Classname of currently given Class is in Extends Array - $extends = $this->componentConfiguration()->pluck('extends', 'name')->filter(); - - $className = $reflection->getName(); - $componentName = $extends->search($className); - - // Found current Class Name, return the Component Name - if ($componentName !== false) { - return $componentName; - } - - // Does the Class have a Parent Class? - // If yes, recursivly call this method - $hasParentClass = $reflection->getParentClass(); - - if ($hasParentClass !== false) { - return $this->extendsLaravelComponentClass($hasParentClass); - } - - // If no, return false - return false; - } - - /** - * Determine if a given Class uses a Laravel Core Trait. - * - * @param ReflectionClass $reflection - * - * @return mixed (string|boolean) - */ - protected function usesLaravelComponentTrait(NativeReflectionClass $reflection) - { - // If the given Class does not use any traits, return false - if (count($reflection->getTraits()) == 0) { - return false; - } - - $uses = $this->componentConfiguration()->pluck('uses', 'name')->filter(); - - $classTraits = $reflection->getTraitNames(); - $componentName = false; - - // Loop through all traits and search Trait in Component Configuration - foreach ($classTraits as $trait) { - if ($uses->search($trait)) { - $componentName = $uses->search($trait); - } - } - - // If Trait has been found, we return the Component Name - if ($componentName !== false) { - return $componentName; - } - - // Does the Class have a Parent Class? - // If yes, recursivly call this method - $hasParentClass = $reflection->getParentClass(); - - if ($hasParentClass !== false) { - return $this->usesLaravelComponentTrait($hasParentClass); - } - - return false; - } - - public function implementsLaravelComponentInterface(NativeReflectionClass $reflection) - { - // If the given Class does not use any traits, return false - if (count($reflection->getInterfaces()) == 0) { - return false; - } - - $implements = $this->componentConfiguration()->pluck('implements', 'name')->filter(); - - foreach ($implements as $name => $interface) { - if ($reflection->implementsInterface($interface) == true) { - return $name; - } - } - - // Does the Class have a Parent Class? - // If yes, recursively call this method - $hasParentClass = $reflection->getParentClass(); - - if ($hasParentClass !== false) { - return $this->implementsLaravelComponentInterface($hasParentClass); - } - - return false; - } - - /** - * Determine if the given class is a registered policy. - * - * @param ReflectionClass $reflection - * - * @return mixed (string|boolean) - */ - public function isRegisteredPolicy(NativeReflectionClass $reflection) - { - $policies = resolve(Gate::class)->policies(); - - if (in_array($reflection->getName(), $policies)) { - return 'Policies'; - } - - $hasParentClass = $reflection->getParentClass(); - - // Does the Class have a Parent Class? - // If yes, recursively call this method - if ($hasParentClass !== false) { - return $this->isRegisteredPolicy($hasParentClass); - } - - return false; - } - - /** - * Determine if the given class is a registered Middleware - * - * @param \ReflectionClass $reflection - * - * @return boolean - */ - public function isRegisteredMiddleware(\ReflectionClass $reflection) - { - // The Router Instance returns empty array, if I don't resolve the - // HTTP Kernel here. Why is that? This seems weird ... - $kernel = resolve(\Illuminate\Contracts\Http\Kernel::class); - - if ($kernel->hasMiddleware($reflection->getName())) { - return 'Middlewares'; - } - - $router = resolve('router'); - $middlewares = collect($router->getMiddleware())->flatten(); - $groupMiddlewares = collect($router->getMiddlewareGroups())->flatten(); - $mergedMiddlewares = $middlewares->merge($groupMiddlewares); - - if ($mergedMiddlewares->contains($reflection->getName())) { - return 'Middlewares'; - } - - $hasParentClass = $reflection->getParentClass(); - - // Does the Class have a Parent Class? - // If yes, recursively call this method - if ($hasParentClass !== false) { - return $this->isRegisteredMiddleware($hasParentClass); - } - - return false; } } diff --git a/tests/ClassifierTest.php b/tests/ClassifierTest.php new file mode 100644 index 00000000..45dab9c7 --- /dev/null +++ b/tests/ClassifierTest.php @@ -0,0 +1,145 @@ +classifier = new Classifier; + } + + /** @test */ + public function it_returns_null_for_unidentified_class() + { + $this->assertNull( + $this->classifier->classify(new ReflectionClass(new class {})) + ); + } + + /** @test */ + public function it_detects_commands() + { + $this->assertSame( + 'Commands', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Commands\DemoCommand::class)) + ); + } + + /** @test */ + public function it_detects_controllers() + { + $this->assertSame( + 'Controllers', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Controllers\ProjectsController::class)) + ); + } + + /** @test */ + public function it_detects_events() + { + $this->assertSame( + 'Events', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Events\DemoEvent::class)) + ); + } + + /** @test */ + public function it_detects_jobs() + { + $this->assertSame( + 'Jobs', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Jobs\DemoJob::class)) + ); + } + + /** @test */ + public function it_detects_mails() + { + $this->assertSame( + 'Mails', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Mails\DemoMail::class)) + ); + } + + /** @test */ + public function it_detects_middlewares() + { + $this->assertSame( + 'Middlewares', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Middlewares\DemoMiddleware::class)) + ); + } + + /** @test */ + public function it_detects_migrations() + { + $this->markTestSkipped(); + } + + /** @test */ + public function it_detects_models() + { + $this->assertSame( + 'Models', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Models\Project::class)) + ); + } + + /** @test */ + public function it_detects_notifications() + { + $this->assertSame( + 'Notifications', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Notifications\ServerDownNotification::class)) + ); + } + + /** @test */ + public function it_detects_policies() + { + Gate::policy(Project::class, \Wnx\LaravelStats\Tests\Stubs\Policies\DemoPolicy::class); + + $this->assertSame( + 'Policies', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Policies\DemoPolicy::class)) + ); + } + + /** @test */ + public function it_detects_requests() + { + $this->assertSame( + 'Requests', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Requests\UserRequest::class)) + ); + } + + /** @test */ + public function it_detects_resources() + { + $this->assertSame( + 'Resources', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Resources\DemoResource::class)) + ); + } + + /** @test */ + public function it_detects_rules() + { + $this->assertSame( + 'Rules', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Rules\DemoRule::class)) + ); + } + + /** @test */ + public function it_detects_seeders() + { + $this->assertSame( + 'Seeders', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Seeders\DemoSeeder::class)) + ); + } + + /** @test */ + public function it_detects_service_providers() + { + $this->assertSame( + 'Service Providers', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\ServiceProviders\DemoProvider::class)) + ); + } +} diff --git a/tests/ComponentConfigurationTest.php b/tests/ComponentConfigurationTest.php deleted file mode 100644 index 8d284a00..00000000 --- a/tests/ComponentConfigurationTest.php +++ /dev/null @@ -1,99 +0,0 @@ -config = resolve(ComponentConfiguration::class)->get(); - } - - /** @test */ - public function it_has_configuration_for_controllers() - { - $this->assertTrue($this->config->contains('name', 'Controllers')); - } - - /** @test */ - public function it_has_configuration_ofrm_models() - { - $this->assertTrue($this->config->contains('name', 'Models')); - } - - /** @test */ - public function it_has_configuration_for_commands() - { - $this->assertTrue($this->config->contains('name', 'Commands')); - } - - /** @test */ - public function it_has_configuration_for_events() - { - $this->assertTrue($this->config->contains('name', 'Events')); - } - - /** @test */ - public function it_has_configuration_for_mails() - { - $this->assertTrue($this->config->contains('name', 'Mails')); - } - - /** @test */ - public function it_has_configuration_for_notifications() - { - $this->assertTrue($this->config->contains('name', 'Notifications')); - } - - /** @test */ - public function it_has_configuration_for_jobs() - { - $this->assertTrue($this->config->contains('name', 'Jobs')); - } - - /** @test */ - public function it_has_configuration_for_migrations() - { - $this->assertTrue($this->config->contains('name', 'Migrations')); - } - - /** @test */ - public function it_has_configuration_for_seeders() - { - $this->assertTrue($this->config->contains('name', 'Seeders')); - } - - /** @test */ - public function it_has_configuration_for_api_resources() - { - $this->assertTrue($this->config->contains('name', 'Resources')); - } - - /** @test */ - public function it_has_configuration_for_validation_rules() - { - $this->assertTrue($this->config->contains('name', 'Rules')); - } - - /** @test */ - public function it_has_configuration_for_form_requests() - { - $this->assertTrue($this->config->contains('name', 'Requests')); - } - - /** @test */ - public function it_has_configuration_for_service_providers() - { - $this->assertTrue($this->config->contains('name', 'Service Providers')); - } - - /** @test */ - public function it_has_configuration_for_policies() - { - $this->assertTrue($this->config->contains('name', 'Policies')); - } -} diff --git a/tests/ReflectionClassTest.php b/tests/ReflectionClassTest.php index 79016ba3..4889e4ae 100644 --- a/tests/ReflectionClassTest.php +++ b/tests/ReflectionClassTest.php @@ -2,36 +2,13 @@ namespace Wnx\LaravelStats\Tests; -use Illuminate\Support\Facades\Gate; use Wnx\LaravelStats\ReflectionClass; use Wnx\LaravelStats\Tests\Stubs\Controllers\ProjectsController; -use Wnx\LaravelStats\Tests\Stubs\Middlewares\DemoMiddleware; -use Wnx\LaravelStats\Tests\Stubs\Models\Project; -use Wnx\LaravelStats\Tests\Stubs\Policies\DemoPolicy; -use Wnx\LaravelStats\Tests\Stubs\Rules\DemoRule; class ReflectionClassTest extends TestCase { /** @test */ - public function it_returns_null_if_component_name_could_not_be_defined() - { - $reflection = new ReflectionClass(new class() { - }); - - $this->assertNull($reflection->getLaravelComponentName()); - } - - /** @test */ - public function it_returns_false_if_class_could_not_be_identified_as_a_laravel_component() - { - $reflection = new ReflectionClass(new class() { - }); - - $this->assertFalse($reflection->isLaravelComponent()); - } - - /** @test */ - public function it_returns_true_if_class_could_be_identified_as_a_laravel_component() + public function it_determines_wether_given_class_is_laravel_component() { $reflection = new ReflectionClass(ProjectsController::class); @@ -45,51 +22,4 @@ public function it_returns_component_name() $this->assertEquals('Controllers', $reflection->getLaravelComponentName()); } - - /** @test */ - public function it_returns_true_if_class_can_be_identified_as_a_component_through_an_interface() - { - $reflection = new ReflectionClass(DemoRule::class); - - $this->assertEquals(true, $reflection->isLaravelComponent()); - } - - /** @test */ - public function it_returns_component_name_for_interface_components() - { - $reflection = new ReflectionClass(DemoRule::class); - - $this->assertEquals('Rules', $reflection->getLaravelComponentName()); - } - - /** @test */ - public function it_returns_component_name_for_policies() - { - Gate::policy(Project::class, DemoPolicy::class); - - $reflection = new ReflectionClass(DemoPolicy::class); - - $this->assertEquals('Policies', $reflection->getLaravelComponentName()); - } - - /** @test */ - public function it_returns_component_name_for_middlewares() - { - $reflection = new ReflectionClass(DemoMiddleware::class); - - $this->assertTrue($reflection->isLaravelComponent()); - $this->assertEquals('Middlewares', $reflection->getLaravelComponentName()); - } - - /** @test */ - public function it_currently_does_not_recognize_middlewares_which_are_only_defined_in_the_global_middleware_array() - { - // TODO: The TrimStrings-Middleware should also be recognized as a Middleware, even though - // it has only been declared in the `$middleware` variable - - $reflection = new ReflectionClass(\Illuminate\Foundation\Http\Middleware\TrimStrings::class); - - $this->assertFalse($reflection->isLaravelComponent()); - $this->assertEquals(null, $reflection->getLaravelComponentName()); - } } From c41f44bd8c1e31e1d7d68c46ceadb619565190dc Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Mon, 16 Oct 2017 19:29:52 +0200 Subject: [PATCH 3/3] Fix it_detects_migrations --- tests/ClassifierTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/ClassifierTest.php b/tests/ClassifierTest.php index 45dab9c7..f6c28cdd 100644 --- a/tests/ClassifierTest.php +++ b/tests/ClassifierTest.php @@ -74,7 +74,10 @@ public function it_detects_middlewares() /** @test */ public function it_detects_migrations() { - $this->markTestSkipped(); + $this->assertSame( + 'Migrations', $this->classifier->classify(new ReflectionClass(\Wnx\LaravelStats\Tests\Stubs\Migrations\CreateUsersTable::class)) + ); + } /** @test */