|
| 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 | +} |
0 commit comments