-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from php-enqueue/bundle-better-dx-when-transp…
…ort-is-missing [bundle][dx] Add a message that suggest installing a pkg to use the transport.
- Loading branch information
Showing
4 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Enqueue\Symfony; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class MissingTransportFactory implements TransportFactoryInterface, DriverFactoryInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
/** | ||
* @var string[] | ||
*/ | ||
private $packages; | ||
|
||
/** | ||
* @param string $name | ||
* @param string[] $packages | ||
*/ | ||
public function __construct($name, array $packages) | ||
{ | ||
$this->name = $name; | ||
$this->packages = $packages; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addConfiguration(ArrayNodeDefinition $builder) | ||
{ | ||
if (1 == count($this->packages)) { | ||
$message = sprintf( | ||
'In order to use the transport "%s" install a package "%s"', | ||
$this->getName(), | ||
implode('", "', $this->packages) | ||
); | ||
} else { | ||
$message = sprintf( | ||
'In order to use the transport "%s" install one of the packages "%s"', | ||
$this->getName(), | ||
implode('", "', $this->packages) | ||
); | ||
} | ||
|
||
$builder | ||
->info($message) | ||
->beforeNormalization() | ||
->always(function () { | ||
return []; | ||
}) | ||
->end() | ||
->validate() | ||
->always(function () use ($message) { | ||
throw new \InvalidArgumentException($message); | ||
}) | ||
->end() | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createConnectionFactory(ContainerBuilder $container, array $config) | ||
{ | ||
throw new \LogicException('Should not be called'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createContext(ContainerBuilder $container, array $config) | ||
{ | ||
throw new \LogicException('Should not be called'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createDriver(ContainerBuilder $container, array $config) | ||
{ | ||
throw new \LogicException('Should not be called'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Enqueue\Tests\Symfony; | ||
|
||
use Enqueue\Symfony\MissingTransportFactory; | ||
use Enqueue\Symfony\TransportFactoryInterface; | ||
use Enqueue\Test\ClassExtensionTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
use Symfony\Component\Config\Definition\Processor; | ||
|
||
class MissingTransportFactoryTest extends TestCase | ||
{ | ||
use ClassExtensionTrait; | ||
|
||
public function testShouldImplementTransportFactoryInterface() | ||
{ | ||
$this->assertClassImplements(TransportFactoryInterface::class, MissingTransportFactory::class); | ||
} | ||
|
||
public function testCouldBeConstructedWithNameAndPackages() | ||
{ | ||
$transport = new MissingTransportFactory('aMissingTransportName', ['aPackage', 'anotherPackage']); | ||
|
||
$this->assertEquals('aMissingTransportName', $transport->getName()); | ||
} | ||
|
||
public function testThrowOnProcessForOnePackageToInstall() | ||
{ | ||
$transport = new MissingTransportFactory('aMissingTransportName', ['aFooPackage']); | ||
$tb = new TreeBuilder(); | ||
$rootNode = $tb->root('foo'); | ||
|
||
$transport->addConfiguration($rootNode); | ||
$processor = new Processor(); | ||
|
||
$this->expectException(InvalidConfigurationException::class); | ||
$this->expectExceptionMessage('Invalid configuration for path "foo": In order to use the transport "aMissingTransportName" install a package "aFooPackage"'); | ||
$processor->process($tb->buildTree(), [[]]); | ||
} | ||
|
||
public function testThrowOnProcessForSeveralPackagesToInstall() | ||
{ | ||
$transport = new MissingTransportFactory('aMissingTransportName', ['aFooPackage', 'aBarPackage']); | ||
$tb = new TreeBuilder(); | ||
$rootNode = $tb->root('foo'); | ||
|
||
$transport->addConfiguration($rootNode); | ||
$processor = new Processor(); | ||
|
||
$this->expectException(InvalidConfigurationException::class); | ||
$this->expectExceptionMessage('Invalid configuration for path "foo": In order to use the transport "aMissingTransportName" install one of the packages "aFooPackage", "aBarPackage"'); | ||
$processor->process($tb->buildTree(), [[]]); | ||
} | ||
|
||
public function testThrowEvenIfThereAreSomeOptionsPassed() | ||
{ | ||
$transport = new MissingTransportFactory('aMissingTransportName', ['aFooPackage', 'aBarPackage']); | ||
$tb = new TreeBuilder(); | ||
$rootNode = $tb->root('foo'); | ||
|
||
$transport->addConfiguration($rootNode); | ||
$processor = new Processor(); | ||
|
||
$this->expectException(InvalidConfigurationException::class); | ||
$this->expectExceptionMessage('In order to use the transport "aMissingTransportName"'); | ||
$processor->process($tb->buildTree(), [[ | ||
'foo' => 'fooVal', | ||
'bar' => 'barVal', | ||
]]); | ||
} | ||
} |