Skip to content

Instantly share code, notes, and snippets.

@thesubhendu
Created January 21, 2025 05:34
Show Gist options
  • Save thesubhendu/f40a289e3c9849a24e418baf26f2b7b6 to your computer and use it in GitHub Desktop.
Save thesubhendu/f40a289e3c9849a24e418baf26f2b7b6 to your computer and use it in GitHub Desktop.
Smoke Testing
<?php
namespace Tests\Smoke;
use App\Models\EmailTemplate;
use App\Models\TemporaryPlacement;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
use App\Models\User;
use App\Models\Customer;
use App\Models\Task;
use App\Models\Submission;
use App\Models\Placement;
use App\Models\JobVerified;
use App\Models\Account;
use App\Models\ClinicalUnit;
use App\Models\CommunicationLog;
use App\Models\RecruiterCustomerPreference;
use App\Models\Comment;
use App\Models\MentionedComment;
use App\Models\Notification;
use App\Models\Note;
use App\Models\CustomerDetails;
use App\Models\PayPackage;
use App\Models\AccountAddress;
use App\Models\Form;
use App\Models\FormInputElement;
use App\Models\JobSearch;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\Sanctum\Sanctum;
use Tests\Smoke\Filter\ExcludedRouteTree;
class CustomRoutesTest extends TestCase
{
use DatabaseTransactions;
protected $form;
protected $formElement;
private function log($uri, $response)
{
$cleanedUri = str_replace('/', '_', $uri);
Storage::put('failed-smoke-tests\\' . $cleanedUri . '.html', $response->getContent());
Log::info($response->getContent());
}
protected function setDatabase()
{
$this->recruiter = User::factory()->create(['role' => 'recruiter-health']);
$this->recruiterId = $this->recruiter->id;
$this->customer = Customer::factory()->create(['recruiter_id' => $this->recruiterId]);
$this->customerId = $this->customer->id_customer;
CustomerDetails::factory()->create(['id_customer' => $this->customerId]);
$this->communication_log = CommunicationLog::factory()->create(
['communicable_id' => $this->customerId]
);
$this->token = $this->customer->token;
$this->taskId = Task::factory()->create([
'taskable_id' => $this->customerId, 'taskable_type' => Customer::class,
])->task_id;;
$this->accountManager = User::factory()->create(['role' => 'account-manager']);
$this->accountManagerId = $this->accountManager->id;
$this->clinicalCoordinator = User::factory()->create(['role' => 'clinical-coordinator']);
$this->clinicalCoordinatorId = $this->clinicalCoordinator->id;
$this->account = Account::factory()->create([
'account_name' => 'Abbott-Littel',
'account_manager' => $this->accountManagerId,
'clinical_coordinator' => $this->clinicalCoordinatorId,
]);
$this->accountId = $this->account->id_account;
AccountAddress::factory()->create(['id_account' => $this->accountId, 'zip' => '10101']);
$clinicalUnitId = ClinicalUnit::factory()->create()->id_clinical_unit;
$this->job = JobVerified::factory()->create([
'account_id' => $this->accountId,
'clinical_unit' => $clinicalUnitId
]);
$this->jobId = $this->job->id_job_verified;
JobSearch::factory()->create(
[
'job_id' => $this->jobId,
'account_id' => $this->accountId,
]
);
$this->payPackageId = PayPackage::factory()->create(
['customer_id' => $this->customerId, 'job_id' => $this->jobId, 'account_id' => $this->accountId, 'token' => $this->token]
)->package_id;
$this->submissionId = Submission::factory()->create([
'job_id' => $this->jobId,
'customer_id' => $this->customerId,
'package_id' => $this->payPackageId,
])->submission_id;
$placement = Placement::factory()->create(
['submission_id' => $this->submissionId]
);
$this->placementId = $placement->placement_id;
$this->packageToken = $placement->submission->package->token;
$this->commentId = Comment::factory()->create([
'customer_id' => $this->customerId, 'placement_id' => $this->placementId,
'submission_id' => $this->submissionId,
])->id;
$this->mentionedCommentId = MentionedComment::factory()->create([
'comment_id' => $this->commentId,
'user_id' => $this->recruiterId,
'author_id' => $this->recruiterId,
])->id;
Notification::factory()->create([
'id' => (string) Str::uuid(),
'notifiable_id' => $this->recruiterId,
'notifiable_type' => User::class,
]);
$this->notificationId = Notification::first()->id;
$this->commentType = "submission";
$this->typeId = $this->submissionId;
$this->noteId = Note::factory()->create([
'id_customer' => $this->customerId, 'account_id' => $this->accountId,
])->id_notes;
$this->preferenceId = RecruiterCustomerPreference::factory()->create(
['customer_id' => $this->customerId]
)->id;
$this->emailTemplate = EmailTemplate::factory()->create();
TemporaryPlacement::factory()->create(['submission_id' => $this->submissionId]);
$this->form = Form::factory()->create();
$this->formElement = FormInputElement::factory()->create([
'form_id' => $this->form->id
]);
}
private function loginAsRecruiter()
{
$user = $this->recruiter;
$user->createToken('auth_token')->plainTextToken;
Sanctum::actingAs($user, ['*'], 'sanctum');
}
public function test_get_customRoutes()
{
$this->setDatabase();
$routes = Route::getRoutes()->getRoutesByMethod();
$routeCollection = $routes['GET'];
$allGetRoutes = [];
foreach ($routeCollection as $v) {
$allGetRoutes[] = $v->uri();
}
$excludedRoutes = [
'clockwork/*',
'__clockwork/*',
'oauth/*',
'sanctum/*',
'api/compliance-tab',
'api/chat/*',
'api/chat-templates/*',
'_ignition/*',
'api/tags/{tag}',
'api/get-recruiter-auth-token',
'api/dashboard/initial-counts',
'api/get-recruiter-auth-token',
'api/dashboard/recent-events-counts',
'api/compliance/get-placement-data/{customer}',
'api/submission/approval-action/{submission}',
'api/get-recruiter-auth-token',
'api/broadcasting/auth',
'api/accounts',
'api/user-search',
'api/time-cards/attachment/{attachment}/download',
'api/time-cards/attachment/{timeCard}/zip-download',
'api/get-amigo-customer-info/{token}',
'admin',
'docs/*',
'admin/login',
'admin/time-cards',
'admin/perm-jobs',
'admin/perm-jobs/*',
'api/perm/*', //TODO: handle the get routes for perm-jobs
'nurse-dash-api/v1/*', //TODO: handle the routes for perm-jobs related to nnd
'admin/job-boost-rules',
'admin/job-boost-rules/create',
'admin/job-boost-rules/{record}',
'livewire/preview-file/{filename}',
'filament/exports/{export}/download',
'filament/imports/{import}/failed-rows/download',
'api/get-boosted-gp-rule/{jobid}',
'api/get-amigo-customer-info/{token}',
'timecard-modal/{timeCardId}/{activeTab}',
'notification-modal/{timeCardId}',
'account-info/{accountId}',
'api/nursing-license/{license}/download',
'api/clinical-certification/{certification}/download',
'nurse-dash-login/*',
'api/dittofeed-integration/job-details',
'api/cdp/customers',
'admin/email-delivery-failures',
'admin/email-delivery-failures/{record}',
'admin/email-templates/*',
];
$routeTree = new ExcludedRouteTree($excludedRoutes);
$filteredRoutes = collect($routeTree->filterRoutes($allGetRoutes));
$filteredRoutes = $filteredRoutes->map(function ($item) {
return $this->modifiedUrl(strval($item));
});
$routesToTest = $filteredRoutes;
$totalPassed = 0;
$totalFailed = 0;
foreach ($routesToTest as $key => $route) {
$this->loginAsRecruiter();
$response = $this->get($route);
$statusCode = $response->getStatusCode();
echo "\n\nKey: $key URI: " . $route;
if ($statusCode == 200) {
echo "\nResult: \e[42m passed \e[0m with status code: $statusCode ";
$totalPassed++;
} else {
echo "\nResult: \e[33m failed to load \e[0m with status code: $statusCode ";
$this->log($route, $response);
$totalFailed++;
}
}
echo "\n\n\e[42m Total passed: $totalPassed \e[0m ";
echo "\n\n\e[33m Total failed: $totalFailed \e[0m ";
if ($totalFailed == 0) {
$this->assertTrue(true);
} else {
$this->assertTrue(false);
}
}
private function getRouteVariables()
{
return [
'{customer?}' => $this->customerId,
'{customer}' => $this->customerId,
'{customerId}' => $this->customerId,
'{customer_id}' => $this->customerId,
'{placement}' => $this->placementId,
'{note}' => $this->noteId,
'{preference}' => $this->preferenceId,
'{id}' => $this->mentionedCommentId,
'{submission}' => $this->submissionId,
'{submissionId}' => $this->submissionId,
'{submissionId?}' => $this->submissionId,
'{task}' => $this->taskId,
'{notification}' => $this->notificationId,
'{commentType}' => $this->commentType,
'{typeId}' => $this->typeId,
'{token}' => $this->token,
'{customer_task}' => $this->taskId,
'{job_id?}' => $this->jobId,
"{customer_task}" => $this->taskId,
"{job}" => $this->jobId,
"{jobVerified}" => $this->jobId,
"{payPackage?}" => $this->payPackageId,
'{accountId}' => $this->accountId,
'search-recruiter-customers' => 'search-recruiter-customers?recruiter_id=' . $this->recruiterId . '&q=bikash',
'search-accounts' => 'search-accounts?q=bikash',
'search-client-contacts' => 'search-client-contacts?q=client',
'{accountName}' => 'Abbott-Littel',
'{emailTemplate}' => $this->emailTemplate->id,
'{packageToken}' => $this->packageToken,
'{accountModel}' => $this->accountId,
'{form}' => $this->form->id,
'{formElement}' => $this->formElement->id,
];
}
private function modifiedUrl($url)
{
return str_replace(array_keys($this->getRouteVariables()), array_values($this->getRouteVariables()), $url);
}
}
<?php
namespace Tests\Smoke\Filter;
class ExcludedRouteTree
{
public $root;
public function __construct($routes)
{
$this->root = new RouteNode('');
$this->root->ends = true;
$this->createStructure($routes);
}
private function createStructure($routes): void
{
foreach($routes as $route)
{
$this->addRoute($route);
}
}
private function addRoute($route): void
{
$segments = explode('/', $route);
$currentNode = $this->root;
foreach($segments as $segment)
{
if (!isset($currentNode->nodes[$segment]))
{
$currentNode->nodes[$segment] = new RouteNode($segment);
}
if($segment == '*'){
$currentNode->ends = true;
}
$currentNode = $currentNode->nodes[$segment];
}
$currentNode->ends = true;
}
private function routeExists($route): bool
{
if($route == '/')
return true;
$segments = explode('/', trim($route, '/'));
$currentNode = $this->root;
foreach ($segments as $segment)
{
if(isset($currentNode->nodes['*'])){
return true;
}
if (!isset($currentNode->nodes[$segment])) {
return false;
}
$currentNode = $currentNode->nodes[$segment];
}
return $currentNode->ends ? true : false;
}
public function filterRoutes($routes): array
{
return array_filter($routes, function($item){
return !$this->routeExists($item);
});
}
}
<?php
namespace Tests\Smoke\Filter;
class RouteNode
{
public $value;
public $nodes;
public $ends;
public function __construct($value)
{
$this->value = $value;
$this->nodes = [];
$this->ends = false;
}
public function addNode(RouteNode $node): void
{
$this->nodes[] = $node;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment