Skip to content

Commit cb1e9cc

Browse files
Update tests
1 parent 215036f commit cb1e9cc

File tree

9 files changed

+322
-328
lines changed

9 files changed

+322
-328
lines changed

composer.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
},
3131
"require-dev": {
3232
"friendsofphp/php-cs-fixer": "^3.7",
33-
"orchestra/testbench": "^7.1",
3433
"phpunit/phpunit": "^9.5"
3534
},
3635
"autoload": {
@@ -48,13 +47,6 @@
4847
},
4948
"minimum-stability": "dev",
5049
"prefer-stable": true,
51-
"extra": {
52-
"laravel": {
53-
"providers": [
54-
"SebastiaanLuca\\BooleanDates\\BooleanDatesServiceProvider"
55-
]
56-
}
57-
},
5850
"scripts": {
5951
"composer:validate": "@composer validate --strict --ansi",
6052
"test": "vendor/bin/phpunit",

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</coverage>
2121
<testsuites>
2222
<testsuite name="Feature tests">
23-
<directory>tests/Feature</directory>
23+
<directory>tests</directory>
2424
</testsuite>
2525
</testsuites>
2626
</phpunit>

tests/BooleanAttributeTest.php

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests;
6+
7+
use Carbon\CarbonImmutable;
8+
use Illuminate\Support\Carbon;
9+
10+
class BooleanAttributeTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function it can handle initial values(): void
16+
{
17+
$model = new TestModel([
18+
'accepted_terms_at' => 1,
19+
'subscribed_to_newsletter_at' => now(),
20+
]);
21+
22+
$this->assertInstanceOf(CarbonImmutable::class, $model->accepted_terms_at);
23+
$this->assertTrue($model->has_accepted_terms);
24+
25+
$this->assertInstanceOf(Carbon::class, $model->subscribed_to_newsletter_at);
26+
$this->assertTrue($model->is_subscribed_to_newsletter);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function it can handle empty initial values(): void
33+
{
34+
$model = new TestModel;
35+
36+
$this->assertNull($model->accepted_terms_at);
37+
$this->assertFalse($model->has_accepted_terms);
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function it can enable a boolean attribute(): void
44+
{
45+
$model = new TestModel;
46+
47+
$model->has_accepted_terms = false;
48+
49+
$this->assertNull($model->accepted_terms_at);
50+
$this->assertFalse($model->has_accepted_terms);
51+
52+
$model->has_accepted_terms = true;
53+
54+
$this->assertInstanceOf(CarbonImmutable::class, $model->accepted_terms_at);
55+
$this->assertTrue($model->has_accepted_terms);
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function it can disable a boolean attribute(): void
62+
{
63+
$model = new TestModel;
64+
65+
$model->has_accepted_terms = true;
66+
67+
$this->assertInstanceOf(CarbonImmutable::class, $model->accepted_terms_at);
68+
$this->assertTrue($model->has_accepted_terms);
69+
70+
$model->has_accepted_terms = false;
71+
72+
$this->assertNull($model->accepted_terms_at);
73+
$this->assertFalse($model->has_accepted_terms);
74+
}
75+
76+
/**
77+
* @test
78+
*/
79+
public function it cannot enable a boolean attribute twice and change its date(): void
80+
{
81+
Carbon::setTestNow('2022-03-13 14:31:00');
82+
83+
$model = new TestModel;
84+
85+
$model->has_accepted_terms = true;
86+
87+
$this->assertInstanceOf(CarbonImmutable::class, $model->accepted_terms_at);
88+
$this->assertSame('2022-03-13 14:31:00', $model->accepted_terms_at->toDateTimeString());
89+
$this->assertTrue($model->has_accepted_terms);
90+
91+
$model->has_accepted_terms = true;
92+
93+
$this->assertInstanceOf(CarbonImmutable::class, $model->accepted_terms_at);
94+
$this->assertSame('2022-03-13 14:31:00', $model->accepted_terms_at->toDateTimeString());
95+
$this->assertTrue($model->has_accepted_terms);
96+
}
97+
98+
/**
99+
* @test
100+
*/
101+
public function it can enable a boolean attribute twice if it was disabled(): void
102+
{
103+
Carbon::setTestNow('2022-03-13 14:00:00');
104+
105+
$model = new TestModel;
106+
107+
$model->is_subscribed_to_newsletter = true;
108+
109+
$this->assertInstanceOf(Carbon::class, $model->subscribed_to_newsletter_at);
110+
$this->assertSame('2022-03-13 14:00:00', $model->subscribed_to_newsletter_at->toDateTimeString());
111+
$this->assertTrue($model->is_subscribed_to_newsletter);
112+
113+
Carbon::setTestNow('2022-03-20 15:00:00');
114+
115+
$model->is_subscribed_to_newsletter = false;
116+
117+
$this->assertNull($model->subscribed_to_newsletter_at);
118+
$this->assertFalse($model->is_subscribed_to_newsletter);
119+
120+
$model->is_subscribed_to_newsletter = true;
121+
122+
$this->assertInstanceOf(Carbon::class, $model->subscribed_to_newsletter_at);
123+
$this->assertSame('2022-03-20 15:00:00', $model->subscribed_to_newsletter_at->toDateTimeString());
124+
$this->assertTrue($model->is_subscribed_to_newsletter);
125+
}
126+
127+
/**
128+
* @test
129+
*/
130+
public function it can enable a boolean attribute from a boolean(): void
131+
{
132+
$model = new TestModel;
133+
134+
$model->has_accepted_terms = true;
135+
136+
$this->assertInstanceOf(CarbonImmutable::class, $model->accepted_terms_at);
137+
138+
$this->assertSame(
139+
Carbon::now()->format('Y-m-d H:i'),
140+
$model->accepted_terms_at->format('Y-m-d H:i')
141+
);
142+
}
143+
144+
/**
145+
* @test
146+
*/
147+
public function it can enable a boolean attribute from a string(): void
148+
{
149+
$model = new TestModel;
150+
151+
$model->has_accepted_terms = 'yes';
152+
153+
$this->assertSame(
154+
Carbon::now()->format('Y-m-d H:i'),
155+
$model->accepted_terms_at->format('Y-m-d H:i')
156+
);
157+
}
158+
159+
/**
160+
* @test
161+
*/
162+
public function it can enable a boolean attribute from an integer string(): void
163+
{
164+
$model = new TestModel;
165+
166+
$model->has_accepted_terms = '1';
167+
168+
$this->assertSame(
169+
Carbon::now()->format('Y-m-d H:i'),
170+
$model->accepted_terms_at->format('Y-m-d H:i')
171+
);
172+
}
173+
174+
/**
175+
* @test
176+
*/
177+
public function it can enable a boolean attribute from an integer(): void
178+
{
179+
$model = new TestModel;
180+
181+
$model->is_subscribed_to_newsletter = 1;
182+
183+
$this->assertSame(
184+
Carbon::now()->format('Y-m-d H:i'),
185+
$model->subscribed_to_newsletter_at->format('Y-m-d H:i')
186+
);
187+
}
188+
189+
/**
190+
* @test
191+
*/
192+
public function it can disable a boolean attribute from a boolean(): void
193+
{
194+
$model = new TestModel;
195+
196+
$model->allows_data_processing = false;
197+
198+
$this->assertNull($model->accepted_processing_at);
199+
}
200+
201+
/**
202+
* @test
203+
*/
204+
public function it can disable a boolean attribute from null(): void
205+
{
206+
$model = new TestModel;
207+
208+
$model->has_agreed_to_something = null;
209+
210+
$this->assertNull($model->agreed_to_something_at);
211+
}
212+
213+
/**
214+
* @test
215+
*/
216+
public function it can disable a boolean attribute from an integer string(): void
217+
{
218+
$model = new TestModel;
219+
220+
$model->has_accepted_terms = '0';
221+
222+
$this->assertNull($model->accepted_terms_at);
223+
}
224+
225+
/**
226+
* @test
227+
*/
228+
public function it can disable a boolean attribute from an integer(): void
229+
{
230+
$model = new TestModel;
231+
232+
$model->has_accepted_terms = 0;
233+
234+
$this->assertNull($model->accepted_terms_at);
235+
}
236+
237+
/**
238+
* @test
239+
*/
240+
public function it returns all attributes(): void
241+
{
242+
Carbon::setTestNow('2018-01-01 10:42:06');
243+
244+
$model = new TestModel;
245+
246+
$model->something = 'something';
247+
248+
$model->has_accepted_terms = 0;
249+
$model->is_subscribed_to_newsletter = 1;
250+
251+
$expected = [
252+
'something' => 'something',
253+
254+
'accepted_terms_at' => null,
255+
'subscribed_to_newsletter_at' => '2018-01-01T10:42:06.000000Z',
256+
257+
'has_accepted_terms' => false,
258+
'is_subscribed_to_newsletter' => true,
259+
];
260+
261+
$this->assertSame(
262+
$expected,
263+
$model->toArray(),
264+
);
265+
}
266+
}

tests/Feature/BooleanArrayTest.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)