Skip to content

Commit 5e13c65

Browse files
Merge pull request #4 from 42coders/add-tests
Add tests
2 parents 2bc9662 + 0c2aa6a commit 5e13c65

File tree

8 files changed

+257
-3
lines changed

8 files changed

+257
-3
lines changed

.github/workflows/run_tests.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Testing
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
laravel-tests:
7+
runs-on: ubuntu-latest
8+
services:
9+
mysql:
10+
image: mysql:8
11+
env:
12+
MYSQL_ROOT_PASSWORD: root
13+
MYSQL_DATABASE: tlap_test
14+
ports:
15+
- 3306:3306
16+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8
23+
- name: Install Dependencies
24+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
25+
- name: Execute tests (Unit and Feature tests) via PHPUnit
26+
run: vendor/bin/phpunit

phpunit.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@
2626
<log type="coverage-text" target="build/coverage.txt"/>
2727
<log type="coverage-clover" target="build/logs/clover.xml"/>
2828
</logging>
29+
<php>
30+
<env name="DB_CONNECTION" value="mysql"/>
31+
<env name="DB_HOST" value="127.0.0.1"/>
32+
<env name="DB_PORT" value="3306"/>
33+
<env name="DB_DATABASE" value="tlap_test"/>
34+
<env name="DB_USERNAME" value="root"/>
35+
<env name="DB_PASSWORD" value="root"/>
36+
</php>
2937
</phpunit>

src/Traits/TLAPAdminTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ public static function validation():array
141141

142142
$columns = self::getTLAPTableStructure();
143143

144+
144145
foreach($columns as $columnName => $column){
145-
if($column['nullable'] === 'NO'){
146+
if($column['nullable'] === 'NO' && $column['key'] !== 'PRI'){
146147
$validation[$columnName] = 'required';
147148
}
148149
}

tests/ExampleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace MaxHutschenreiter\TLAP\Tests;
3+
namespace the42coders\TLAP\Tests;
44

55
use Orchestra\Testbench\TestCase;
6-
use MaxHutschenreiter\TLAP\TLAPServiceProvider;
6+
use the42coders\TLAP\TLAPServiceProvider;
77

88
class ExampleTest extends TestCase
99
{

tests/Integration/TestCase.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace the42coders\TLAP\Tests\Integration;
4+
5+
use Illuminate\Encryption\Encrypter;
6+
use Illuminate\Support\Facades\Route;
7+
use Illuminate\Support\Facades\View;
8+
use the42coders\TLAP\TLAP;
9+
10+
class TestCase extends \Orchestra\Testbench\TestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->loadMigrationsFrom('tests/database/migrations');
17+
$this->loadDefaultConfig();
18+
$this->setUpViews();
19+
$this->generateAppKey();
20+
$this->setUpRoutes();
21+
}
22+
23+
protected function loadDefaultConfig(): void
24+
{
25+
config(['tlap' => $this->getDefaultConfig()]);
26+
}
27+
28+
protected function getDefaultConfig(): array
29+
{
30+
return include __DIR__ . '/../../config/config.php';
31+
}
32+
33+
protected function setUpViews(): void
34+
{
35+
View::addNamespace('tlap', __DIR__ . '/../../resources/views');
36+
View::addLocation(__DIR__ . '/../../resources/views');
37+
}
38+
39+
protected function generateAppKey(): void
40+
{
41+
$key = base64_encode(Encrypter::generateKey('AES-256-CBC'));
42+
config(['app.key' => 'base64:' . $key]);
43+
}
44+
45+
protected function setUpRoutes(): void
46+
{
47+
Route::middleware('web')->group(function () {
48+
TLAP::routes();
49+
});
50+
}
51+
}

