Skip to content

Commit a55c307

Browse files
committed
tests: using pest
1 parent 224f588 commit a55c307

File tree

8 files changed

+97
-41
lines changed

8 files changed

+97
-41
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ jobs:
4242
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
4343

4444
- name: Execute tests
45-
run: vendor/bin/phpunit
45+
run: vendor/bin/pest

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"livewire/livewire": "^2.11.2|^3.0.1",
2323
"openspout/openspout": "^4.12.1",
2424
"phpoffice/phpspreadsheet": "^1.27",
25-
"yajra/laravel-datatables-buttons": "^11.0"
25+
"yajra/laravel-datatables-buttons": "^11.0",
26+
"pestphp/pest": "^2.34",
27+
"pestphp/pest-plugin-laravel": "^2.3"
2628
},
2729
"require-dev": {
2830
"larastan/larastan": "^2.9.1",
@@ -41,7 +43,7 @@
4143
}
4244
},
4345
"scripts": {
44-
"test": "./vendor/bin/phpunit",
46+
"test": "./vendor/bin/pest",
4547
"pint": "./vendor/bin/pint",
4648
"rector": "./vendor/bin/rector",
4749
"stan": "./vendor/bin/phpstan analyse --memory-limit=2G --ansi --no-progress --no-interaction --configuration=phpstan.neon.dist",
@@ -55,7 +57,10 @@
5557
"config": {
5658
"preferred-install": "dist",
5759
"sort-packages": true,
58-
"optimize-autoloader": true
60+
"optimize-autoloader": true,
61+
"allow-plugins": {
62+
"pestphp/pest-plugin": true
63+
}
5964
},
6065
"extra": {
6166
"branch-alias": {

phpunit.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
</phpunit>

src/Jobs/DataTableExportJob.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ class DataTableExportJob implements ShouldBeUnique, ShouldQueue
4545

4646
/**
4747
* Create a new job instance.
48-
*
49-
* @param int|string $user
5048
*/
51-
public function __construct(array $dataTable, public array $request, public $user, public string $sheetName = 'Sheet1')
52-
{
49+
public function __construct(
50+
array $dataTable,
51+
public array $request,
52+
public ?int $user,
53+
public string $sheetName = 'Sheet1'
54+
) {
5355
$this->dataTable = $dataTable[0];
5456
$this->attributes = $dataTable[1];
5557
}
@@ -64,7 +66,7 @@ public function __construct(array $dataTable, public array $request, public $use
6466
* @throws \OpenSpout\Writer\Exception\WriterNotOpenedException
6567
* @throws \OpenSpout\Writer\Exception\InvalidSheetNameException
6668
*/
67-
public function handle()
69+
public function handle(): void
6870
{
6971
if ($this->user) {
7072
Event::forget(Login::class);

tests/ExportTest.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/Feature/ExportTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\DB;
4+
5+
test('it can export to excel', function () {
6+
$this->get('/users')->assertOk();
7+
$batchId = $this->getAjax('/users?action=exportQueue')->getContent();
8+
9+
$this->assertTrue(DB::table('job_batches')->where('id', $batchId)->exists());
10+
});

tests/Pest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
|
8+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10+
| need to change it using the "uses()" function to bind a different classes or traits.
11+
|
12+
*/
13+
14+
use Illuminate\Foundation\Testing\RefreshDatabase;
15+
use Yajra\DataTables\Exports\Tests\TestCase;
16+
17+
uses(TestCase::class, RefreshDatabase::class)->in('Feature');
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Expectations
22+
|--------------------------------------------------------------------------
23+
|
24+
| When you're writing tests, you often need to check that values meet certain conditions. The
25+
| "expect()" function gives you access to a set of "expectations" methods that you can use
26+
| to assert different things. Of course, you may extend the Expectation API at any time.
27+
|
28+
*/
29+
30+
expect()->extend('toBeOne', function () {
31+
return $this->toBe(1);
32+
});
33+
34+
/*
35+
|--------------------------------------------------------------------------
36+
| Functions
37+
|--------------------------------------------------------------------------
38+
|
39+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
40+
| project that you don't want to repeat in every file. Here you can also expose helpers as
41+
| global functions to help you to reduce the number of lines of code in your test files.
42+
|
43+
*/
44+
45+
function something()
46+
{
47+
// ..
48+
}

tests/TestCase.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Yajra\DataTables\Exports\Tests;
44

55
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\DB;
67
use Illuminate\Support\Facades\View;
78
use Illuminate\Testing\TestResponse;
89
use Orchestra\Testbench\TestCase as BaseTestCase;
10+
use Yajra\DataTables\Exports\Tests\DataTables\UsersDataTable;
911
use Yajra\DataTables\Exports\Tests\Models\User;
1012

1113
abstract class TestCase extends BaseTestCase
@@ -29,10 +31,16 @@ protected function setUp(): void
2931
$this->seedDatabase();
3032
}
3133

34+
protected function defineRoutes($router): void
35+
{
36+
$router->get('/users', function (UsersDataTable $dataTable) {
37+
return $dataTable->render('tests::users');
38+
});
39+
}
40+
3241
protected function migrateDatabase(): void
3342
{
34-
/** @var \Illuminate\Database\Schema\Builder $schemaBuilder */
35-
$schemaBuilder = $this->app['db']->connection()->getSchemaBuilder();
43+
$schemaBuilder = DB::getSchemaBuilder();
3644
if (! $schemaBuilder->hasTable('users')) {
3745
$schemaBuilder->create('users', function (Blueprint $table) {
3846
$table->increments('id');
@@ -83,7 +91,7 @@ protected function seedDatabase(): void
8391
*
8492
* @param \Illuminate\Foundation\Application $app
8593
*/
86-
protected function getEnvironmentSetUp($app)
94+
protected function getEnvironmentSetUp($app): void
8795
{
8896
$app['config']->set('app.debug', true);
8997
$app['config']->set('queue.default', 'sync');

0 commit comments

Comments
 (0)