CityTest.php 2.75 KB
<?php

namespace Tests\Feature;

use App\Models\Account;
use App\Models\Campaigns;
use App\Models\Dictionary;
use App\Models\Tokens;
use App\Models\User;
use App\Models\Organization;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CityTest extends TestCase
{
    use RefreshDatabase;

    private $token;
    private $token_main;

    protected function setUp(): void
    {
        parent::setUp();

        $account = Account::create(['name' => 'Acme Corporation']);

        $this->user = factory(User::class)->create([
            'account_id' => $account->id,
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => 'johndoe@example.com',
            'owner' => true,
        ]);

        $this->token = factory(Tokens::class)->create([
            'created_by' => $this->user->getKey()
        ]);

        $this->token_main = factory(Tokens::class)->create([
            'type' => Tokens::MAIN,
        ]);

        factory(Campaigns::class, 2)->create([
            'manage' => true,
            'token' => $this->token_main->getKey(),
        ]);

        factory(Campaigns::class, 2)->create([
            'manage' => false,
            'token' => $this->token_main->getKey(),
        ]);

        factory(Dictionary::class, 10)->create([
            'type' => Dictionary::CITY,
        ]);
    }

    public function test_add_city(){
        $ids = Dictionary::all()->pluck('id');

        $this->actingAs($this->user)
            ->post(route('token.city.store', [
                'token' =>  $this->token->id,
                'city'  => $ids[0]
            ]))
            ->assertStatus(302);

        $this->token->load('cities.campaigns');
        $this->assertEquals($this->token->cities->count(), 1);
        foreach ($this->token->cities as $city){
            $this->assertEquals($city->campaigns->count(), 2);
        }

        $this->actingAs($this->user)
            ->post(route('token.city.store', [
                'token' =>  $this->token->id,
                'city'  => $ids[1]
            ]))
            ->assertStatus(302);

        $this->token->load('cities.campaigns');
        $this->assertEquals($this->token->cities->count(), 2);
        foreach ($this->token->cities as $city){
            $this->assertEquals($city->campaigns->count(), 2);
        }

        $this->actingAs($this->user)
            ->post(route('token.city.store', [
                'token' =>  $this->token->id,
                'city'  => $ids[2]
            ]))
            ->assertStatus(302);

        $this->token->load('cities.campaigns');
        $this->assertEquals($this->token->cities->count(), 3);
        foreach ($this->token->cities as $city){
            $this->assertEquals($city->campaigns->count(), 2);
        }
    }

}