UpdateCampaignsTest.php
3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Tests\Unit;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Account;
use App\Models\Campaigns;
use App\Models\Dictionary;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Models\User;
use App\Service\Contract\API;
use App\Service\Requests\APIRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UpdateCampaignsTest extends TestCase
{
use RefreshDatabase;
private $request;
private $user;
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_main = factory(Tokens::class)->create([
'type' => Tokens::MAIN,
'created_by' => $this->user->id,
]);
$this->token = factory(Tokens::class)->create([
'limit' => 20,
'created_by' => $this->user->getKey()
]);
$this->request = APIRequest::getInstance(API::YANDEX)
->setToken($this->token_main)
->getRequest('Campaigns', 'update');
$this->campaign = factory(Campaigns::class)->create([
'manage' => true,
'token' => $this->token_main->getKey(),
]);
$this->dictionaries = factory(Dictionary::class, 12)->create([
'token_id' => $this->token->getKey(),
'type' => Dictionary::CITY,
]);
$this->campaign->dictionaries()->syncWithoutDetaching(
$this->dictionaries->keyBy(Campaigns::getModel()->getKeyName())->transform(function (Dictionary $dictionary) {
return DictionaryCampaign::copyPropertyFromMain($this->campaign);
})->all()
);
}
function test_get_campaigns_for_update(){
$dictionaryCampaign = DictionaryCampaign::first();
$dictionaryCampaign['external_id'] = 1;
$data['external_updated_at'] = Carbon::now();
if ($dictionaryCampaign->updated) {
$data['updated_need'] = Carbon::now();
}
$dictionaryCampaign->update($data);
$tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate')
->with('dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate.campaign')
->where('type', '!=', Tokens::MAIN)
->get();
Queue::fake();
foreach ($tokens as $token) {
$this->assertEquals($dictionaryCampaign->toArray()['external_id'], $token->dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate->toArray()[0]['external_id']);
$this->request->call([
'dictionaryCampaigns' => $token->dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate,
]);
Queue::assertPushed(ProcessCallLimitedAPI::class);
}
}
}