Skip to content

Commit 6c26bc0

Browse files
committed
add test coverage and illuminate 12
1 parent 7ac5ccc commit 6c26bc0

32 files changed

+1870
-38
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require": {
2222
"php": "^8.0",
2323
"guzzlehttp/guzzle": "^6.0 | ^7.0",
24-
"illuminate/contracts": "^8.0 | ^9.0 | ^10.0 | ^11.0",
24+
"illuminate/contracts": "^8.0 | ^9.0 | ^10.0 | ^11.0 | ^12.0",
2525
"nesbot/carbon": "^2.0 | ^3.0",
2626
"spatie/laravel-package-tools": "^1.0 | ^1.14.0"
2727
},

database/factories/BillFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function definition()
1414
return [
1515
'ref' => $this->faker->name,
1616
'price' => $this->faker->randomNumber(4),
17+
'amount' => $this->faker->randomNumber(3),
1718
];
1819
}
1920
}

database/factories/UserFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function definition()
1313
{
1414
return [
1515
'name' => $this->faker->name,
16+
'active' => $this->faker->boolean,
1617
];
1718
}
1819
}

database/migrations/create_bills_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ return new class extends Migration
1212
$table->id();
1313
$table->string('ref');
1414
$table->integer('price');
15+
$table->integer('amount');
1516
$table->timestamps();
1617
});
1718
}

database/migrations/create_users_table.php.stub

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ return new class extends Migration
1111
Schema::create('users', function (Blueprint $table) {
1212
$table->id();
1313
$table->string('name');
14+
$table->boolean('active')->default(true);
15+
$table->string('status')->nullable();
1416
$table->timestamps();
1517
});
1618
}

src/Commands/ModelCountOverall.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public function perform(string $model, $modelsPath = null, ?string $scope = null
3030
$modelClass = $this->getModelClass($model, $modelsPath);
3131

3232
try {
33+
// Check if the model class exists
34+
if (!class_exists($modelClass)) {
35+
$this->error("Model class $modelClass does not exist.");
36+
return 1;
37+
}
3338

3439
// sound avg / min / max / sum return 0 if no records found ? for now
3540
$query = $modelClass::query();
@@ -58,8 +63,7 @@ public function perform(string $model, $modelsPath = null, ?string $scope = null
5863

5964
$metrics[] = (object) ['label' => $label, 'count' => $count];
6065
} catch (\Exception $e) {
61-
$this->verbose('An error occurred: '.$e->getMessage(), 'error');
62-
66+
$this->error('An error occurred: '.$e->getMessage());
6367
return 1;
6468
}
6569

@@ -82,10 +86,21 @@ public function handle()
8286
}
8387

8488
$metrics = $this->perform($model, $modelsPath, $scope, $scopeValue);
89+
90+
// If perform returns 1, it means there was an error
91+
if ($metrics === 1) {
92+
return 1;
93+
}
94+
95+
// If oldestDate is null or 1, it means there was an error
96+
if ($oldestDate === null || $oldestDate === 1) {
97+
return 1;
98+
}
99+
85100
$period = new CarbonPeriod($oldestDate, Carbon::now()->endOfDay());
86101
$metricsData = $this->prepareMetricsData($metrics, $period, 'overall');
87102

88-
$this->sendMetricsData($metricsData, config('eloquentize.ELOQUENTIZE_API_TOKEN'));
103+
$this->sendMetricsData($metricsData, \config('eloquentize.ELOQUENTIZE_API_TOKEN'));
89104

90105
return 0;
91106
}

src/Commands/ModelsCount.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Eloquentize\LaravelClient\Commands\Traits\ModelsOption;
1111
use Eloquentize\LaravelClient\Commands\Traits\PrepareMetricsData;
1212
use Eloquentize\LaravelClient\Commands\Traits\SendMetricsData;
13+
use Illuminate\Support\Facades\Config;
1314

