Skip to content

Commit

Permalink
Merge pull request #100 from mblsolutions/feature/laravel-v10-upgrade
Browse files Browse the repository at this point in the history
Feature/laravel v10 upgrade AB#35895
  • Loading branch information
hayleyberryl2s authored Jan 22, 2024
2 parents f07f674 + 82b0bff commit b2bbbdf
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 33 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/laravel-report-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ jobs:
strategy:
fail-fast: true
matrix:
php: ["7.4", "8.0", "8.1"]
laravel: ["^8.0", "^9.0"]
exclude:
- php: "7.4"
laravel: "^9.0"
php: ["8.0", "8.1"]
laravel: ["^8.0", "^9.0", "^10.0"]

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ code_coverage
composer.lock
package.lock
phpunit.xml
.phpunit.result.cache
.phpunit.result.cache
.vscode
21 changes: 10 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Laravel Report package",
"keywords": ["redu", "report", "laravel"],
"type": "package",
"license": "MIT",
"license": "proprietary",
"authors": [
{
"name": "MBL Solutions Ltd.",
Expand All @@ -13,17 +13,16 @@
"require": {
"php": "^7.4||^8.0",
"ext-json": "*",
"illuminate/cache": "^8.0||^9.0",
"illuminate/console": "^8.0||^9.0",
"illuminate/contracts": "^8.0||^9.0",
"illuminate/database": "^8.0||^9.0",
"illuminate/events": "^8.0||^9.0",
"illuminate/filesystem": "^8.0||^9.0",
"illuminate/queue": "^8.0||^9.0",
"illuminate/support": "^8.0||^9.0",
"illuminate/http": "^8.0||^9.0",
"illuminate/cache": "^8.0||^9.0||^10",
"illuminate/console": "^8.0||^9.0||^10",
"illuminate/contracts": "^8.0||^9.0||^10",
"illuminate/database": "^8.0||^9.0||^10",
"illuminate/events": "^8.0||^9.0||^10",
"illuminate/filesystem": "^8.0||^9.0||^10",
"illuminate/queue": "^8.0||^9.0||^10",
"illuminate/support": "^8.0||^9.0||^10",
"illuminate/http": "^8.0||^9.0||^10",
"maatwebsite/excel": "^3.1",
"lerouse/laravel-repository": "^1.0||^2.0",
"laravel/legacy-factories": "^1.2",
"doctrine/dbal": "^3.3"
},
Expand Down
14 changes: 3 additions & 11 deletions src/Console/Commands/DispatchScheduledReportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace MBLSolutions\Report\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
Expand All @@ -15,26 +14,19 @@

class DispatchScheduledReportsCommand extends Command
{
protected \Carbon\Carbon $date;

protected $signature = 'report:schedule';

protected $description = 'Dispatch scheduled reports';

public function __construct()
{
parent::__construct();

$this->date = Carbon::now();
}

public function handle(): int
{
$date = now();

$repository = new ScheduledReportRepository();

$this->info('Running Report Schedules');

$repository->getScheduledReportsToRun($this->date)->each(function (ScheduledReport $schedule) {
$repository->getScheduledReportsToRun($date)->each(function (ScheduledReport $schedule) {
$this->info(
sprintf('Running Schedule for Report %s', $schedule->getKey())
);
Expand Down
194 changes: 192 additions & 2 deletions src/Repositories/PackageReportRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

namespace MBLSolutions\Report\Repositories;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Lerouse\LaravelRepository\EloquentRepository;
use Illuminate\Support\Collection;
use MBLSolutions\Report\Support\HandlesAuthenticatable;

abstract class PackageReportRepository extends EloquentRepository
abstract class PackageReportRepository
{
use HandlesAuthenticatable;

/** @var string */
protected $key = 'id';

/** @var int */
protected $paginationLimit = 25;

protected function authNameQuery(Builder $builder, Model $authModel, string $join): Builder
{
return $builder->selectRaw(
Expand All @@ -20,4 +28,186 @@ protected function authNameQuery(Builder $builder, Model $authModel, string $joi
);
}

/**
* Get all Models
*
* @return EloquentCollection
*/
public function all(): EloquentCollection
{
return $this->builder()->get();
}

/**
* Get all models as Paginated Results
*
* @param int|null $limit
* @return LengthAwarePaginator
*/
public function paginate(int $limit = null): LengthAwarePaginator
{
return $this->builder()->paginate($limit ?? $this->paginationLimit);
}

/**
* Find a Model by its Primary Key
*
* @param mixed $id
* @param bool $fail
* @return mixed
*/
public function find($id, bool $fail = true)
{
if ($fail) {
return $this->builder()->findOrFail($id);
}

return $this->builder()->find($id);
}

/**
* Find Many models by its primary Key
*
* @param array $ids
* @param string|null $column
* @return Collection
*/
public function findMany(array $ids, string $column = null): Collection
{
$column = $column ?? $this->getModelPrimaryKey();

return $this->builder()->whereIn($this->getModelTable() . '.' . $column, $ids)->get();
}

/**
* Create a new Model
*
* @param array $data
* @return mixed
*/
public function create(array $data)
{
$model = $this->builder()->create($data);

return $model;
}

/**
* Create a collection of new Models
*
* @param array $data
* @return bool
*/
public function createMany(array $data): bool
{
$result = $this->builder()->getModel()->insert($data);

return $result;
}

/**
* Update a Model by its Primary Key
*
* @param array $data
* @param $id
* @return mixed
*/
public function update(array $data, $id)
{
$model = $this->find($id);

$model->update($data);

$model = $model->fresh();

return $model;
}

/**
* Update a collection of Models
*
* @param array $data
* @param array $ids
* @param string|null $column
* @return bool
*/
public function updateMany(array $data, array $ids, string $column = null): bool
{
$column = $column ?? $this->getModelPrimaryKey();

$result = $this->builder()->whereIn($this->getModelTable() . '.' . $column, $ids)->update($data);

return $result;
}

/**
* Delete a Model by its Primary Key
*
* @param $id
* @return mixed
*/
public function delete($id)
{
$model = $this->find($id);

$model->delete();

return $model->fresh();
}

/**
* Delete a collection of Models by their Primary Keys
*
* @param array $ids
* @param string|null $column
* @return mixed
*/
public function deleteMany(array $ids, string $column = null): void
{
$column = $column ?? $this->getModelPrimaryKey();

$this->builder()->whereIn($this->getModelTable() . '.' . $column, $ids)->delete();
}

/**
* Get the next Auto Increment on table
*
* @param string|null $column
* @return int
*/
public function getNextAutoIncrement(string $column = null): int
{
$column = $column ?? $this->getModelPrimaryKey();

$latest = $this->builder()->orderBy($this->getModelTable() . '.' . $column, 'desc')->first();

return $latest ? ($latest->getKey() + 1) : 1;
}

/**
* Get the Models Primary Key
*
* @return string|null
*/
protected function getModelPrimaryKey(): string
{
return $this->builder()->getModel()->getKey() ?? $this->key;
}

/**
* Get the Models Table
*
* @return string
*/
protected function getModelTable(): string
{
return $this->builder()->getModel()->getTable();
}

/**
* Get the Repository Query Builder
*
* @return Builder
*/
abstract public function builder(): Builder;
}
3 changes: 2 additions & 1 deletion tests/LaravelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MBLSolutions\Report\Tests;

use Faker\Factory;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -95,6 +94,8 @@ private function runPackageMigrations(): void
private function loadTestMigrations(): void
{
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');

$this->artisan('migrate')->run();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ protected function setUp(): void
/** @test **/
public function can_run_daily_schedule(): void
{
Carbon::setTestNow('2021-11-10 00:00:00'); // daily at midnight (Wednesday)
$testTime = now();
$testTime->setTime(0,0,0);

Carbon::setTestNow($testTime);

$this->artisan('report:schedule');

$this->assertScheduledEventDispatch('c066524b-b5ad-46d1-961d-f96203c54181');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected function setUp(): void

Bus::fake();

$testNow = now();
$testNow->setTime(0,0,0);

Carbon::setTestNow($testNow);

$this->report = factory(Report::class)->create();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
return [
'name' => $faker->words(3, true),
'description' => $faker->sentence(),
'connection' => 'sqlite',
'connection' => 'testing',
'display_limit' => 25,
'table' => 'users',
'where' => null,
Expand Down

0 comments on commit b2bbbdf

Please sign in to comment.