Skip to content

Commit ef9b6b3

Browse files
committed
TDD remaining statement lexers
1 parent f9b43cb commit ef9b6b3

File tree

5 files changed

+223
-3
lines changed

5 files changed

+223
-3
lines changed

src/Lexers/StatementLexer.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
use Blueprint\Contracts\Lexer;
77
use Blueprint\Models\Statements\DispatchStatement;
8+
use Blueprint\Models\Statements\EloquentStatement;
89
use Blueprint\Models\Statements\FireStatement;
10+
use Blueprint\Models\Statements\RedirectStatement;
911
use Blueprint\Models\Statements\SendStatement;
1012
use Blueprint\Models\Statements\QueryStatement;
1113
use Blueprint\Models\Statements\RenderStatement;
14+
use Blueprint\Models\Statements\SessionStatement;
1215
use Blueprint\Models\Statements\ValidateStatement;
16+
use Illuminate\Support\Facades\Redirect;
1317

1418
class StatementLexer implements Lexer
1519
{
@@ -38,6 +42,18 @@ public function analyze(array $tokens): array
3842
case 'validate':
3943
$statements[] = $this->analyzeValidate($statement);
4044
break;
45+
case 'redirect':
46+
$statements[] = $this->analyzeRedirect($statement);
47+
break;
48+
case 'save':
49+
case 'update':
50+
case 'delete':
51+
$statements[] = new EloquentStatement($command, $statement);
52+
break;
53+
case 'flash':
54+
case 'store':
55+
$statements[] = new SessionStatement($command, $statement);
56+
break;
4157
}
4258
}
4359

@@ -65,6 +81,13 @@ private function analyzeDispatch(string $statement)
6581
return new DispatchStatement($job, $data);
6682
}
6783

84+
private function analyzeRedirect(string $statement)
85+
{
86+
[$route, $data] = $this->parseWithStatement($statement);
87+
88+
return new RedirectStatement($route, $data);
89+
}
90+
6891
private function parseWithStatement(string $statement)
6992
{
7093
[$object, $with] = $this->extractTokens($statement, 2);
@@ -107,6 +130,4 @@ private function extractTokens(string $statement, int $limit)
107130
{
108131
return array_pad(preg_split('/[ \t]+/', $statement, $limit), $limit, null);
109132
}
110-
111-
112133
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
4+
namespace Blueprint\Models\Statements;
5+
6+
7+
class EloquentStatement
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $operation;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $reference;
18+
19+
public function __construct(string $operation, string $reference)
20+
{
21+
$this->operation = $operation;
22+
$this->reference = $reference;
23+
}
24+
25+
public function operation(): string
26+
{
27+
return $this->operation;
28+
}
29+
30+
public function reference(): string
31+
{
32+
return $this->reference;
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
4+
namespace Blueprint\Models\Statements;
5+
6+
7+
class RedirectStatement
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $route;
13+
14+
/**
15+
* @var array
16+
*/
17+
private $data;
18+
19+
public function __construct(string $route, array $data = [])
20+
{
21+
$this->route = $route;
22+
$this->data = $data;
23+
}
24+
25+
public function route()
26+
{
27+
return $this->route;
28+
}
29+
30+
/**
31+
* @return array
32+
*/
33+
public function data(): array
34+
{
35+
return $this->data;
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
4+
namespace Blueprint\Models\Statements;
5+
6+
7+
class SessionStatement
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $operation;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $reference;
18+
19+
public function __construct(string $operation, string $reference)
20+
{
21+
$this->operation = $operation;
22+
$this->reference = $reference;
23+
}
24+
25+
public function operation(): string
26+
{
27+
return $this->operation;
28+
}
29+
30+
public function reference(): string
31+
{
32+
return $this->reference;
33+
}
34+
}

tests/Feature/Lexers/StatementLexerTest.php

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Blueprint\Lexers\StatementLexer;
66
use Blueprint\Models\Statements\DispatchStatement;
7+
use Blueprint\Models\Statements\EloquentStatement;
78
use Blueprint\Models\Statements\FireStatement;
8-
use Blueprint\Models\Statements\SendStatement;
9+
use Blueprint\Models\Statements\RedirectStatement;
910
use Blueprint\Models\Statements\RenderStatement;
11+
use Blueprint\Models\Statements\SendStatement;
12+
use Blueprint\Models\Statements\SessionStatement;
1013
use Blueprint\Models\Statements\ValidateStatement;
1114
use PHPUnit\Framework\TestCase;
1215

@@ -235,4 +238,95 @@ public function it_returns_a_validate_statement()
235238

236239
$this->assertSame(['title', 'author_id', 'content'], $actual[0]->data());
237240
}
241+
242+
/**
243+
* @test
244+
* @dataProvider eloquentTokensProvider
245+
*/
246+
public function it_returns_an_eloquent_statement($operation, $reference)
247+
{
248+
$tokens = [
249+
$operation => $reference
250+
];
251+
252+
$actual = $this->subject->analyze($tokens);
253+
254+
$this->assertCount(1, $actual);
255+
$this->assertInstanceOf(EloquentStatement::class, $actual[0]);
256+
257+
$this->assertSame($operation, $actual[0]->operation());
258+
$this->assertSame($reference, $actual[0]->reference());
259+
}
260+
261+
/**
262+
* @test
263+
* @dataProvider sessionTokensProvider
264+
*/
265+
public function it_returns_a_session_statement($operation, $reference)
266+
{
267+
$tokens = [
268+
$operation => $reference
269+
];
270+
271+
$actual = $this->subject->analyze($tokens);
272+
273+
$this->assertCount(1, $actual);
274+
$this->assertInstanceOf(SessionStatement::class, $actual[0]);
275+
276+
$this->assertSame($operation, $actual[0]->operation());
277+
$this->assertSame($reference, $actual[0]->reference());
278+
}
279+
280+
/**
281+
* @test
282+
*/
283+
public function it_returns_a_redirect_statement()
284+
{
285+
$tokens = [
286+
'redirect' => 'route.index'
287+
];
288+
289+
$actual = $this->subject->analyze($tokens);
290+
291+
$this->assertCount(1, $actual);
292+
$this->assertInstanceOf(RedirectStatement::class, $actual[0]);
293+
294+
$this->assertEquals('route.index', $actual[0]->route());
295+
$this->assertSame([], $actual[0]->data());
296+
}
297+
298+
/**
299+
* @test
300+
*/
301+
public function it_returns_a_redirect_statement_with_data()
302+
{
303+
$tokens = [
304+
'redirect' => 'route.show with:foo, bar, baz'
305+
];
306+
307+
$actual = $this->subject->analyze($tokens);
308+
309+
$this->assertCount(1, $actual);
310+
$this->assertInstanceOf(RedirectStatement::class, $actual[0]);
311+
312+
$this->assertEquals('route.show', $actual[0]->route());
313+
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
314+
}
315+
316+
public function eloquentTokensProvider()
317+
{
318+
return [
319+
['save', 'post'],
320+
['update', 'post'],
321+
['delete', 'post.id'],
322+
];
323+
}
324+
325+
public function sessionTokensProvider()
326+
{
327+
return [
328+
['flash', 'post.title'],
329+
['store', 'post.id'],
330+
];
331+
}
238332
}

0 commit comments

Comments
 (0)