Skip to content

Commit 9fdae62

Browse files
committed
Testing Cases Initialisation using TestBench
1 parent 3e739ab commit 9fdae62

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"Tests\\Suite\\PostAPISuccessTest::test_post_api":7},"times":{"Tests\\Suite\\PostAPISuccessTest::test_post_api":0.05}}

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
"ext-json": "*"
3535
},
3636
"require-dev": {
37-
"laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0"
37+
"laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0",
38+
"orchestra/testbench": "^8.5",
39+
"phpunit/phpunit": "^10.1"
3840
},
3941
"autoload": {
4042
"psr-4": {

examples/database/database.sqlite

Whitespace-only changes.

examples/tests/TestCase.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
class TestCase extends \Orchestra\Testbench\TestCase
6+
{
7+
use \Illuminate\Foundation\Testing\RefreshDatabase;
8+
public function defineEnvironment($app)
9+
{
10+
tap($app->make('config'), function (\Illuminate\Contracts\Config\Repository $config) {
11+
$config->set('database.default', 'testbench');
12+
$config->set('database.connections.testbench', [
13+
'driver' => 'sqlite',
14+
'database' => ':memory:',
15+
'prefix' => '',
16+
]);
17+
});
18+
}
19+
20+
protected function defineRoutes($router)
21+
{
22+
\Illuminate\Support\Facades\Route::resource('api/minions', 'MinionController', ['parameters' => ['minions' => 'id']]);
23+
}
24+
25+
protected function defineDatabaseMigrations()
26+
{
27+
$this->loadMigrationsFrom(__DIR__ . '/../migrations');
28+
$this->artisan('migrate', ['--database' => 'testbench'])->run();
29+
}
30+
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
5+
use \Tests\TestCase;
6+
7+
class PostAPISuccessTest extends TestCase
8+
{
9+
public function test_post_api()
10+
{
11+
$payload = [
12+
'name' => 'Stuart',
13+
'totalEyes' => 2,
14+
'favouriteSound' => 'Grrrrrrrrrrr',
15+
'hasHairs' => true,
16+
];
17+
18+
$response = $this->postJson('/api/minions', $payload);
19+
$response->assertStatus(200);
20+
$response->assertJson([
21+
'message' => 'Resource Created successfully',
22+
'data' => [
23+
'name' => 'Stuart',
24+
'totalEyes' => 2,
25+
'favouriteSound' => 'Grrrrrrrrrrr',
26+
'hasHairs' => true,
27+
'id' => 1,
28+
],
29+
'type' => null,
30+
]);
31+
}
32+
33+
}

phpunit.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Unit">
9+
<directory suffix="Test.php">.examples/tests/suite</directory>
10+
</testsuite>
11+
<testsuite name="Feature">
12+
<directory suffix="Test.php">.examples/tests/suite</directory>
13+
</testsuite>
14+
</testsuites>
15+
<source>
16+
<include>
17+
<directory suffix=".php">./app</directory>
18+
</include>
19+
</source>
20+
<php>
21+
<env name="APP_ENV" value="testbench"/>
22+
<env name="BCRYPT_ROUNDS" value="4"/>
23+
<env name="CACHE_DRIVER" value="array"/>
24+
<env name="DB_CONNECTION" value="testbench"/>
25+
<env name="DB_DATABASE" value=":memory:"/>
26+
<env name="MAIL_MAILER" value="array"/>
27+
<env name="QUEUE_CONNECTION" value="sync"/>
28+
<env name="SESSION_DRIVER" value="array"/>
29+
<env name="TELESCOPE_ENABLED" value="false"/>
30+
</php>
31+
</phpunit>

0 commit comments

Comments
 (0)