Skip to content

Commit 3aed7c9

Browse files
authored
Support notification via model (#253)
1 parent 0d82e12 commit 3aed7c9

17 files changed

+370
-29
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ private function buildMethods(Controller $controller)
102102
foreach ($statements as $statement) {
103103
if ($statement instanceof SendStatement) {
104104
$body .= self::INDENT . $statement->output() . PHP_EOL;
105-
if ($statement->type() === 'notification') {
105+
if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) {
106106
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification');
107107
$this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail());
108-
} else {
108+
} elseif ($statement->type() === SendStatement::TYPE_MAIL) {
109109
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail');
110110
$this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail());
111111
}

src/Generators/Statements/MailGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function output(array $tree): array
3232
continue;
3333
}
3434

35-
if ($statement->type() !== 'mail') {
35+
if ($statement->type() !== SendStatement::TYPE_MAIL) {
3636
continue;
3737
}
3838

src/Generators/Statements/NotificationGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public function output(array $tree): array
3333
continue;
3434
}
3535

36-
if ($statement->type() !== 'notification') {
36+
if ($statement->type() !== SendStatement::TYPE_NOTIFICATION_WITH_FACADE
37+
&& $statement->type() !== SendStatement::TYPE_NOTIFICATION_WITH_MODEL
38+
) {
3739
continue;
3840
}
3941

src/Generators/TestGenerator.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,25 @@ protected function buildTestCases(Controller $controller)
116116

117117
foreach ($statements as $statement) {
118118
if ($statement instanceof SendStatement) {
119-
if ($statement->type() === 'notification') {
119+
if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE
120+
|| $statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_MODEL
121+
) {
120122
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification');
121123
$this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail());
122124

123125
$setup['mock'][] = 'Notification::fake();';
124126

125-
$assertion = sprintf('Notification::assertSent(%s::class', $statement->mail());
127+
$assertion = sprintf(
128+
'Notification::assertSentTo($%s, %s::class',
129+
str_replace('.', '->', $statement->to()),
130+
$statement->mail()
131+
);
126132

127-
if ($statement->data() || $statement->to()) {
133+
if ($statement->data()) {
128134
$conditions = [];
129135
$variables = [];
130136
$assertion .= ', function ($notification)';
131137

132-
if ($statement->to()) {
133-
$conditions[] = '$notification->hasTo($' . str_replace('.', '->', $statement->to()) . ')';
134-
}
135-
136138
foreach ($statement->data() as $data) {
137139
if (Str::studly(Str::singular($data)) === $context) {
138140
$variables[] .= '$' . $data;

src/Lexers/StatementLexer.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function analyze(array $tokens): array
4040
case 'send':
4141
$statements[] = $this->analyzeSend($statement);
4242
break;
43+
case 'notify':
44+
$statements[] = $this->analyzeNotify($statement);
45+
break;
4346
case 'validate':
4447
$statements[] = $this->analyzeValidate($statement);
4548
break;
@@ -120,14 +123,26 @@ private function analyzeSend($statement)
120123
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
121124
}
122125

123-
$type = 'mail';
126+
$type = SendStatement::TYPE_MAIL;
124127
if (Str::endsWith($object, 'Notification')) {
125-
$type = 'notification';
128+
$type = SendStatement::TYPE_NOTIFICATION_WITH_FACADE;
126129
}
127130

128131
return new SendStatement($object, $to, $data, $type);
129132
}
130133

134+
private function analyzeNotify($statement)
135+
{
136+
[$model, $notification, $with] = $this->extractTokens($statement, 3);
137+
138+
$data = [];
139+
if (!empty($with)) {
140+
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
141+
}
142+
143+
return new SendStatement($notification, $model, $data, SendStatement::TYPE_NOTIFICATION_WITH_MODEL);
144+
}
145+
131146
private function analyzeValidate($statement)
132147
{
133148
return new ValidateStatement(preg_split('/,([ \t]+)?/', $statement));

src/Models/Statements/SendStatement.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
class SendStatement
77
{
8+
const TYPE_MAIL = 'mail';
9+
10+
const TYPE_NOTIFICATION_WITH_FACADE = 'notification_with_facade';
11+
12+
const TYPE_NOTIFICATION_WITH_MODEL = 'notification_with_model';
13+
814
/**
915
* @var string
1016
*/
@@ -58,7 +64,15 @@ public function data(): array
5864

5965
public function output()
6066
{
61-
return $this->type() === 'mail' ? $this->mailOutput() : $this->notificationOutput();
67+
if ($this->type() === self::TYPE_NOTIFICATION_WITH_FACADE) {
68+
return $this->notificationFacadeOutput();
69+
}
70+
71+
if ($this->type() === self::TYPE_NOTIFICATION_WITH_MODEL) {
72+
return $this->notificationModelOutput();
73+
}
74+
75+
return $this->mailOutput();
6276
}
6377

6478
private function mailOutput()
@@ -80,7 +94,7 @@ private function mailOutput()
8094
return $code;
8195
}
8296

83-
private function notificationOutput()
97+
private function notificationFacadeOutput()
8498
{
8599
$code = 'Notification::';
86100

@@ -97,6 +111,24 @@ private function notificationOutput()
97111
return $code;
98112
}
99113

114+
private function notificationModelOutput()
115+
{
116+
$code = '';
117+
118+
if ($this->to()) {
119+
$code .= sprintf('$%s->', str_replace('.', '->', $this->to()));
120+
$code .= 'notify(new ' . $this->mail() . '(';
121+
}
122+
123+
if ($this->data()) {
124+
$code .= $this->buildParameters($this->data());
125+
}
126+
127+
$code .= '));';
128+
129+
return $code;
130+
}
131+
100132
private function buildParameters(array $data)
101133
{
102134
$parameters = array_map(function ($parameter) {

tests/Feature/Generator/ControllerGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public function controllerTreeDataProvider()
167167
{
168168
return [
169169
['drafts/readme-example.yaml', 'app/Http/Controllers/PostController.php', 'controllers/readme-example.php'],
170+
['drafts/readme-example-notification-facade.yaml', 'app/Http/Controllers/PostController.php', 'controllers/readme-example-notification-facade.php'],
171+
['drafts/readme-example-notification-model.yaml', 'app/Http/Controllers/PostController.php', 'controllers/readme-example-notification-model.php'],
170172
['drafts/crazy-eloquent.yaml', 'app/Http/Controllers/PostController.php', 'controllers/crazy-eloquent.php'],
171173
['drafts/nested-components.yaml', 'app/Http/Controllers/Admin/UserController.php', 'controllers/nested-components.php'],
172174
['drafts/respond-statements.yaml', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'],

tests/Feature/Generator/Statements/NotificationGeneratorTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,19 @@ public function output_writes_nothing_tree_without_validate_statements()
6464

6565
/**
6666
* @test
67+
* @dataProvider notificationDraftProvider
6768
*/
68-
public function output_writes_notifications()
69+
public function output_writes_notifications($draft)
6970
{
7071
$this->files->expects('stub')
7172
->with('notification.stub')
7273
->andReturn(file_get_contents('stubs/notification.stub'));
7374

74-
$this->files->expects('stub')
75-
->with('partials/constructor.stub')
76-
->andReturn(file_get_contents('stubs/partials/constructor.stub'));
77-
78-
// var_dump($this->fixture('notifications/review-post.php'));die();
75+
if ($draft === 'drafts/send-statements-notification-facade.yaml') {
76+
$this->files->expects('stub')
77+
->with('partials/constructor.stub')
78+
->andReturn(file_get_contents('stubs/partials/constructor.stub'));
79+
}
7980

8081
$this->files->shouldReceive('exists')
8182
->twice()
@@ -95,9 +96,7 @@ public function output_writes_notifications()
9596
$this->files->expects('put')
9697
->with('app/Notification/PublishedPostNotification.php', $this->fixture('notifications/published-post.php'));
9798

98-
99-
100-
$tokens = $this->blueprint->parse($this->fixture('drafts/send-statements-notification.yaml'));
99+
$tokens = $this->blueprint->parse($this->fixture($draft));
101100
$tree = $this->blueprint->analyze($tokens);
102101

103102
$this->assertEquals(['created' => ['app/Notification/ReviewPostNotification.php', 'app/Notification/PublishedPostNotification.php']], $this->subject->output($tree));
@@ -119,7 +118,7 @@ public function it_only_outputs_new_notifications()
119118
->with('app/Notification/PublishedPostNotification.php')
120119
->andReturnTrue();
121120

122-
$tokens = $this->blueprint->parse($this->fixture('drafts/send-statements-notification.yaml'));
121+
$tokens = $this->blueprint->parse($this->fixture('drafts/send-statements-notification-facade.yaml'));
123122
$tree = $this->blueprint->analyze($tokens);
124123

125124
$this->assertEquals([], $this->subject->output($tree));
@@ -148,9 +147,17 @@ public function it_respects_configuration()
148147
$this->files->expects('put')
149148
->with('src/path/Notification/ReviewNotification.php', $this->fixture('notifications/notification-configured.php'));
150149

151-
$tokens = $this->blueprint->parse($this->fixture('drafts/readme-example-notification.yaml'));
150+
$tokens = $this->blueprint->parse($this->fixture('drafts/readme-example-notification-facade.yaml'));
152151
$tree = $this->blueprint->analyze($tokens);
153152

154153
$this->assertEquals(['created' => ['src/path/Notification/ReviewNotification.php']], $this->subject->output($tree));
155154
}
155+
156+
public function notificationDraftProvider()
157+
{
158+
return [
159+
['drafts/send-statements-notification-facade.yaml'],
160+
['drafts/send-statements-notification-model.yaml']
161+
];
162+
}
156163
}

tests/Feature/Generator/TestGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public function controllerTreeDataProvider()
176176
{
177177
return [
178178
['drafts/readme-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example.php'],
179-
['drafts/readme-example-notification.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
179+
['drafts/readme-example-notification-facade.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
180+
['drafts/readme-example-notification-model.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
180181
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
181182
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example.php'],
182183
];

0 commit comments

Comments
 (0)