|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binarcode\LaravelMailator\Tests\Feature; |
| 4 | + |
| 5 | +use Binarcode\LaravelMailator\Models\MailatorSchedule; |
| 6 | +use Binarcode\LaravelMailator\Tests\Fixtures\PrivatePropertyMailable; |
| 7 | +use Binarcode\LaravelMailator\Tests\Fixtures\ProtectedPropertyMailable; |
| 8 | +use Binarcode\LaravelMailator\Tests\Fixtures\PublicPropertyMailable; |
| 9 | +use Binarcode\LaravelMailator\Tests\TestCase; |
| 10 | +use Illuminate\Support\Facades\Mail; |
| 11 | +use RuntimeException; |
| 12 | + |
| 13 | +class AllowNonPublicPropertiesTest extends TestCase |
| 14 | +{ |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + |
| 20 | + Mail::fake(); |
| 21 | + } |
| 22 | + |
| 23 | + public function test_can_send_email_with_private_property(): void |
| 24 | + { |
| 25 | + config()->set('mailator.serialization.enforce_public_properties', false); |
| 26 | + |
| 27 | + MailatorSchedule::init('private') |
| 28 | + ->mailable(new PrivatePropertyMailable('test')) |
| 29 | + ->execute(); |
| 30 | + |
| 31 | + Mail::assertSent(PrivatePropertyMailable::class); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_can_not_send_email_with_private_property(): void |
| 35 | + { |
| 36 | + config()->set('mailator.serialization.enforce_public_properties', true); |
| 37 | + |
| 38 | + $this->expectException(RuntimeException::class); |
| 39 | + $this->expectExceptionMessage('Mailable contains non-public constructor properties which cannot be safely serialized'); |
| 40 | + |
| 41 | + MailatorSchedule::init('private') |
| 42 | + ->mailable(new PrivatePropertyMailable('test')) |
| 43 | + ->execute(); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_can_send_email_with_protected_property(): void |
| 47 | + { |
| 48 | + config()->set('mailator.serialization.enforce_public_properties', false); |
| 49 | + |
| 50 | + MailatorSchedule::init('protected') |
| 51 | + ->mailable(new ProtectedPropertyMailable('test')) |
| 52 | + ->execute(); |
| 53 | + |
| 54 | + Mail::assertSent(ProtectedPropertyMailable::class); |
| 55 | + } |
| 56 | + |
| 57 | + public function test_can_not_send_email_with_protected_property(): void |
| 58 | + { |
| 59 | + config()->set('mailator.serialization.enforce_public_properties', true); |
| 60 | + |
| 61 | + $this->expectException(RuntimeException::class); |
| 62 | + $this->expectExceptionMessage('Mailable contains non-public constructor properties which cannot be safely serialized'); |
| 63 | + |
| 64 | + MailatorSchedule::init('protected') |
| 65 | + ->mailable(new ProtectedPropertyMailable('test')) |
| 66 | + ->execute(); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_can_send_email_with_public_property(): void |
| 70 | + { |
| 71 | + config()->set('mailator.serialization.enforce_public_properties', true); |
| 72 | + |
| 73 | + MailatorSchedule::init('protected') |
| 74 | + ->mailable(new PublicPropertyMailable('test')) |
| 75 | + ->execute(); |
| 76 | + |
| 77 | + Mail::assertSent(PublicPropertyMailable::class); |
| 78 | + |
| 79 | + config()->set('mailator.serialization.enforce_public_properties', false); |
| 80 | + |
| 81 | + MailatorSchedule::init('protected') |
| 82 | + ->mailable(new PublicPropertyMailable('test')) |
| 83 | + ->execute(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments