Skip to content

Commit 77f7126

Browse files
authored
Merge pull request #47 from alexmanase/convert-all-tests-to-pest
Convert all tests to Pest
2 parents 8cc6567 + 68ba4bf commit 77f7126

8 files changed

+148
-187
lines changed

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ jobs:
4141
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
4242
4343
- name: Execute tests
44-
run: vendor/bin/phpunit
44+
run: vendor/bin/pest

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ docs
44
vendor
55
coverage
66
.php-cs-fixer.cache
7+
.phpunit.result.cache

.phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::to":4,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::it":4},"times":{"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelMakeCommandTest.php::it":0.003,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::public":0.002,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::values":0.002,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::callables":0.002,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::ignored":0.002,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::to":0.003,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::magic":0.002,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-view-models\/tests\/ViewModelTest.php::it":0.003}}

composer.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"require-dev": {
2323
"orchestra/testbench": "^6.23|^7.0",
24-
"phpunit/phpunit": "^9.4"
24+
"pestphp/pest": "^1.22"
2525
},
2626
"autoload": {
2727
"psr-4": {
@@ -41,13 +41,16 @@
4141
}
4242
},
4343
"scripts": {
44-
"test": "vendor/bin/phpunit",
45-
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
44+
"test": "vendor/bin/pest",
45+
"test-coverage": "vendor/bin/pest --coverage-html coverage",
4646
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
4747
},
4848
"config": {
49-
"sort-packages": true
49+
"sort-packages": true,
50+
"allow-plugins": {
51+
"pestphp/pest-plugin": true
52+
}
5053
},
5154
"minimum-stability": "dev",
5255
"prefer-stable": true
53-
}
56+
}

tests/Pest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Symfony\Component\HttpFoundation\Response;
4+
use Illuminate\Http\Request;
5+
6+
uses(Spatie\ViewModels\Tests\TestCase::class)->in('.');
7+
8+
// Functions
9+
10+
function createRequest(array $headers = []): Request
11+
{
12+
$request = Request::create('/', 'GET', [], [], [], [], []);
13+
14+
foreach ($headers as $header => $value) {
15+
$request->headers->set($header, $value);
16+
}
17+
18+
return $request;
19+
}
20+
21+
function getResponseBody(Response $response): array
22+
{
23+
return json_decode($response->getContent(), true);
24+
}

tests/TestCase.php

+1-19
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,17 @@
22

33
namespace Spatie\ViewModels\Tests;
44

5-
use Illuminate\Http\Request;
65
use Illuminate\Support\Facades\View;
76
use Orchestra\Testbench\TestCase as OrchestraTestCase;
87
use Spatie\ViewModels\Providers\ViewModelsServiceProvider;
9-
use Symfony\Component\HttpFoundation\Response;
108

119
class TestCase extends OrchestraTestCase
1210
{
1311
protected function setUp(): void
1412
{
1513
parent::setUp();
1614

17-
View::addLocation(__DIR__.'/resources/views');
18-
}
19-
20-
protected function createRequest(array $headers = []): Request
21-
{
22-
$request = Request::create('/', 'GET', [], [], [], [], []);
23-
24-
foreach ($headers as $header => $value) {
25-
$request->headers->set($header, $value);
26-
}
27-
28-
return $request;
29-
}
30-
31-
protected function getResponseBody(Response $response): array
32-
{
33-
return json_decode($response->getContent(), true);
15+
View::addLocation(__DIR__ . '/resources/views');
3416
}
3517

3618
protected function getPackageProviders($app)

tests/ViewModelMakeCommandTest.php

+27-40
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,42 @@
11
<?php
22

3-
namespace Spatie\ViewModels\Tests;
4-
53
use Illuminate\Support\Facades\Artisan;
64
use Illuminate\Support\Facades\File;
75

8-
class ViewModelMakeCommandTest extends TestCase
9-
{
10-
/** @test */
11-
public function it_can_create_a_view_model()
12-
{
13-
$exitCode = Artisan::call('make:view-model', [
14-
'name' => 'HomeViewModel',
15-
'--force' => true,
16-
]);
17-
18-
$this->assertEquals(0, $exitCode);
19-
20-
$this->assertMatchesRegularExpression('~ViewModel( \[.+\])? created successfully~', Artisan::output());
21-
22-
$shouldOutputFilePath = $this->app['path'].'/ViewModels/HomeViewModel.php';
23-
24-
$this->assertTrue(File::exists($shouldOutputFilePath), 'File exists in default app/ViewModels folder');
25-
26-
$contents = File::get($shouldOutputFilePath);
6+
use function PHPUnit\Framework\assertTrue;
277

28-
$this->assertStringContainsString('namespace App\ViewModels;', $contents);
8+
it('can create a view model', function () {
9+
$exitCode = Artisan::call('make:view-model', [
10+
'name' => 'HomeViewModel',
11+
'--force' => true,
12+
]);
2913

30-
$this->assertStringContainsString('class HomeViewModel extends ViewModel', $contents);
31-
}
14+
expect($exitCode)->toEqual(0)
15+
->and(Artisan::output())->toMatch('~ViewModel( \[.+\])? created successfully~');
3216

33-
/** @test */
34-
public function it_can_create_a_view_model_with_a_custom_namespace()
35-
{
36-
$exitCode = Artisan::call('make:view-model', [
37-
'name' => 'Blog/PostsViewModel',
38-
'--force' => true,
39-
]);
17+
$shouldOutputFilePath = $this->app['path'] . '/ViewModels/HomeViewModel.php';
4018

41-
$this->assertEquals(0, $exitCode);
19+
assertTrue(File::exists($shouldOutputFilePath), 'File exists in default app/ViewModels folder');
4220

43-
$this->assertMatchesRegularExpression('~ViewModel( \[.+\])? created successfully~', Artisan::output());
21+
expect(File::get($shouldOutputFilePath))
22+
->toContain('namespace App\ViewModels;')
23+
->toContain('class HomeViewModel extends ViewModel');
24+
});
4425

45-
$shouldOutputFilePath = $this->app['path'].'/ViewModels/Blog/PostsViewModel.php';
26+
it('can create a view model with a custom namespace', function () {
27+
$exitCode = Artisan::call('make:view-model', [
28+
'name' => 'Blog/PostsViewModel',
29+
'--force' => true,
30+
]);
4631

47-
$this->assertTrue(File::exists($shouldOutputFilePath), 'File exists in custom app/Blog folder');
32+
expect($exitCode)->toEqual(0)
33+
->and(Artisan::output())->toMatch('~ViewModel( \[.+\])? created successfully~');
4834

49-
$contents = File::get($shouldOutputFilePath);
35+
$shouldOutputFilePath = $this->app['path'] . '/ViewModels/Blog/PostsViewModel.php';
5036

51-
$this->assertStringContainsString('namespace App\ViewModels\Blog;', $contents);
37+
assertTrue(File::exists($shouldOutputFilePath), 'File exists in custom app/Blog folder');
5238

53-
$this->assertStringContainsString('class PostsViewModel extends ViewModel', $contents);
54-
}
55-
}
39+
expect(File::get($shouldOutputFilePath))
40+
->toContain('namespace App\ViewModels\Blog;')
41+
->toContain('class PostsViewModel extends ViewModel');
42+
});

0 commit comments

Comments
 (0)