<?php
use App\Branch;
use App\Organization;

class BranchTest extends TestCase
{
     /**
     * Test to check save branch
     *
     * @return void
     */
    public function testSaveBranch()
    {
        $name = str_random(20); 
        $org = Organization::max('id');
        $response = $this->call('POST', 'api/v1/branch/', ['name' => $name, 'organization_id' => $org]); 
        $this->assertEquals(200, $response->status());
        $this->post('api/v1/branch/', ['name' => $name, 'organization_id' => $org])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    /**
     * Test to check get branches
     *
     * @return void
     */
    public function testBranches()
    {
       $response = $this->call('GET', 'api/v1/branch/');
       $this->assertEquals(200, $response->status());
    }
    
    /**
     * Test to check get branch
     *
     * @return void
     */
    public function testGetBranch()
    {
        $last = Organization::max('id');
        $response = $this->call('GET', "api/v1/branch/$last");
        $this->assertEquals(200, $response->status());
    }
    
    /**
     * Test to check get branches
     *
     * @return void
     */
    public function testGetBranches()
    {
       $last = Organization::max('id');
       $response = $this->call('POST', 'api/v1/branch/all', ['organization_id' => $last]); 
       $this->assertEquals(200, $response->status());
       
       $this->post('api/v1/branch/all', ['organization_id' => 0000])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    /**
     * Test to check update branch
     *
     * @return void
     */
    public function testUpdateBranch()
    {
        $name = str_random(20); 
        $org = Organization::max('id');
        $response = $this->call('POST', 'api/v1/branch/', ['name' => $name, 'organization_id' => $org]); 
        $this->assertEquals(200, $response->status());
        $last = Branch::max('id');
        $name = str_random(20); 
        $this->put('api/v1/branch/000', ['name' => $name,'organization_id'=>$org])
             ->seeJson([
                'status' => 'fail',
             ]);
        $response = $this->call('PUT', "api/v1/branch/$last", ['name' => $name,'organization_id'=>$org]); 
        $this->assertEquals(200, $response->status()); 
    }


/**
     * Test to check save place
     *
     * @return void
     */
    public function testSavePlace()
    {
        $branch           = new Branch();
        $branch ->name = str_random(5);
        $org = Organization::max('id');
        $branch ->organization_id = $org;
        $date               = new \DateTime();
        $ddf                = $date->format('Y-m-d');
        $branch->created  = $ddf;
        $branch->save();

        $lastId = $branch->id;
        $name = 'ChIJCzYy5IS16lQRQrfeQ5K5Oxw'; 
        $response = $this->call('POST', 'api/v1/branch/saveplace', ['branchId' => $lastId, 'place_value' => $name]); 
        $this->assertEquals(200, $response->status());

        $this->post('api/v1/branch/saveplace', ['branchId' => 0, 'place_value' => $name])
             ->seeJson([
                'status' => 'fail',
             ]);

       Branch::destroy($lastId);
    }

     /**
     * Test to check save branch
     *
     * @return void
     */
    public function testDeleteBranch()
    {
       $branch        = new Branch();
       $branch ->name = str_random(5);;
       $branch ->organization_id = 1;
       $date               = new \DateTime();
       $ddf                = $date->format('Y-m-d');
       $branch->created  = $ddf;
       $branch->save();

       $lastId = $branch->id;
       $this->delete("api/v1/branch/$lastId")
             ->seeJson([
                'status' => 'success',
             ]);
    }
}
