Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ private function buildMethods(Controller $controller)
foreach ($statements as $statement) {
if ($statement instanceof SendStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
if ($statement->type() === 'notification') {
if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) {
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification');
$this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail());
} else {
} elseif ($statement->type() === SendStatement::TYPE_MAIL) {
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail');
$this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Statements/MailGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function output(array $tree): array
continue;
}

if ($statement->type() !== 'mail') {
if ($statement->type() !== SendStatement::TYPE_MAIL) {
continue;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Generators/Statements/NotificationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public function output(array $tree): array
continue;
}

if ($statement->type() !== 'notification') {
if ($statement->type() !== SendStatement::TYPE_NOTIFICATION_WITH_FACADE
&& $statement->type() !== SendStatement::TYPE_NOTIFICATION_WITH_MODEL
) {
continue;
}

Expand Down
16 changes: 9 additions & 7 deletions src/Generators/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,25 @@ protected function buildTestCases(Controller $controller)

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

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

$assertion = sprintf('Notification::assertSent(%s::class', $statement->mail());
$assertion = sprintf(
'Notification::assertSentTo($%s, %s::class',
str_replace('.', '->', $statement->to()),
$statement->mail()
);

if ($statement->data() || $statement->to()) {
if ($statement->data()) {
$conditions = [];
$variables = [];
$assertion .= ', function ($notification)';

if ($statement->to()) {
$conditions[] = '$notification->hasTo($' . str_replace('.', '->', $statement->to()) . ')';
}

foreach ($statement->data() as $data) {
if (Str::studly(Str::singular($data)) === $context) {
$variables[] .= '$' . $data;
Expand Down
19 changes: 17 additions & 2 deletions src/Lexers/StatementLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function analyze(array $tokens): array
case 'send':
$statements[] = $this->analyzeSend($statement);
break;
case 'notify':
$statements[] = $this->analyzeNotify($statement);
break;
case 'validate':
$statements[] = $this->analyzeValidate($statement);
break;
Expand Down Expand Up @@ -120,14 +123,26 @@ private function analyzeSend($statement)
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
}

$type = 'mail';
$type = SendStatement::TYPE_MAIL;
if (Str::endsWith($object, 'Notification')) {
$type = 'notification';
$type = SendStatement::TYPE_NOTIFICATION_WITH_FACADE;
}

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

private function analyzeNotify($statement)
{
[$model, $notification, $with] = $this->extractTokens($statement, 3);

$data = [];
if (!empty($with)) {
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
}

return new SendStatement($notification, $model, $data, SendStatement::TYPE_NOTIFICATION_WITH_MODEL);
}

private function analyzeValidate($statement)
{
return new ValidateStatement(preg_split('/,([ \t]+)?/', $statement));
Expand Down
36 changes: 34 additions & 2 deletions src/Models/Statements/SendStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

class SendStatement
{
const TYPE_MAIL = 'mail';

const TYPE_NOTIFICATION_WITH_FACADE = 'notification_with_facade';

const TYPE_NOTIFICATION_WITH_MODEL = 'notification_with_model';

/**
* @var string
*/
Expand Down Expand Up @@ -58,7 +64,15 @@ public function data(): array

public function output()
{
return $this->type() === 'mail' ? $this->mailOutput() : $this->notificationOutput();
if ($this->type() === self::TYPE_NOTIFICATION_WITH_FACADE) {
return $this->notificationFacadeOutput();
}

if ($this->type() === self::TYPE_NOTIFICATION_WITH_MODEL) {
return $this->notificationModelOutput();
}

return $this->mailOutput();
}

private function mailOutput()
Expand All @@ -80,7 +94,7 @@ private function mailOutput()
return $code;
}

private function notificationOutput()
private function notificationFacadeOutput()
{
$code = 'Notification::';

Expand All @@ -97,6 +111,24 @@ private function notificationOutput()
return $code;
}

private function notificationModelOutput()
{
$code = '';

if ($this->to()) {
$code .= sprintf('$%s->', str_replace('.', '->', $this->to()));
$code .= 'notify(new ' . $this->mail() . '(';
}

if ($this->data()) {
$code .= $this->buildParameters($this->data());
}

$code .= '));';

return $code;
}

private function buildParameters(array $data)
{
$parameters = array_map(function ($parameter) {
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Generator/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public function controllerTreeDataProvider()
{
return [
['drafts/readme-example.yaml', 'app/Http/Controllers/PostController.php', 'controllers/readme-example.php'],
['drafts/readme-example-notification-facade.yaml', 'app/Http/Controllers/PostController.php', 'controllers/readme-example-notification-facade.php'],
['drafts/readme-example-notification-model.yaml', 'app/Http/Controllers/PostController.php', 'controllers/readme-example-notification-model.php'],
['drafts/crazy-eloquent.yaml', 'app/Http/Controllers/PostController.php', 'controllers/crazy-eloquent.php'],
['drafts/nested-components.yaml', 'app/Http/Controllers/Admin/UserController.php', 'controllers/nested-components.php'],
['drafts/respond-statements.yaml', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'],
Expand Down
29 changes: 18 additions & 11 deletions tests/Feature/Generator/Statements/NotificationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,19 @@ public function output_writes_nothing_tree_without_validate_statements()

/**
* @test
* @dataProvider notificationDraftProvider
*/
public function output_writes_notifications()
public function output_writes_notifications($draft)
{
$this->files->expects('stub')
->with('notification.stub')
->andReturn(file_get_contents('stubs/notification.stub'));

$this->files->expects('stub')
->with('partials/constructor.stub')
->andReturn(file_get_contents('stubs/partials/constructor.stub'));

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

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



$tokens = $this->blueprint->parse($this->fixture('drafts/send-statements-notification.yaml'));
$tokens = $this->blueprint->parse($this->fixture($draft));
$tree = $this->blueprint->analyze($tokens);

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

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

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

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

$this->assertEquals(['created' => ['src/path/Notification/ReviewNotification.php']], $this->subject->output($tree));
}

public function notificationDraftProvider()
{
return [
['drafts/send-statements-notification-facade.yaml'],
['drafts/send-statements-notification-model.yaml']
];
}
}
3 changes: 2 additions & 1 deletion tests/Feature/Generator/TestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public function controllerTreeDataProvider()
{
return [
['drafts/readme-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example.php'],
['drafts/readme-example-notification.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
['drafts/readme-example-notification-facade.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
['drafts/readme-example-notification-model.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example.php'],
];
Expand Down
Loading