Skip to content

Commit e4fe1ba

Browse files
committed
Merge pull request #37 from mnapoli/cli-advices
Show help message when bind/type yields empty list
2 parents 10e9be6 + b6dec4c commit e4fe1ba

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/Handler/BindCommandHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function handleList(Args $args, IO $io)
8080
$printBindingState = count($bindingStates) > 1;
8181
$printPackageName = count($packageNames) > 1;
8282
$printHeaders = $printBindingState || $printPackageName;
83+
$printAdvice = true;
8384
$indentation = $printBindingState && $printPackageName ? 8
8485
: ($printBindingState || $printPackageName ? 4 : 0);
8586

@@ -96,6 +97,8 @@ public function handleList(Args $args, IO $io)
9697
continue;
9798
}
9899

100+
$printAdvice = false;
101+
99102
if (!$bindingStatePrinted) {
100103
$this->printBindingStateHeader($io, $bindingState);
101104
$bindingStatePrinted = true;
@@ -115,6 +118,10 @@ public function handleList(Args $args, IO $io)
115118
}
116119
}
117120

121+
if ($printAdvice) {
122+
$io->writeLine('No bindings found. Use "puli bind <artifact> <type>" to bind an artifact to a type.');
123+
}
124+
118125
return 0;
119126
}
120127

src/Handler/TypeCommandHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public function handleList(Args $args, IO $io)
7373
$printStates = count($states) > 1;
7474
$printPackageName = count($packageNames) > 1;
7575
$printHeaders = $printStates || $printPackageName;
76-
$printAdvice = false;
76+
$printTypeAdvice = true;
77+
$printBindAdvice = false;
7778
$indentation = $printStates && $printPackageName ? 8
7879
: ($printStates || $printPackageName ? 4 : 0);
7980

@@ -90,12 +91,14 @@ public function handleList(Args $args, IO $io)
9091
continue;
9192
}
9293

94+
$printTypeAdvice = false;
95+
9396
if (!$statePrinted) {
9497
$this->printBindingTypeState($io, $state);
9598
$statePrinted = true;
9699

97100
// Only print the advice if at least one type was printed
98-
$printAdvice = true;
101+
$printBindAdvice = true;
99102
}
100103

101104
if ($printPackageName) {
@@ -114,7 +117,10 @@ public function handleList(Args $args, IO $io)
114117
}
115118
}
116119

117-
if ($printAdvice) {
120+
if ($printTypeAdvice) {
121+
$io->writeLine('No types defined. Use "puli type --define <name>" to define a type.');
122+
}
123+
if ($printBindAdvice) {
118124
$io->writeLine('Use "puli bind <resource> <type>" to bind a resource to a type.');
119125
}
120126

tests/Handler/BindCommandHandlerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,22 @@ public function testListBindingsWithParameters()
757757
$this->assertEmpty($this->io->fetchErrors());
758758
}
759759

760+
public function testListNoBindings()
761+
{
762+
$args = self::$listCommand->parseArgs(new StringArgs(''));
763+
764+
$statusCode = $this->handler->handleList($args, $this->io);
765+
766+
$expected = <<<EOF
767+
No bindings found. Use "puli bind <artifact> <type>" to bind an artifact to a type.
768+
769+
EOF;
770+
771+
$this->assertSame(0, $statusCode);
772+
$this->assertSame($expected, $this->io->fetchOutput());
773+
$this->assertEmpty($this->io->fetchErrors());
774+
}
775+
760776
public function testAddResourceBindingWithRelativePath()
761777
{
762778
$args = self::$addCommand->parseArgs(new StringArgs('path Puli\Cli\Tests\Fixtures\Foo'));

tests/Handler/TypeCommandHandlerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,28 @@ public function testListEnabledTypesInPackage()
448448
$this->assertEmpty($this->io->fetchErrors());
449449
}
450450

451+
public function testListNoTypes()
452+
{
453+
$this->discoveryManager = $this->getMock('Puli\Manager\Api\Discovery\DiscoveryManager');
454+
$this->discoveryManager->expects($this->any())
455+
->method('findTypeDescriptors')
456+
->willReturn(array());
457+
$this->handler = new TypeCommandHandler($this->discoveryManager, $this->packages);
458+
459+
$args = self::$listCommand->parseArgs(new StringArgs(''));
460+
461+
$statusCode = $this->handler->handleList($args, $this->io);
462+
463+
$expected = <<<EOF
464+
No types defined. Use "puli type --define <name>" to define a type.
465+
466+
EOF;
467+
468+
$this->assertSame(0, $statusCode);
469+
$this->assertSame($expected, $this->io->fetchOutput());
470+
$this->assertEmpty($this->io->fetchErrors());
471+
}
472+
451473
public function testDefineType()
452474
{
453475
$args = self::$defineCommand->parseArgs(new StringArgs('my/type'));

0 commit comments

Comments
 (0)