1415
class ModelsCount extends BaseCommand
1516
{
@@ -100,14 +101,14 @@ public function handle()
100101
$metrics = $this->performModelCount($models, $period, $event, $modelsPath, $scope, $scopeValue);
101102
$metricsData = $this->prepareMetricsData($metrics, $period, $event);
102103

103-
$this->verbose('Sending models count data to eloquentize...'.config('eloquentize.api_url').'/api/metrics/models');
104+
$this->verbose('Sending models count data to eloquentize...'.Config::get('eloquentize.api_url').'/api/metrics/models');
104105
if ($this->dry) {
105106
$this->line('');
106107
$this->warn('Dry run enabled. Data NOT sent to eloquentize.');
107108
$this->line('');
108109
$this->line('----- Source data -----');
109110
$this->line('The data will be stored in source :');
110-
$this->info('***** '.$this->cleanAppUrl(config('app.url')).'-'.config('app.env').' *****');
111+
$this->info('***** '.$this->cleanAppUrl(Config::get('app.url')).'-'.Config::get('app.env').' *****');
111112
$this->line('Be sure to define a relevant source name by setting APP_URL ');
112113
$this->line('');
113114
$this->line('----- Models tracked -----');
@@ -119,7 +120,7 @@ public function handle()
119120

120121
return 0;
121122
} else {
122-
$this->sendMetricsData($metricsData, config('eloquentize.ELOQUENTIZE_API_TOKEN'));
123+
$this->sendMetricsData($metricsData, Config::get('eloquentize.ELOQUENTIZE_API_TOKEN'));
123124
}
124125

125126
$this->line('Models count data sent to eloquentize.');

src/Commands/ModelsCountLegacy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public function handle()
5353
$this->verbose("Gather metrics from: $date");
5454

5555
// find a way to use the progress bar only if laravel version is >8 disabled for now
56-
//$progress = progress(label: 'Processing date range', steps: count($dateRange));
57-
//$progress->start();
56+
// $progress = progress(label: 'Processing date range', steps: count($dateRange));
57+
// $progress->start();
5858

5959
foreach ($dateRange as $d) {
6060
// Format the date into a string that the command will understand
@@ -74,11 +74,11 @@ public function handle()
7474
]);
7575

7676
// Advance the progress bar after processing each date
77-
//$progress->advance();
77+
// $progress->advance();
7878
}
7979

8080
// Finish the progress bar after processing all dates
81-
//$progress->finish();
81+
// $progress->finish();
8282
return 0;
8383
}
8484
}

src/Commands/PropertyAggregate.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function perform(string $model, AggregationType $aggregation, string $pro
2828
$modelClass = $this->getModelClass($model, $modelsPath);
2929

3030
if (! $this->isModelValid($modelClass, $property)) {
31-
//exit(1);
32-
//echo "\n".$modelClass;
33-
//echo "\n"."Model is not valid";
31+
// exit(1);
32+
// echo "\n".$modelClass;
33+
// echo "\n"."Model is not valid";
3434
return 1;
3535
}
3636

@@ -52,11 +52,11 @@ public function perform(string $model, AggregationType $aggregation, string $pro
5252
}
5353

5454
$count = $query->$method($property) ?? 0;
55-
//echo "handle\n ";
56-
//echo "The ".$method." of ".$model."->".$property." is : ".$count;
55+
// echo "handle\n ";
56+
// echo "The ".$method." of ".$model."->".$property." is : ".$count;
5757
$this->verbose('The '.$method.' of '.$model.'->'.$property.' is : '.$count);
5858
if ($scope && $scopeValue) {
59-
//echo "-- WITH ".$scope."(".$scopeValue.")";
59+
// echo "-- WITH ".$scope."(".$scopeValue.")";
6060
}
6161

6262
$label = $model;

src/Commands/PropertyAggregateLegacy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public function handle()
5151
$this->verbose("Gather metrics from: $date");
5252

5353
// find a way to use the progress bar only if laravel version is >8 disabled for now
54-
//$progress = progress(label: 'Processing date range', steps: count($dateRange));
55-
//$progress->start();
54+
// $progress = progress(label: 'Processing date range', steps: count($dateRange));
55+
// $progress->start();
5656

5757
foreach ($dateRange as $d) {
5858
// Format the date into a string that the command will understand
@@ -74,11 +74,11 @@ public function handle()
7474
]);
7575

7676
// Advance the progress bar after processing each date
77-
//$progress->advance();
77+
// $progress->advance();
7878
}
7979

8080
// Finish the progress bar after processing all dates
81-
//$progress->finish();
81+
// $progress->finish();
8282
return 0;
8383
}
8484
}

src/Commands/Traits/BuildPeriod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function buildPeriod(&$date, $periodType, $dateFormat)
4949

5050
public function getArrayOfDays(Carbon $oldestDate)
5151
{
52-
//This is if you want to get the previous day from today
52+
// This is if you want to get the previous day from today
5353
$yesterday = Carbon::now()->subDay();
5454

5555
$dateRange = [];

src/Commands/Traits/GatherModels.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function gatherModels($filterModels = null, ?string $modelsPath = null):
1818
}
1919
}
2020

