-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Additional tests * fixes * Additional tests Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
- Loading branch information
Showing
15 changed files
with
495 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
namespace EscolaLms\Core\Tests\Api; | ||
|
||
use Carbon\Carbon; | ||
use EscolaLms\Core\Enums\StatusEnum; | ||
use EscolaLms\Core\Tests\Mocks\ExampleEntity\ExampleEntity; | ||
use EscolaLms\Core\Tests\Mocks\ExampleEntity\TestCase; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
|
||
class CriteriaApiTest extends TestCase | ||
{ | ||
use DatabaseTransactions, WithFaker; | ||
|
||
public function testPagination() | ||
{ | ||
ExampleEntity::factory() | ||
->count(10) | ||
->create(); | ||
|
||
$response = $this->json('GET', 'api/core?limit=5'); | ||
$response->assertJsonCount(5, 'data'); | ||
} | ||
|
||
public function testDateCriterion() | ||
{ | ||
$date = Carbon::now()->addDays(5); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['created_at' => $date]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(); | ||
|
||
$response = $this->json('GET', 'api/core?gt_created_at=' . $date); | ||
|
||
$response->assertJsonCount(5, 'data'); | ||
} | ||
|
||
public function testLikeCriterion() | ||
{ | ||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['name' => 'simple name']); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['name' => $this->faker->word]); | ||
|
||
$response = $this->json('GET', 'api/core?name=simple'); | ||
$response->assertJsonCount(5, 'data'); | ||
} | ||
|
||
public function testInCriterion() | ||
{ | ||
$ids = ExampleEntity::factory() | ||
->count(5) | ||
->create() | ||
->pluck('id') | ||
->map(fn ($item) => 'ids[]=' . $item . '&'); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(); | ||
|
||
$response = $this->json('GET', 'api/core?' . $ids->implode('')); | ||
$response->assertJsonCount(5, 'data'); | ||
} | ||
|
||
public function testIsNullCriterion() | ||
{ | ||
ExampleEntity::factory() | ||
->count(5) | ||
->create(); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['description' => null]); | ||
|
||
$response = $this->json('GET', 'api/core?nullable=1'); | ||
$response->assertJsonCount(5, 'data'); | ||
} | ||
|
||
public function testEqualsCriterion() | ||
{ | ||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['status' => StatusEnum::INACTIVE]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['status' => StatusEnum::ACTIVE]); | ||
|
||
$response = $this->json('GET', 'api/core?status=' . StatusEnum::ACTIVE); | ||
$response->assertJsonCount(5, 'data'); | ||
} | ||
|
||
public function testPeriodCriterion() | ||
{ | ||
$dateFrom = Carbon::now(); | ||
$dateTo = Carbon::now()->addDays(5); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => Carbon::now()->subYear()]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => $dateFrom]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => $dateTo]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => Carbon::now()->addYear()]); | ||
|
||
$response = $this->json('GET', 'api/core?from=' . $dateFrom . '&to=' . $dateTo); | ||
$response->assertJsonCount(10, 'data'); | ||
} | ||
|
||
public function testPeriodDto() | ||
{ | ||
$dateFrom = Carbon::now(); | ||
$dateTo = Carbon::now()->addDays(5); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => Carbon::now()->subYear()]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => $dateFrom]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => $dateTo]); | ||
|
||
ExampleEntity::factory() | ||
->count(5) | ||
->create(['date_time' => Carbon::now()->addYear()]); | ||
|
||
$response = $this->json('GET', 'api/core/period?from=' . $dateFrom . '&to=' . $dateTo); | ||
$response->assertJsonCount(10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace EscolaLms\Core\Tests\Mocks\ExampleEntity; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ExampleEntity extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'example_entities'; | ||
|
||
protected $guarded = ['id']; | ||
|
||
protected static function newFactory(): ExampleEntityFactory | ||
{ | ||
return ExampleEntityFactory::new(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
namespace EscolaLms\Core\Tests\Mocks\ExampleEntity; | ||
|
||
use EscolaLms\Core\Dtos\OrderDto; | ||
use EscolaLms\Core\Dtos\PaginationDto; | ||
use EscolaLms\Core\Dtos\PeriodDto; | ||
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class ExampleEntityController extends EscolaLmsBaseController | ||
{ | ||
private ExampleEntityService $service; | ||
|
||
public function __construct(ExampleEntityService $service) | ||
{ | ||
$this->service = $service; | ||
} | ||
|
||
public function index(Request $request): JsonResponse | ||
{ | ||
$orderDto = OrderDto::instantiateFromRequest($request); | ||
$paginationDto = PaginationDto::instantiateFromRequest($request); | ||
$searchDto = ExampleEntitySearchDto::instantiateFromRequest($request); | ||
|
||
$results = $this->service->getExampleEntities($searchDto, $paginationDto, $orderDto); | ||
|
||
return new JsonResponse($results); | ||
} | ||
|
||
public function period(Request $request): JsonResponse | ||
{ | ||
$periodDto = PeriodDto::instantiateFromRequest($request); | ||
|
||
$results = $this->service->getByPeriod($periodDto); | ||
|
||
return new JsonResponse($results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace EscolaLms\Core\Tests\Mocks\ExampleEntity; | ||
|
||
use EscolaLms\Core\Enums\StatusEnum; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class ExampleEntityFactory extends Factory | ||
{ | ||
protected $model = ExampleEntity::class; | ||
|
||
public function definition() | ||
{ | ||
return [ | ||
'name' => $this->faker->word(), | ||
'description' => $this->faker->words(3, true), | ||
'status' => $this->faker->randomElement(StatusEnum::asArray()), | ||
'date_time' => $this->faker->dateTimeBetween(), | ||
]; | ||
} | ||
} |
Oops, something went wrong.