Skip to content

Commit 94fdbff

Browse files
committed
feat: pest testing framework added
1 parent dc8b947 commit 94fdbff

12 files changed

+116
-205
lines changed

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ jobs:
2929
run: composer install --prefer-dist --no-interaction --no-suggest
3030

3131
- name: Execute tests
32-
run: vendor/bin/phpunit --verbose
32+
run: vendor/bin/pest

composer.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^9.5.10",
35-
"mockery/mockery": "^1.4.4"
35+
"mockery/mockery": "^1.4.4",
36+
"orchestra/testbench": "^7.5",
37+
"pestphp/pest": "^1.21",
38+
"pestphp/pest-plugin-laravel": "^1.2"
3639
},
3740
"autoload": {
3841
"psr-4": {
@@ -61,5 +64,10 @@
6164
}
6265
],
6366
"minimum-stability": "dev",
64-
"prefer-stable": true
67+
"prefer-stable": true,
68+
"config": {
69+
"allow-plugins": {
70+
"pestphp/pest-plugin": true
71+
}
72+
}
6573
}

phpunit.xml.dist

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false">
10+
stopOnFailure="false">
1211
<testsuites>
1312
<testsuite name="Package Test Suite">
1413
<directory suffix=".php">./tests/</directory>
1514
</testsuite>
1615
</testsuites>
16+
17+
<php>
18+
<env name="APP_KEY" value="BckfSJCXivnK6r38GVIWUAxmbBSjTsmF"/>
19+
</php>
1720
</phpunit>

src/Commands/RollbackGeneratorCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function handle()
6363
{
6464
$type = $this->argument('type');
6565
if (!in_array($type, ['api', 'scaffold', 'api_scaffold'])) {
66-
$this->error('invalid rollback type');
66+
$this->error('Invalid rollback type');
6767

6868
return 1;
6969
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use InfyOm\Generator\Commands\Publish\PublishTablesCommand;
4+
use function Pest\Laravel\artisan;
5+
6+
it('thrown exceptions with invalid type passed', function () {
7+
artisan(PublishTablesCommand::class, ['type' => 'invalid']);
8+
})->throws(Exception::class, 'Invalid Table Type');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use InfyOm\Generator\Commands\RollbackGeneratorCommand;
4+
use function Pest\Laravel\artisan;
5+
6+
it('fails with invalid rollback type', function () {
7+
artisan(RollbackGeneratorCommand::class, ['model' => 'User', 'type' => 'random'])
8+
->assertExitCode(1);
9+
});

tests/Helpers/HelpersTest.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@
22

33
namespace Tests\Helpers;
44

5-
use PHPUnit\Framework\TestCase;
6-
7-
class HelpersTest extends TestCase
8-
{
9-
public function test_model_name_from_table_name()
10-
{
11-
$tableNames = ['posts', 'person_addresses', 'personEmails'];
12-
$modelNames = ['Post', 'PersonAddress', 'PersonEmail'];
13-
14-
$i = 0;
15-
foreach ($tableNames as $tableName) {
16-
$result = model_name_from_table_name($tableName);
17-
$this->assertEquals($modelNames[$i], $result);
18-
$i++;
19-
}
5+
it('verifies model name from table names', function () {
6+
$tableNames = ['posts', 'person_addresses', 'personEmails'];
7+
$modelNames = ['Post', 'PersonAddress', 'PersonEmail'];
8+
9+
$i = 0;
10+
foreach ($tableNames as $tableName) {
11+
$result = model_name_from_table_name($tableName);
12+
expect($result)->toBe($modelNames[$i]);
13+
$i++;
2014
}
21-
}
15+
});

tests/Pest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
uses(Tests\TestCase::class)->in(__DIR__);
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Expectations
19+
|--------------------------------------------------------------------------
20+
|
21+
| When you're writing tests, you often need to check that values meet certain conditions. The
22+
| "expect()" function gives you access to a set of "expectations" methods that you can use
23+
| to assert different things. Of course, you may extend the Expectation API at any time.
24+
|
25+
*/
26+
27+
expect()->extend('toBeOne', function () {
28+
return $this->toBe(1);
29+
});
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Functions
34+
|--------------------------------------------------------------------------
35+
|
36+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
37+
| project that you don't want to repeat in every file. Here you can also expose helpers as
38+
| global functions to help you to reduce the number of lines of code in your test files.
39+
|
40+
*/
41+

tests/TestCase.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use InfyOm\Generator\InfyOmGeneratorServiceProvider;
6+
use Orchestra\Testbench\TestCase as Orchestra;
7+
8+
class TestCase extends Orchestra
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [
13+
InfyOmGeneratorServiceProvider::class,
14+
];
15+
}
16+
}

tests/Utils/FileUtilTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use InfyOm\Generator\Utils\FileUtil;
4+
5+
it('mocks create file', function () {
6+
$mock = \Mockery::mock('alias:'.FileUtil::class);
7+
8+
$mock->shouldReceive('createFile')
9+
->withArgs([__DIR__, 'test.php', 'test'])
10+
->andReturn(true);
11+
12+
$result = FileUtil::createFile(__DIR__, 'test.php', 'test');
13+
14+
$this->assertEquals(true, $result);
15+
});

tests/Utils/GeneratorFieldsInputUtilTest.php

-139
This file was deleted.

tests/Utils/ResponseUtilTest.php

-44
This file was deleted.

0 commit comments

Comments
 (0)