/
SendLeadsUnitTest.php
204 lines (188 loc) · 6.08 KB
/
SendLeadsUnitTest.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
use Illuminate\Database\QueryException;
class SendLeadsUnitTest extends BrowserKitTestCase
{
/**
* the SendLeadsValidator instance
*/
private $validator;
/**
* return array
*/
private $errors;
/**
* the campaign instance
*/
private $campaign;
/**
* begin silly testing
*/
use SendLeadsTrait;
/**
* constructor
*/
public function __construct()
{
$this->getLeadsForValidator();
}
/**
* @test
* test if SendLeadsTrait is called successfully
*/
public function show()
{
var_dump($this->results);
}
/**
* @test
* return the campaign id
* integer
*/
public function check_if_campaign_return_id()
{
$this->checkMikeTestHello(CampaignRepository::class, 'getCampaignByIdAndStatus', $this->results['eiq_campaign_id']);
}
/**
* @test
*
* @return affiliate id
* integer
*/
public function check_if_affiliate_return_id()
{
$this->checkMikeTestHello(AffiliateRepository::class, 'getAffiliateById', $this->results['eiq_affiliate_id']);
}
/**
* @test
*
* @return object
*/
public function check_if_campaign_should_return_object()
{
$campaignCount = \App\LeadCount::where('campaign_id', $this->results['eiq_campaign_id'])->first();
if (null !== $campaignCount) {
$this->assertIsObject($campaignCount);
} else {
$this->assertNull($campaignCount);
}
}
/**
* @test
*
* @return bool if object
*/
public function check_if_campaign_affiliate_should_return_object()
{
$campaignAffiliateCount = \App\LeadCount::where('campaign_id', $this->results['eiq_campaign_id'])->where('affiliate_id', $this->results['eiq_affiliate_id'])->first();
if (null !== $campaignAffiliateCount) {
$this->assertIsObject($campaignAffiliateCount);
} else {
$this->assertNull($campaignAffiliateCount);
}
}
/**
* @test
*
* @return bool
* silly very long method name, at least you got the idea
*/
public function capreach_should_return_true_if_campaign_counts_and_campaign_affiliate_counts_were_both_not_null_and_return_false_if_campaign_counts_and_campaign_affiliate_counts_were_both_null()
{
$response = true;
/**
* get the campaign object
*/
$campaign = $this->getMockBuilder(CampaignRepository::class)
->setMethods(['getCampaignByIdAndStatus'])
->getMock();
$campaign->expects($this->once())
->method('getCampaignByIdAndStatus')
->will($this->returnValue($response));
$campaign = $campaign->getCampaignByIdAndStatus($this->results['eiq_campaign_id']);
/**
* get the affiliate object
*/
$affiliate = $this->getMockBuilder(AffiliateRepository::class)
->setMethods(['getAffiliateById'])
->getMock();
$affiliate->expects($this->once())
->method('getAffiliateById')
->will($this->returnValue(1));
$affiliate = $affiliate->getAffiliateById($this->results['eiq_affiliate_id']);
/**
* campaign count object
*/
$campaignCounts = \App\LeadCount::where('campaign_id', $this->results['eiq_campaign_id'])->first();
/**
* affiliate object
*/
$campaignAffiliateCounts = \App\LeadCount::where('campaign_id', $this->results['eiq_campaign_id'])->where('affiliate_id', $this->results['eiq_affiliate_id'])->first();
/**
* check if $campaignCounts and $campaignAffiliateCounts true
*/
if (null === $campaignCounts || null === $campaignAffiliateCounts) {
$response = false;
}
/**
* capreach
* now we can simulate the method checkCapNoReset using the variables as parameters created above
*/
$capReached = $this->getMockBuilder(LeadTestController::class)
->disableOriginalConstructor()
->setMethods(['checkCapNoReset'])
->getMock();
$capReached->expects($this->once())
->method('checkCapNoReset')
->will($this->returnValue($response));
$capReached = $capReached->checkCapNoReset($campaign, $affiliate, $campaignCounts, $campaignAffiliateCounts);
$this->assertTrue($response === $capReached);
/**
* now this should throws errors if cap is reached
*/
$this->validator = new App\Http\Services\SendLeadsValidator;
if ($this->validator->isBoolean($capReached) === false) {
$this->errors = $this->validator->getErrors();
$this->assertArrayHasKey('status', $this->errors);
}
}
/**
* @test
* save the leads if not duplicate entry
*/
public function leads_should_save_if_not_duplicate()
{
$payout = 1;
$received = 2;
$lead = new \App\Http\Services\Repositories\LeadRepository;
try {
$lead->leadCreate($this->results, $payout, $received);
$this->seeInDatabase('leads', ['lead_email' => $this->results['eiq_email']]);
} catch (QueryException $e) {
$this->assertSame($e->errorInfo[1], 1062);
} finally {
$this->assertClassNotHasAttribute('call_user_func', LeadTestController::class);
}
}
/**
* reusable method
*
* @param [mixed] $className
* @param [method] $method
* @param [var] $param
* @return [integer]
*/
private function checkMikeTestHello($className, $method, $param)
{
$response = new stdClass();
$response->id = 2;
$stub = $this->getMockBuilder($className)
->setMethods([$method])
->getMock();
$stub->expects($this->once())
->method($method)
->will($this->returnValue($response));
$id = $stub->$method($param);
$this->assertEquals($response->id, $id->id);
$this->assertIsInt($id->id);
}
}