<commit_before><?php

use Laravel\Lumen\Testing\DatabaseTransactions;
use App\Entities\TireSensor;
use App\Entities\Type;

class TireSensorTest extends TestCase
{
    private function setEloquentMock($method, $return)
    {
        $mockRepo = \Mockery::mock('App\Http\Controllers\TireConditionController');
        $mockRepo->shouldReceive($method)->andReturn($return);
        $this->app->instance('App\Http\Controllers\TireConditionController', $mockRepo);
    }
    
    public function testTireSensorDeleteFail()
    {
        $this->delete('/api/v1/tiresensor');

        $this->assertEquals($this->response->status(), 405);

    }

    public function testTireSensorPostFail()
    {
        $this->setEloquentMock('checkTireCondition', true);
        $this->post('/api/v1/tiresensor', ['vehicle_id' => 1, 
                'json' => '[{"id":"0000000001","pr":127,"pos":2,"tp":22.0,"ba":2.95'
                            .',"latitude":51.10,"longitude":30.05}]'
        ]);

        $this->assertEquals($this->response->status(), 401);

    }

    public function testTireSensorPostSuccess()
    {
        $company = factory('App\Company')->create();

        //$this->setEloquentMock('checkTireCondition', true);
        $this->actingAs($company)
            ->post('/api/v1/tiresensor', ['api_token' => env('APP_TOKEN'), 
                'email' => '<EMAIL>', 
                'vehicle_id' => 1, 
                'dataIsCompressed' => 0,
                'json' => '[{"id":"0000000001","pr":127,"pos":2,"tp":22.0,"ba":2.95'
                            .',"latitude":51.10,"longitude":30.05}]'
            ]);
//             ->assertResponseStatus(200);
        echo $this->response->getContent();

        $this->seeInDatabase('tire_sensor', ['latitude' => 51.10, 
                                    'longitude' => 30.05, 
                                    'temperature' => 22.0, 
                                    'pressure' => 127, 
                                    'battery' => 2.95, 
                                    'number' => '0000000001'
        ]);
    }

    public function testTireSensorGenerateEntry()
    {
        $company = factory('App\Company')->create();
        $entry_type = factory('App\Entities\Type')->create();
        
        $this->actingAs($company)
            ->post('/api/v1/tiresensor', ['api_token' => env('APP_TOKEN'),
                'email' => '<EMAIL>',
                'vehicle_id' => 1,
                'dataIsCompressed' => 0,
                'json' => '[{"id":"0000000001","pr":125,"pos":2,"tp":122.0,"ba":2.95'
                .',"latitude":51.10,"longitude":30.05}]'
            ]);            

    }

    public function testTireSensorPostSuccessDataIsCompressed()
    {
        $company = factory('App\Company')->create();<commit_msg>Aumento na cobertura de testes<commit_after><?php

use Laravel\Lumen\Testing\DatabaseTransactions;
use App\Entities\TireSensor;
use App\Entities\Type;

class TireSensorTest extends TestCase
{
    private function setEloquentMock($method, $return)
    {
        $mockRepo = \Mockery::mock('App\Http\Controllers\TireConditionController');
        $mockRepo->shouldReceive($method)->andReturn($return);
        $this->app->instance('App\Http\Controllers\TireConditionController', $mockRepo);
    }
    
    public function testTireSensorDeleteFail()
    {
        $this->delete('/api/v1/tiresensor');

        $this->assertEquals($this->response->status(), 405);

    }

    public function testTireSensorPostFail()
    {
        $this->setEloquentMock('checkTireCondition', true);
        $this->post('/api/v1/tiresensor', ['vehicle_id' => 1, 
                'json' => '[{"id":"0000000001","pr":127,"pos":2,"tp":22.0,"ba":2.95'
                            .',"latitude":51.10,"longitude":30.05}]'
        ]);

        $this->assertEquals($this->response->status(), 401);

    }

    public function testTireSensorPostSuccess()
    {
        $company = factory('App\Company')->create();

        //$this->setEloquentMock('checkTireCondition', true);
        $this->actingAs($company)
            ->post('/api/v1/tiresensor', ['api_token' => env('APP_TOKEN'), 
                'email' => '<EMAIL>', 
                'vehicle_id' => 1, 
                'dataIsCompressed' => 0,
                'json' => '[{"id":"0000000001","pr":127,"pos":2,"tp":22.0,"ba":2.95'
                            .',"latitude":51.10,"longitude":30.05}]'
            ]);
//             ->assertResponseStatus(200);
        echo $this->response->getContent();

        $this->seeInDatabase('tire_sensor', ['latitude' => 51.10, 
                                    'longitude' => 30.05, 
                                    'temperature' => 22.0, 
                                    'pressure' => 127, 
                                    'battery' => 2.95, 
                                    'number' => '0000000001'
        ]);
    }

    public function testTireSensorGenerateEntry()
    {
        $company = factory('App\Company')->create();
        $entry_type = factory('App\Entities\Type')->create();
        
        $this->actingAs($company)
            ->post('/api/v1/tiresensor', ['api_token' => env('APP_TOKEN'),
                'email' => '<EMAIL>',
                'vehicle_id' => 1,
                'dataIsCompressed' => 0,
                'json' => '[{"id":"0000000001","pr":225,"pos":2,"tp":122.0,"ba":2.95'
                .',"latitude":51.10,"longitude":30.05}]'
            ]);     
        
        $this->actingAs($company)
            ->post('/api/v1/tiresensor', ['api_token' => env('APP_TOKEN'),
                'email' => '<EMAIL>',
                'vehicle_id' => 1,
                'dataIsCompressed' => 0,
                'json' => '[{"id":"0000000001","pr":125,"pos":2,"tp":122.0,"ba":2.95'
                .',"latitude":51.10,"longitude":30.05}]'
            ]); 
            
        $entry_type = Type::select('id')->where('company_id', 1)
            ->where(function ($query) {
                $query->where('name', 'calibration maintenance')
                ->orWhere('name', 'manuten&ccedil;&atilde;o de calibragem');
            })
            ->first();

        $this->seeInDatabase('entries', ['company_id' => 1, 
                                    'entry_type_id' => $entry_type->id
        ]);
    }

    public function testTireSensorPostSuccessDataIsCompressed()
    {
        $company = factory('App\Company')->create();
{'fraction_non_alphanumeric': 0.18214285714285713, 'fraction_numerical': 0.04565217391304348, 'mean_word_length': 1.9171195652173914, 'pattern_counts': {'":': 49, '<': 10, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 9, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '889677', 'n_tokens_mistral': 2145, 'n_tokens_neox': 1900, 'n_words': 234}