diff --git a/.gitignore b/.gitignore index 2c1fc0c1..08dc6ae9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +/build /vendor composer.phar composer.lock -.DS_Store \ No newline at end of file +.DS_Store +.phpunit.result.cache diff --git a/composer.json b/composer.json index a3677e48..6d11b0e4 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,10 @@ "symfony/debug": "^3|^4|^5", "symfony/finder": "^3|^4|^5" }, + "require-dev": { + "orchestra/testbench": "^3.5|^4.0|^5.0", + "phpunit/phpunit": "^6.0|^7.0|^8.5|^9.0" + }, "autoload": { "psr-4": { "Barryvdh\\Debugbar\\": "src/" @@ -26,6 +30,11 @@ "src/helpers.php" ] }, + "autoload-dev": { + "psr-4": { + "Barryvdh\\Debugbar\\Tests\\": "tests" + } + }, "minimum-stability": "dev", "prefer-stable": true, "extra": { @@ -40,8 +49,5 @@ "Debugbar": "Barryvdh\\Debugbar\\Facade" } } - }, - "require-dev": { - "laravel/framework": "5.5.x" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000..11da2e94 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,32 @@ + + + + + tests + + + + + src/ + + + + + + + + + + + + + diff --git a/src/DataCollector/ModelsCollector.php b/src/DataCollector/ModelsCollector.php index ddbfab75..0ac99cf0 100644 --- a/src/DataCollector/ModelsCollector.php +++ b/src/DataCollector/ModelsCollector.php @@ -6,7 +6,6 @@ use DebugBar\DataCollector\DataCollectorInterface; use DebugBar\DataCollector\Renderable; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Support\Str; /** * Collector for Models. @@ -21,13 +20,11 @@ class ModelsCollector extends DataCollector implements DataCollectorInterface, R */ public function __construct(Dispatcher $events) { - $events->listen('eloquent.*', function ($event, $models) { - if (Str::contains($event, 'eloquent.retrieved')) { - foreach (array_filter($models) as $model) { - $class = get_class($model); - $this->models[$class] = ($this->models[$class] ?? 0) + 1; - $this->count++; - } + $events->listen('eloquent.retrieved:*', function ($event, $models) { + foreach (array_filter($models) as $model) { + $class = get_class($model); + $this->models[$class] = ($this->models[$class] ?? 0) + 1; + $this->count++; } }); } diff --git a/tests/DataCollector/ModelsCollectorTest.php b/tests/DataCollector/ModelsCollectorTest.php new file mode 100644 index 00000000..e96d4c0a --- /dev/null +++ b/tests/DataCollector/ModelsCollectorTest.php @@ -0,0 +1,65 @@ +loadLaravelMigrations(); + + $this->debugbar()->boot(); + + /** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */ + $collector = $this->debugbar()->getCollector('models'); + + User::create([ + 'name' => 'John Doe', + 'email' => 'john@example.com', + 'password' => Hash::make('password'), + ]); + + User::create([ + 'name' => 'Jane Doe', + 'email' => 'jane@example.com', + 'password' => Hash::make('password'), + ]); + + $this->assertEquals( + ['data' => [], 'count' => 0], + $collector->collect() + ); + + User::first(); + + $this->assertEquals( + ['data' => [User::class => 1], 'count' => 1], + $collector->collect() + ); + + Person::all(); + + $this->assertEquals( + ['data' => [User::class => 1, Person::class => 2], 'count' => 3], + $collector->collect() + ); + } +} + +class User extends Model +{ + protected $table = 'users'; + protected $guarded = []; +} + +class Person extends User +{ +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 00000000..eb13371d --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,51 @@ + Facade::class]; + } + + public function getEnvironmentSetUp($app) + { + } + + /** + * Get the Laravel Debugbar instance. + * + * @return \Barryvdh\Debugbar\LaravelDebugbar + */ + public function debugbar() + { + return $this->debugbar ?? $this->debugbar = $this->app->debugbar; + } +}