21+
// Check if the directory exists
22+
if (! is_dir($modelsDirectory)) {
23+
return [];
24+
}
25+
2126
$files = scandir($modelsDirectory);
2227
$models = [];
2328
foreach ($files as $file) {
@@ -108,12 +113,13 @@ protected function getModelClass(string $model, $modelsPath = null): string
108113
if ($modelsPath !== null) {
109114
// Validate if the custom path is a directory
110115
if (! is_dir(app_path($modelsPath))) {
111-
throw new \Exception("The provided models path is not a valid directory: {$modelsPath}");
116+
$this->error("The provided models path is not a valid directory: {$modelsPath}");
117+
return "App\\{$model}";
112118
}
113119

114120
// Use the custom models path to determine the namespace
115121
$namespace = str_replace('/', '\\', $modelsPath); // Convert path to namespace
116-
//return namespace; // Ensure the namespace is properly formatted
122+
// return namespace; // Ensure the namespace is properly formatted
117123

118124
return "App\\{$namespace}\\{$model}";
119125
}
@@ -130,7 +136,7 @@ protected function isModelValid(string $modelClass, string $column): bool
130136
{
131137

132138
if (! class_exists($modelClass)) {
133-
//$this->defaultErrorMessage();
139+
// $this->defaultErrorMessage();
134140
$this->verbose("Model class $modelClass did not exists.", 'warn');
135141

136142
return false;
@@ -139,28 +145,28 @@ protected function isModelValid(string $modelClass, string $column): bool
139145
try {
140146
$instance = new $modelClass;
141147
} catch (\Throwable $e) {
142-
//$this->defaultErrorMessage();
148+
// $this->defaultErrorMessage();
143149
$this->verbose("Model class $modelClass is not instanciable.", 'warn');
144150

145151
return false;
146152
}
147153

148154
if (! $instance instanceof \Illuminate\Database\Eloquent\Model) {
149-
//$this->defaultErrorMessage();
155+
// $this->defaultErrorMessage();
150156
$this->verbose("Model class $modelClass is not an instance of \Illuminate\Database\Eloquent\Model.", 'warn');
151157

152158
return false;
153159
}
154160

155161
if (! $instance->usesTimestamps()) {
156-
//$this->defaultErrorMessage();
162+
// $this->defaultErrorMessage();
157163
$this->verbose("Model class $modelClass does not use timestamps.", 'warn');
158164

159165
return false;
160166
}
161167

162168
if (! Schema::connection($instance->getConnectionName())->hasColumn($instance->getTable(), $column)) {
163-
//$this->defaultErrorMessage();
169+
// $this->defaultErrorMessage();
164170
$this->verbose("Model class $modelClass does not have a column named ".$column, 'warn');
165171

166172
return false;

src/Commands/Traits/PrepareMetricsData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait PrepareMetricsData
99
protected function prepareMetricsData($metrics, CarbonPeriod $period, $event)
1010
{
1111
return [
12-
//'team_id' => $this->getTeamId(), // Assume this method retrieves the team ID, possibly from a config or environment variable
12+
// 'team_id' => $this->getTeamId(), // Assume this method retrieves the team ID, possibly from a config or environment variable
1313
'start' => $period->getStartDate(),
1414
'end' => $period->getEndDate(),
1515
'timezone' => config('app.timezone') ?? 'UTC',

src/Commands/Traits/SendMetricsData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Eloquentize\LaravelClient\Commands\Traits;
44

5+
use Illuminate\Support\Facades\Config;
56
use Illuminate\Support\Facades\Http;
67

78
trait SendMetricsData
89
{
910
protected function sendMetricsData($data, $token)
1011
{
11-
$url = config('eloquentize.api_url').'/api/metrics/models';
12+
$url = Config::get('eloquentize.api_url').'/api/metrics/models';
1213
try {
1314
$response = Http::acceptJson()->withToken($token)->post($url, $data);
1415

@@ -19,7 +20,7 @@ protected function sendMetricsData($data, $token)
1920
$this->error($response->body(), 'error');
2021
}
2122
} catch (\Exception $e) {
22-
$this->verbose($e->getMessage(), 'error');
23+
$this->error($e->getMessage(), 'error');
2324
}
2425
}
2526
}

src/LaravelClient.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
namespace Eloquentize\LaravelClient;
44

5-
class LaravelClient
6-
{
7-
}
5+
class LaravelClient {}

src/LaravelClientServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function configurePackage(Package $package): void
2323
$package
2424
->name('laravel-eloquentize-client')
2525
->hasConfigFile('eloquentize')
26-
//->hasViews()
27-
//->hasMigration('create_laravel-client_table')
26+
// ->hasViews()
27+
// ->hasMigration('create_laravel-client_table')
2828
->hasCommand(ModelsCount::class)
2929
->hasCommand(ModelsCountLegacy::class)
3030
->hasCommand(ModelCountOverall::class)

0 commit comments

Comments
 (0)