tests/Integration/UserCrudTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace the42coders\TLAP\Tests\Integration;
4+
5+
use the42coders\TLAP\Tests\models\User;
6+
7+
class UserCrudTest extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
parent::setUp();
12+
config(['tlap.models.users' => User::class]);
13+
}
14+
15+
public function testItCanListUsers()
16+
{
17+
$user = User::create([
18+
'name' => 'Test User',
19+
'email' => 'user@test.com',
20+
'password' => 'secret'
21+
]);
22+
23+
$this->get(route('tlap.index', ['models' => User::getModelPluralName()]))
24+
->assertOk();
25+
26+
}
27+
28+
public function testItCanCreateUser()
29+
{
30+
$userData = [
31+
'name' => 'Test User',
32+
'email' => 'user@test.com',
33+
'password' => 'pass'
34+
];
35+
36+
$this->post(route('tlap.store', ['models' => User::getModelPluralName()]), $userData)
37+
->assertRedirect();
38+
39+
unset($userData['password']);
40+
$this->assertDatabaseHas('users', $userData);
41+
}
42+
43+
public function testItCanShowUser()
44+
{
45+
/** @var User $user */
46+
$user = User::create([
47+
'name' => 'Test User',
48+
'email' => 'user@test.com',
49+
'password' => 'secret'
50+
]);
51+
52+
$this->get(route('tlap.show', ['models' => User::getModelPluralName(), 'id' => $user->id]))
53+
->assertOk()
54+
->assertSee($user->name);
55+
}
56+
57+
public function testItCanShowUserCreateForm()
58+
{
59+
$this->get(route('tlap.create', ['models' => User::getModelPluralName()]))
60+
->assertOk()
61+
->assertSee('name');
62+
}
63+
64+
public function testItCanShowUserEditForm()
65+
{
66+
/** @var User $user */
67+
$user = User::create([
68+
'name' => 'Test User',
69+
'email' => 'user@test.com',
70+
'password' => 'secret'
71+
]);
72+
73+
$this->get(route('tlap.edit', ['models' => User::getModelPluralName(), 'id' => $user->id]))
74+
->assertOk()
75+
->assertSee('name')
76+
->assertSee($user->name);
77+
}
78+
79+
public function testItCanUpdateUser()
80+
{
81+
/** @var User $user */
82+
$user = User::create([
83+
'name' => 'Test User',
84+
'email' => 'user@test.com',
85+
'password' => 'secret'
86+
]);
87+
88+
$userData = [
89+
'name' => 'Test User Updated',
90+
'email' => 'user@test.com',
91+
'password' => 'pass'
92+
];
93+
94+
$this->post(route('tlap.update', ['models' => User::getModelPluralName(), 'id' => $user->id]), $userData)
95+
->assertRedirect();
96+
97+
unset($userData['password']);
98+
$this->assertDatabaseHas('users', $userData);
99+
}
100+
101+
public function testItCanDeleteUser()
102+
{
103+
/** @var User $user */
104+
$user = User::create([
105+
'name' => 'Test User',
106+
'email' => 'user@test.com',
107+
'password' => 'secret'
108+
]);
109+
110+
$this->assertModelExists($user);
111+
112+
$this->get(route('tlap.delete', ['models' => User::getModelPluralName(), 'id' => $user->id]))
113+
->assertRedirect();
114+
115+
$this->assertModelMissing($user);
116+
}
117+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use App\Enums\UserRoles;
4+
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class CreateUsersTable extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
Schema::create('users', function (Blueprint $table) {
18+
$table->id();
19+
$table->string('name');
20+
$table->string('email')->unique();
21+
$table->timestamp('email_verified_at')->nullable();
22+
$table->string('password');
23+
$table->rememberToken();
24+
$table->timestamps();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::dropIfExists('users');
36+
}
37+
}

tests/models/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace the42coders\TLAP\Tests\models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use the42coders\TLAP\Traits\TLAPAdminTrait;
7+
8+
class User extends Model
9+
{
10+
use TLAPAdminTrait;
11+
12+
protected $guarded = [];
13+
14+
}

0 commit comments

Comments
 (0)