Skip to content

Commit 13a4c93

Browse files
authored
Merge pull request #97 from cfong63/dump-with-metadata
Add `--with-metadata` support for workflow:dump command
2 parents 97cbe03 + 2da3b24 commit 13a4c93

File tree

3 files changed

+92
-51
lines changed

3 files changed

+92
-51
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ You can change the image format with the `--format` option. By default the forma
573573

574574
php artisan workflow:dump workflow_name --format=jpg
575575

576+
Similar to [Symfony](https://symfony.com/doc/current/workflow/dumping-workflows.html#styling). You can use `--with-metadata` to include workflow's metadata
577+
578+
php artisan workflow:dump workflow_name --with-metadata
579+
576580
If you would like to output to a different directory than root, you can use the `--disk` and `--path` options to set the Storage disk (`local` by default) and path (`root_path()` by default).
577581

578582
php artisan workflow:dump workflow-name --class=App\\BlogPost --disk=s3 --path="workflows/diagrams/"

src/Commands/WorkflowDumpCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class WorkflowDumpCommand extends Command
2727
{--class= : the support class name}
2828
{--format=png : the image format}
2929
{--disk=local : the storage disk name}
30-
{--path= : the optional path within selected disk}';
30+
{--path= : the optional path within selected disk}
31+
{--with-metadata : dumps metadata beneath the label }';
3132

3233
/**
3334
* The console command description.
@@ -50,6 +51,7 @@ public function handle()
5051
$config = Config::get('workflow');
5152
$disk = $this->option('disk');
5253
$optionalPath = $this->option('path');
54+
$withMetadata = $this->option('with-metadata');
5355

5456
if ($disk === 'local') {
5557
$optionalPath ??= '.';
@@ -69,6 +71,10 @@ public function handle()
6971
' Please specify a valid support class with the --class option.');
7072
}
7173

74+
$dumperOptions = [
75+
'with-metadata' => $withMetadata,
76+
];
77+
7278
$subject = new $class();
7379
$workflow = Workflow::get($subject, $workflowName);
7480
$definition = $workflow->getDefinition();
@@ -83,7 +89,7 @@ public function handle()
8389

8490
$process = new Process($dotCommand);
8591
$process->setWorkingDirectory($path);
86-
$process->setInput($dumper->dump($definition));
92+
$process->setInput($dumper->dump($definition, options: $dumperOptions));
8793
$process->mustRun();
8894
}
8995
}

tests/WorkflowDumpCommandTest.php

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,113 +4,144 @@
44

55
use Mockery;
66
use Illuminate\Support\Facades\Storage;
7+
use Mockery\MockInterface;
78
use ZeroDaHero\LaravelWorkflow\Commands\WorkflowDumpCommand;
89

910
class WorkflowDumpCommandTest extends BaseWorkflowTestCase
1011
{
1112
public function testShouldThrowExceptionForUndefinedWorkflow()
1213
{
13-
$command = Mockery::mock(WorkflowDumpCommand::class)
14-
->makePartial()
15-
->shouldReceive('argument')
16-
->with('workflow')
17-
->andReturn('fake')
18-
->shouldReceive('option')
19-
->with('format')
20-
->andReturn('png')
21-
->shouldReceive('option')
22-
->with('class')
23-
->andReturn('Tests\Fixtures\TestObject')
24-
->shouldReceive('option')
25-
->with('disk')
26-
->andReturn('local')
27-
->shouldReceive('option')
28-
->with('path')
29-
->andReturn('/')
30-
->getMock();
14+
$command = $this->getMock(workflow: 'fake');
3115

3216
$this->expectException(\Exception::class);
3317
$this->expectExceptionMessage('Workflow fake is not configured.');
18+
3419
$command->handle();
3520
}
3621

3722
public function testShouldThrowExceptionForUndefinedClass()
3823
{
39-
$command = Mockery::mock(WorkflowDumpCommand::class)
40-
->makePartial()
41-
->shouldReceive('argument')
42-
->with('workflow')
43-
->andReturn('straight')
44-
->shouldReceive('option')
45-
->with('format')
46-
->andReturn('png')
47-
->shouldReceive('option')
48-
->with('class')
49-
->andReturn('Tests\Fixtures\FakeObject')
50-
->shouldReceive('option')
51-
->with('disk')
52-
->andReturn('local')
53-
->shouldReceive('option')
54-
->with('path')
55-
->andReturn('/')
56-
->getMock();
24+
$command = $this->getMock(class: 'Tests\Fixtures\FakeObject');
5725

5826
$this->expectException(\Exception::class);
5927
$this->expectExceptionMessage('Workflow straight has no support for' .
6028
' class Tests\Fixtures\FakeObject. Please specify a valid support' .
6129
' class with the --class option.');
30+
6231
$command->handle();
6332
}
6433

6534
public function testWorkflowCommand()
6635
{
6736
$optionalPath = '/my/path';
68-
$disk = 'public';
37+
$disk = 'public';
6938

7039
Storage::fake($disk);
7140

7241
if (Storage::disk($disk)->exists($optionalPath . '/straight.png')) {
7342
Storage::disk($disk)->delete($optionalPath . '/straight.png');
7443
}
7544

76-
$command = Mockery::mock(WorkflowDumpCommand::class)
45+
$command = $this->getMock(disk: $disk, path: $optionalPath);
46+
$command->handle();
47+
48+
Storage::disk($disk)->assertExists($optionalPath . '/straight.png');
49+
}
50+
51+
public function testWorkflowCommandWithMetadata()
52+
{
53+
$disk = 'public';
54+
55+
Storage::fake($disk);
56+
57+
$command = $this->getMock(
58+
disk: $disk,
59+
format: 'svg',
60+
withMetadata: true,
61+
);
62+
63+
$command->handle();
64+
65+
Storage::disk($disk)->assertExists('straight.svg');
66+
$svg_file = Storage::disk($disk)->get('straight.svg');
67+
$this->assertStringContainsString('metadata_place', $svg_file);
68+
$this->assertStringContainsString('metadata_exists', $svg_file);
69+
}
70+
71+
public function testWorkflowCommandWithoutMetadata()
72+
{
73+
$disk = 'public';
74+
75+
Storage::fake($disk);
76+
77+
$command = $this->getMock(
78+
disk: $disk,
79+
format: 'svg',
80+
withMetadata: false,
81+
);
82+
83+
$command->handle();
84+
85+
Storage::disk($disk)->assertExists('straight.svg');
86+
$svg_file = Storage::disk($disk)->get('straight.svg');
87+
$this->assertStringContainsString('metadata_place', $svg_file);
88+
$this->assertStringNotContainsString('metadata_exists', $svg_file);
89+
}
90+
91+
private function getMock(
92+
string $workflow = 'straight',
93+
string $format = 'png',
94+
string $class = 'Tests\Fixtures\TestObject',
95+
string $disk = 'local',
96+
string $path = '/',
97+
bool $withMetadata = false,
98+
): MockInterface {
99+
return Mockery::mock(WorkflowDumpCommand::class)
77100
->makePartial()
78101
->shouldReceive('argument')
79102
->with('workflow')
80-
->andReturn('straight')
103+
->andReturn($workflow)
81104
->shouldReceive('option')
82105
->with('format')
83-
->andReturn('png')
106+
->andReturn($format)
84107
->shouldReceive('option')
85108
->with('class')
86-
->andReturn('Tests\Fixtures\TestObject')
109+
->andReturn($class)
87110
->shouldReceive('option')
88111
->with('disk')
89112
->andReturn($disk)
90113
->shouldReceive('option')
91114
->with('path')
92-
->andReturn($optionalPath)
115+
->andReturn($path)
116+
->shouldReceive('option')
117+
->with('with-metadata')
118+
->andReturn($withMetadata)
93119
->getMock();
94-
95-
$command->handle();
96-
97-
Storage::disk($disk)->assertExists($optionalPath . '/straight.png');
98120
}
99121

100122
protected function getEnvironmentSetUp($app)
101123
{
102124
$app['config']['workflow'] = [
103125
'straight' => [
104126
'supports' => ['Tests\Fixtures\TestObject'],
105-
'places' => ['a', 'b', 'c'],
127+
'places' => [
128+
'a',
129+
'b',
130+
'c',
131+
'metadata_place' => [
132+
'metadata' => [
133+
'metadata_exists' => true,
134+
],
135+
],
136+
],
106137
'transitions' => [
107138
't1' => [
108139
'from' => 'a',
109-
'to' => 'b',
140+
'to' => 'b',
110141
],
111142
't2' => [
112143
'from' => 'b',
113-
'to' => 'c',
144+
'to' => 'c',
114145
],
115146
],
116147
],

0 commit comments

Comments
 (0)