Skip to content

Commit 24a4756

Browse files
Add tests
1 parent d7c381a commit 24a4756

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed

tests/Feature/BooleanArrayTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests\Feature;
6+
7+
use Carbon\Carbon;
8+
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
9+
use SebastiaanLuca\BooleanDates\Tests\TestCase;
10+
11+
class BooleanArrayTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it returns all boolean dates() : void
17+
{
18+
$model = new TestModel;
19+
20+
$expected = [
21+
'has_accepted_terms_and_conditions' => 'accepted_terms_at',
22+
'allows_data_processing' => 'accepted_processing_at',
23+
'has_agreed_to_something' => 'agreed_to_something_at',
24+
];
25+
26+
$this->assertSame(
27+
$expected,
28+
$model->getBooleanDates()
29+
);
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function it returns all attributes() : void
36+
{
37+
Carbon::setTestNow('2018-01-01 10:42:06');
38+
39+
$model = new TestModel;
40+
41+
$model->something = 'something';
42+
$model->tested_at = Carbon::now();
43+
44+
$model->has_accepted_terms_and_conditions = false;
45+
$model->allows_data_processing = true;
46+
$model->has_agreed_to_something = '0';
47+
48+
$expected = [
49+
'something' => 'something',
50+
'tested_at' => '2018-01-01 10:42:06',
51+
52+
'has_accepted_terms_and_conditions' => false,
53+
'allows_data_processing' => true,
54+
'has_agreed_to_something' => false,
55+
56+
'accepted_terms_at' => null,
57+
'accepted_processing_at' => '2018-01-01 10:42:06',
58+
'agreed_to_something_at' => null,
59+
];
60+
61+
$this->assertArraySubset(
62+
$expected,
63+
$model->toArray()
64+
);
65+
}
66+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests\Feature;
6+
7+
use Carbon\Carbon;
8+
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
9+
use SebastiaanLuca\BooleanDates\Tests\TestCase;
10+
11+
class BooleanAttributeTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it skips returning invalid fields() : void
17+
{
18+
$model = new TestModel;
19+
20+
$this->assertNull($model->getAttribute(null));
21+
$this->assertNull($model->getAttribute(false));
22+
$this->assertNull($model->getAttribute(''));
23+
$this->assertNull($model->getAttribute('0'));
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function it leaves other attributes untouched() : void
30+
{
31+
$model = new TestModel;
32+
33+
$model->something = 'something';
34+
35+
$this->assertSame(
36+
'something',
37+
$model->something
38+
);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function it sets the date from a true boolean() : void
45+
{
46+
$model = new TestModel;
47+
48+
$model->has_accepted_terms_and_conditions = true;
49+
50+
$this->assertSame(
51+
Carbon::now()->format('Y-m-d H:i:s'),
52+
$model->accepted_terms_at
53+
);
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function it sets the date from a non empty string() : void
60+
{
61+
$model = new TestModel;
62+
63+
$model->has_accepted_terms_and_conditions = 'yes';
64+
65+
$this->assertSame(
66+
Carbon::now()->format('Y-m-d H:i:s'),
67+
$model->accepted_terms_at
68+
);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function it sets the date from a positive integer value() : void
75+
{
76+
$model = new TestModel;
77+
78+
$model->has_agreed_to_something = 1;
79+
80+
$this->assertSame(
81+
Carbon::now()->format('Y-m-d H:i:s'),
82+
$model->agreed_to_something_at
83+
);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function it sets the date from a positive integer string value() : void
90+
{
91+
$model = new TestModel;
92+
93+
$model->has_accepted_terms_and_conditions = '1';
94+
95+
$this->assertSame(
96+
Carbon::now()->format('Y-m-d H:i:s'),
97+
$model->accepted_terms_at
98+
);
99+
}
100+
101+
/**
102+
* @test
103+
*/
104+
public function it clears the date from a false boolean() : void
105+
{
106+
$model = new TestModel;
107+
108+
$model->allows_data_processing = false;
109+
110+
$this->assertNull($model->accepted_processing_at);
111+
}
112+
113+
/**
114+
* @test
115+
*/
116+
public function it clears the date from null() : void
117+
{
118+
$model = new TestModel;
119+
120+
$model->has_agreed_to_something = null;
121+
122+
$this->assertNull($model->agreed_to_something_at);
123+
}
124+
125+
/**
126+
* @test
127+
*/
128+
public function it clears the date from a false string value() : void
129+
{
130+
$model = new TestModel;
131+
132+
$model->has_accepted_terms_and_conditions = '0';
133+
134+
$this->assertNull($model->accepted_terms_at);
135+
}
136+
137+
/**
138+
* @test
139+
*/
140+
public function it clears the date from an empty string value() : void
141+
{
142+
$model = new TestModel;
143+
144+
$model->has_accepted_terms_and_conditions = '0';
145+
146+
$this->assertNull($model->accepted_terms_at);
147+
}
148+
}

tests/Feature/BooleanDateTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests\Feature;
6+
7+
use Carbon\Carbon;
8+
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
9+
use SebastiaanLuca\BooleanDates\Tests\TestCase;
10+
11+
class BooleanDateTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it leaves other date attributes untouched() : void
17+
{
18+
$model = new TestModel;
19+
20+
$model->tested_at = Carbon::now('+5 days');
21+
22+
$this->assertSame(
23+
Carbon::now('+5 days')->format('Y-m-d H:i:s'),
24+
$model->tested_at->format('Y-m-d H:i:s')
25+
);
26+
}
27+
28+
/**
29+
* @test
30+
*/
31+
public function it returns a date object from a true attribute() : void
32+
{
33+
$model = new TestModel;
34+
35+
$model->allows_data_processing = true;
36+
37+
$this->assertInstanceOf(
38+
Carbon::class,
39+
$model->accepted_processing_at
40+
);
41+
42+
$this->assertEquals(
43+
Carbon::now()->format('Y-m-d H:i:s'),
44+
$model->accepted_processing_at->format('Y-m-d H:i:s')
45+
);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function it returns null from a false attribute() : void
52+
{
53+
$model = new TestModel;
54+
55+
$model->allows_data_processing = false;
56+
57+
$this->assertNull($model->accepted_processing_at);
58+
}
59+
}

tests/resources/TestModel.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ class TestModel extends Model
1212
use BooleanDates;
1313

1414
/**
15+
* Set the date of fields to the current date and time if a counterpart boolean field is
16+
* true-ish.
17+
*
18+
* Keys and values should be in the format: `'boolean_field' => 'internal_timestamp_field'`.
19+
*
1520
* @var array
1621
*/
1722
protected $booleanDates = [
1823
'has_accepted_terms_and_conditions' => 'accepted_terms_at',
1924
'allows_data_processing' => 'accepted_processing_at',
2025
'has_agreed_to_something' => 'agreed_to_something_at',
2126
];
27+
28+
/**
29+
* The attributes that should be mutated to dates.
30+
*
31+
* @var array
32+
*/
33+
protected $dates = [
34+
'accepted_processing_at',
35+
'tested_at',
36+
];
2237
}

0 commit comments

Comments
 (0)