Skip to content

Commit a9a6e4c

Browse files
authored
Add more unit tests for Gift model (#843)
1 parent 297be6d commit a9a6e4c

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

app/Gift.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,12 @@ public function hasParticularRecipient()
109109
/**
110110
* Set the recipient for the gift.
111111
*
112-
* @param string $recipient
113-
* @return static
112+
* @param int $value
113+
* @return string
114114
*/
115-
public function forRecipient($recipient)
115+
public function setIsForAttribute($value)
116116
{
117-
$this->is_for = $recipient;
118-
119-
return $this;
117+
$this->attributes['is_for'] = $value;
120118
}
121119

122120
/**

app/Http/Controllers/Contacts/GiftsController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public function store(GiftsRequest $request, Contact $contact)
9696
);
9797

9898
if ($request->get('has_recipient')) {
99-
$gift->forRecipient($request->get('recipient'))->save();
99+
$gift->is_for = $request->get('recipient');
100+
$gift->save();
100101
}
101102

102103
$contact->logEvent('gift', $gift->id, 'create');
@@ -144,7 +145,8 @@ public function update(GiftsRequest $request, Contact $contact, Gift $gift)
144145
);
145146

146147
if ($request->get('has_recipient')) {
147-
$gift->forRecipient($request->get('recipient'))->save();
148+
$gift->is_for = $request->get('recipient');
149+
$gift->save();
148150
}
149151

150152
$contact->logEvent('gift', $gift->id, 'update');

tests/Unit/GiftTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit;
44

55
use App\Gift;
6+
use App\Contact;
67
use Tests\TestCase;
78
use Illuminate\Foundation\Testing\DatabaseTransactions;
89

@@ -53,4 +54,94 @@ public function test_toggle_a_gift_offered()
5354
$gift->has_been_received
5455
);
5556
}
57+
58+
public function test_has_particular_recipient_returns_false_if_it_s_for_no_specific_recipient()
59+
{
60+
$gift = new Gift;
61+
62+
$this->assertEquals(
63+
false,
64+
$gift->hasParticularRecipient()
65+
);
66+
}
67+
68+
public function test_has_particular_recipient_returns_true_if_it_s_for_a_specific_recipient()
69+
{
70+
$gift = new Gift;
71+
$gift->is_for = 1;
72+
73+
$this->assertEquals(
74+
true,
75+
$gift->hasParticularRecipient()
76+
);
77+
}
78+
79+
public function test_it_sets_is_for_attribute()
80+
{
81+
$gift = new Gift;
82+
$gift->is_for = 1;
83+
84+
$this->assertEquals(
85+
1,
86+
$gift->is_for
87+
);
88+
}
89+
90+
public function test_it_gets_the_recipient_name()
91+
{
92+
$gift = new Gift;
93+
$gift->is_for = 1;
94+
$gift->contact_id = 1;
95+
96+
$contact = factory(Contact::class)->create(['id' => 1, 'first_name' => 'Regis']);
97+
98+
$this->assertEquals(
99+
'Regis',
100+
$gift->recipient_name
101+
);
102+
}
103+
104+
public function test_it_gets_the_gift_name()
105+
{
106+
$gift = new Gift;
107+
$gift->name = 'Maison de folie';
108+
109+
$this->assertEquals(
110+
'Maison de folie',
111+
$gift->name
112+
);
113+
}
114+
115+
public function test_it_gets_the_gift_url()
116+
{
117+
$gift = new Gift;
118+
$gift->url = 'https://facebook.com';
119+
120+
$this->assertEquals(
121+
'https://facebook.com',
122+
$gift->url
123+
);
124+
}
125+
126+
public function test_it_gets_the_comment()
127+
{
128+
$gift = new Gift;
129+
$gift->comment = 'This is just a comment';
130+
131+
$this->assertEquals(
132+
'This is just a comment',
133+
$gift->comment
134+
);
135+
}
136+
137+
public function test_it_gets_the_value()
138+
{
139+
$gift = new Gift;
140+
$gift->value = '100$';
141+
142+
$this->assertEquals(
143+
'100$',
144+
$gift->value
145+
);
146+
}
56147
}

0 commit comments

Comments
 (0)