Skip to content

Commit 1835439

Browse files
committed
added controller tests
1 parent 7890f00 commit 1835439

File tree

5 files changed

+95
-22
lines changed

5 files changed

+95
-22
lines changed

app/Http/Controllers/Api/V1/SiteController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use GuzzleHttp\Exception\ServerException;
1414
use Illuminate\Http\JsonResponse;
1515
use Illuminate\Http\Request;
16+
use Illuminate\Support\Facades\App;
1617

1718
/**
1819
* Class SiteController
@@ -34,7 +35,7 @@ public function register(SiteRequest $request): JsonResponse
3435
$url = $request->string('url');
3536
$key = $request->string('key');
3637

37-
$connectionService = new Connection($url, $key);
38+
$connectionService = App::makeWith(Connection::class, [$url, $key]);
3839

3940
// Do a health check
4041
try {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('sites', function (Blueprint $table) {
15+
$table->dropColumn('update_patch');
16+
$table->dropColumn('update_minor');
17+
$table->dropColumn('update_major');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::table('sites', function (Blueprint $table) {
27+
$table->boolean('update_patch');
28+
$table->boolean('update_minor');
29+
$table->boolean('update_major');
30+
});
31+
}
32+
};

phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
2323
<env name="BCRYPT_ROUNDS" value="4"/>
2424
<env name="CACHE_STORE" value="array"/>
25-
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
26-
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
25+
<env name="DB_CONNECTION" value="sqlite"/>
26+
<env name="DB_DATABASE" value=":memory:"/>
2727
<env name="MAIL_MAILER" value="array"/>
2828
<env name="PULSE_ENABLED" value="false"/>
2929
<env name="QUEUE_CONNECTION" value="sync"/>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Tests\Api\Feature;
4+
5+
use App\Models\Site;
6+
use App\RemoteSite\Connection;
7+
use App\RemoteSite\Responses\HealthCheck;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Support\Facades\App;
10+
use Tests\TestCase;
11+
12+
class SiteControllerTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
public function testRegisteringASiteWithoutUrlOrKeyFails(): void
17+
{
18+
$response = $this->postJson(
19+
'/api/v1/register',
20+
);
21+
22+
$response->assertStatus(422);
23+
}
24+
25+
public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
26+
{
27+
App::bind(Connection::class, fn() => $this->getConnectionMock(HealthCheck::from([
28+
"php_version" => "1.0.0",
29+
"db_type" => "mysqli",
30+
"db_version" => "1.0.0",
31+
"cms_version" => "1.0.0",
32+
"server_os" => "Joomla OS 1.0.0"
33+
])));
34+
35+
$response = $this->postJson(
36+
'/api/v1/register',
37+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
38+
);
39+
40+
$response->assertStatus(200);
41+
$result = Site::where('url', 'https://www.joomla.org')->first();
42+
43+
$this->assertEquals([
44+
45+
], $result->toArray());
46+
}
47+
48+
protected function getConnectionMock(HealthCheck $response)
49+
{
50+
$mock = $this->getMockBuilder(Connection::class)
51+
->disableOriginalConstructor()
52+
->onlyMethods(['__call'])
53+
->getMock();
54+
55+
$mock->method('__call')->willReturn($response);
56+
57+
return $mock;
58+
}
59+
}

tests/Feature/ExampleTest.php

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

0 commit comments

Comments
 (0)