MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 8.0.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/.trash/tests.1/Unit/Admin/SubjectTest.php
<?php

namespace Tests\Unit\Admin;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Str;

class SubjectTest extends TestCase
{
     protected $users , $clientData, $role;
    protected function setUp(): void 
    {
        parent::setUp();
        $this->role = \DB::table('roles')->where('id',1)->first();
    }

    public function test_admin_to_create_subject()
    {
        $userAdmin = factory(\App\User::class)->create(['role'=>1]);
        $name = Str::random(10);
        $this->withoutMiddleware();
        $this->withoutExceptionHandling();

        $this->actingAs($userAdmin)->json('GET','/admin/expertise');
        $data = [
            "subject" => $name,
        ];
        
        $response = $this->json('POST', 'api/admin/subject/store', $data, 
            [
                'Accept' => 'application/json', 
                'HTTP_Authorization' => 'Bearer' . $userAdmin->remember_token
            ])
            ->assertStatus(200);
        $response->assertOk();

        \App\Models\Subject::orderBy('id','DESC')->first()->delete();
        \App\User::where('id', $userAdmin->id)->delete();

         // $response1 = $this->post('api/admin/subject/store', $data);
        // dd($response1->getContent()); // add this temporarily
        // $response1->assertResponseStatus(200);

    }

    public function test_admin_to_update_subject ()
    {

        $userAdmin = factory(\App\User::class)->create(['role'=>1]);
        $name = Str::random(10);
        $this->withoutMiddleware();
        $this->withoutExceptionHandling();
        $subject = factory(\App\Models\Subject::class)->create(['subject'=>$name]);
        $this->actingAs($userAdmin)->json('GET','/admin/expertise');
        $data = [
            "subject" => $name." test",
            'subject_id' => $subject->id
        ];
        
        $response = $this->json('POST', 'api/admin/subject/update', $data, 
            [
                'Accept' => 'application/json', 
                'HTTP_Authorization' => 'Bearer' . $userAdmin->remember_token
            ])
            ->assertStatus(200);
        $response->assertOk();

        \App\Models\Subject::where('id', $subject->id)->delete();
        \App\User::where('id', $userAdmin->id)->delete();
     
    }



    public function test_admin_to_delete_subject ()
    {

         $userAdmin = factory(\App\User::class)->create(['role'=>1]);
        $name = Str::random(10);
        $this->withoutMiddleware();
        $this->withoutExceptionHandling();
        $subject = factory(\App\Models\Subject::class)->create(['subject'=>$name]);
        $this->actingAs($userAdmin)->json('GET','/admin/expertise');
        $data = [
            'id' => $subject->id
        ];
        
        $response = $this->json('POST', 'api/admin/subject/delete', $data, 
            [
                'Accept' => 'application/json', 
                'HTTP_Authorization' => 'Bearer' . $userAdmin->remember_token
            ])
            ->assertStatus(200);
        $response->assertOk();

        \App\Models\Subject::where('id', $subject->id)->delete();
        \App\User::where('id', $userAdmin->id)->delete();
    }

    public function tearDown(): void
    {
        $maxId = \DB::table('subjects')->max('id');
        \DB::statement('ALTER TABLE subjects AUTO_INCREMENT=' . intval($maxId + 1) . ';');

        $maxId = \DB::table('users')->max('id');
        \DB::statement('ALTER TABLE users AUTO_INCREMENT=' . intval($maxId + 1) . ';');
        parent::tearDown();
    }
}