vito/tests/TestCase.php
Pierluigi Cau (PG) d702b95e0c
Add notification for deployment completion (#445)
* Add notification for deployment completion

Add notification for deployment completion status.

* Create `DeploymentCompleted` notification class in `app/Notifications/DeploymentCompleted.php` to handle deployment completion notifications.
* Update `app/Actions/Site/Deploy.php` to send `DeploymentCompleted` notification using `Notifier` when a deployment completes or fails.
* Import `Notifier` and `DeploymentCompleted` classes in `app/Actions/Site/Deploy.php`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/vitodeploy/vito?shareId=XXXX-XXXX-XXXX-XXXX).

* Format with pint

* Add tests

* Pint format

* Delete tests/Feature/Notifications/DeploymentCompletedTest.php

* Delete tests/Unit/Notifications/DeploymentCompletedTest.php

* 🍻🍻

* tests

---------

Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com>
Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
2025-01-29 19:29:51 +01:00

119 lines
3.3 KiB
PHP

<?php
namespace Tests;
use App\Enums\Database;
use App\Enums\ServiceStatus;
use App\Enums\UserRole;
use App\Enums\Webserver;
use App\Models\NotificationChannel;
use App\Models\Server;
use App\Models\Site;
use App\Models\SourceControl;
use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\File;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected User $user;
protected Server $server;
protected Site $site;
protected NotificationChannel $notificationChannel;
public const EXPECT_SUCCESS = true;
public const EXPECT_FAILURE = false;
protected function setUp(): void
{
parent::setUp();
config()->set('queue.connections.ssh.driver', 'sync');
config()->set('filesystems.disks.key-pairs.root', storage_path('app/key-pairs-test'));
$this->user = User::factory()->create([
'role' => UserRole::ADMIN,
]);
$this->user->createDefaultProject();
$this->notificationChannel = NotificationChannel::factory()->create([
'provider' => \App\Enums\NotificationChannel::EMAIL,
'connected' => true,
'data' => [
'email' => 'user@example.com',
],
]);
$this->setupServer();
$this->setupSite();
$this->setupKeys();
}
protected function tearDown(): void
{
parent::tearDown();
if (File::exists(storage_path('app/key-pairs-test'))) {
File::deleteDirectory(storage_path('app/key-pairs-test'));
}
}
private function setupServer(): void
{
$this->server = Server::factory()->create([
'user_id' => $this->user->id,
'project_id' => $this->user->current_project_id,
]);
$keys = $this->server->sshKey();
if (! File::exists($keys['public_key_path']) || ! File::exists($keys['private_key_path'])) {
$this->server->provider()->generateKeyPair();
}
$this->server->type()->createServices([
'webserver' => Webserver::NGINX,
'database' => Database::MYSQL80,
'php' => '8.2',
]);
$this->server->services()->update([
'status' => ServiceStatus::READY,
]);
}
private function setupSite(): void
{
/** @var SourceControl $sourceControl */
$sourceControl = SourceControl::factory()->github()->create();
$this->site = Site::factory()->create([
'domain' => 'vito.test',
'aliases' => ['www.vito.test'],
'server_id' => $this->server->id,
'source_control_id' => $sourceControl->id,
'repository' => 'organization/repository',
'path' => '/home/vito/vito.test',
'web_directory' => 'public',
'branch' => 'main',
]);
}
private function setupKeys(): void
{
config()->set('core.ssh_public_key_name', 'test-key.pub');
config()->set('core.ssh_private_key_name', 'test-key');
$publicKeypath = storage_path(config('core.ssh_public_key_name'));
$privateKeyPath = storage_path(config('core.ssh_private_key_name'));
if (! File::exists($publicKeypath) || ! File::exists($privateKeyPath)) {
generate_key_pair(storage_path('test-key'));
}
}
}