File: /home/imensosw/.trash/tests/Unit/Admin/SoftwareToolTest.php
<?php
namespace Tests\Unit\Admin;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\SoftwareTool;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Str;
use App\Models\Software;
class SoftwareToolTest extends TestCase
{
protected $users , $clientData, $software_id, $role;
protected function setUp(): void
{
parent::setUp();
// $user = factory(\App\User::class)->make();
// $this->users = $user;
$this->software_id = Software::inRandomOrder()->first();
$this->role = \DB::table('roles')->where('id', 1)->first();
/* $client = factory(SoftwareTool::class)->create();
$this->clientData = $client;*/
}
public function test_admin_to_create_software_tool()
{
$user = factory(\App\User::class)->create(['role' => $this->role->id]);
$profile = factory(\App\Models\Profile::class)->create(['user_id' => $user->id]);
$name = Str::random(10);
$this->withoutMiddleware();
$this->withoutExceptionHandling();
$this->actingAs($user)->json('GET', '/admin/software_tool');
$data = [
"software_id" => $this->software_id->id,
"tool" => $name,
];
$response = $this->json(
'POST',
'api/admin/software_tool/store',
$data,
[
'Accept' => 'application/json',
// 'HTTP_Authorization' => 'Bearer' . $user->remember_token
]
)
->assertStatus(200);
$response->assertOk();
\App\Models\SoftwareTool::orderBy('id', 'DESC')->first()->delete();
\App\Models\Profile::where('user_id', $user->id)->delete();
\App\User::where('id', $user->id)->delete();
/* $response1 = $this->post('/api/admin/software_tool/store', $data);
dd($response1->getContent()); // add this temporarily
$response1->assertResponseStatus(200);
*/
}
public function test_admin_to_update_software_tool()
{
$name = Str::random(10);
$createSoftwareTool = factory(SoftwareTool::class)->create(['software_id' => $this->software_id->id ]);
$user = factory(\App\User::class)->create(['role' => $this->role->id]);
$profile = factory(\App\Models\Profile::class)->create(['user_id' => $user->id]);
// $result = SoftwareTool::orderBy('id','DESC')->first();
$this->withoutMiddleware();
$this->withoutExceptionHandling();
$this->actingAs($user)->json('GET', '/admin/software_tool');
$test = "Eddkvc";
$data = [
'software_tool_id' => $createSoftwareTool->id,
"tool" => "dfdfdf",
"software_id" => $createSoftwareTool->software_id,
];
$response = $this->json(
'POST',
'api/admin/software_tool/update',
$data,
[
'Accept' => 'application/json',
'HTTP_Authorization' => 'Bearer' . $user->remember_token
]
)
->assertStatus(200);
$response->assertOk();
SoftwareTool::where('id', $createSoftwareTool->id)->delete();
\App\Models\Profile::where('user_id', $user->id)->delete();
\App\User::where('id', $user->id)->delete();
}
public function test_admin_to_delete_software_tool()
{
$user = factory(\App\User::class)->create(['role' => $this->role->id]);
$profile = factory(\App\Models\Profile::class)->create(['user_id' => $user->id]);
$this->withoutMiddleware();
$this->withoutExceptionHandling();
$this->actingAs($user)->json('GET', '/admin/software_tool');
$createSoftwareTool = factory(SoftwareTool::class)->create(['software_id' => $this->software_id->id ]);
$data = [
'id' => $createSoftwareTool->id,
];
$response = $this->json(
'POST',
'api/admin/software_tool/delete',
$data,
[
'Accept' => 'application/json',
'HTTP_Authorization' => 'Bearer' . $user->remember_token
]
)
->assertStatus(200);
$response->assertOk();
// \App\Models\Profile::where('user_id',$user->id)->delete();
SoftwareTool::where('id', $createSoftwareTool->id)->delete();
\App\Models\Profile::where('user_id', $user->id)->delete();
\App\User::where('id', $user->id)->delete();
}
public function tearDown(): void
{
$maxId = \DB::table('software_tools')->max('id');
\DB::statement('ALTER TABLE software_tools AUTO_INCREMENT=' . intval($maxId + 1) . ';');
/* $maxId = \DB::table('profiles')->max('id');
\DB::statement('ALTER TABLE profiles AUTO_INCREMENT=' . intval($maxId + 1) . ';');*/
$maxId = \DB::table('users')->max('id');
\DB::statement('ALTER TABLE users AUTO_INCREMENT=' . intval($maxId + 1) . ';');
parent::tearDown();
}
}