22
33namespace Enqueue \Tests \Client ;
44
5+ use Enqueue \Client \Amqp \RabbitMqDriver ;
6+ use Enqueue \Client \DriverInterface ;
57use Enqueue \Client \Resources ;
8+ use Enqueue \Redis \Client \RedisDriver ;
69use PHPUnit \Framework \TestCase ;
710
811class ResourcesTest extends TestCase
@@ -20,4 +23,124 @@ public function testShouldConstructorBePrivate()
2023
2124 $ this ->assertTrue ($ rc ->getConstructor ()->isPrivate ());
2225 }
26+
27+ public function testShouldGetAvailableDriverInExpectedFormat ()
28+ {
29+ $ availableDrivers = Resources ::getAvailableDrivers ();
30+
31+ $ this ->assertInternalType ('array ' , $ availableDrivers );
32+ $ this ->assertArrayHasKey (RedisDriver::class, $ availableDrivers );
33+
34+ $ driverInfo = $ availableDrivers [RedisDriver::class];
35+ $ this ->assertArrayHasKey ('schemes ' , $ driverInfo );
36+ $ this ->assertSame (['redis ' ], $ driverInfo ['schemes ' ]);
37+
38+ $ this ->assertArrayHasKey ('requiredSchemeExtensions ' , $ driverInfo );
39+ $ this ->assertSame ([], $ driverInfo ['requiredSchemeExtensions ' ]);
40+
41+ $ this ->assertArrayHasKey ('package ' , $ driverInfo );
42+ $ this ->assertSame ('enqueue/redis ' , $ driverInfo ['package ' ]);
43+ }
44+
45+ public function testShouldGetAvailableDriverWithRequiredExtensionInExpectedFormat ()
46+ {
47+ $ availableDrivers = Resources ::getAvailableDrivers ();
48+
49+ $ this ->assertInternalType ('array ' , $ availableDrivers );
50+ $ this ->assertArrayHasKey (RabbitMqDriver::class, $ availableDrivers );
51+
52+ $ driverInfo = $ availableDrivers [RabbitMqDriver::class];
53+ $ this ->assertArrayHasKey ('schemes ' , $ driverInfo );
54+ $ this ->assertSame (['amqp ' ], $ driverInfo ['schemes ' ]);
55+
56+ $ this ->assertArrayHasKey ('requiredSchemeExtensions ' , $ driverInfo );
57+ $ this ->assertSame (['rabbitmq ' ], $ driverInfo ['requiredSchemeExtensions ' ]);
58+
59+ $ this ->assertArrayHasKey ('package ' , $ driverInfo );
60+ $ this ->assertSame ('enqueue/enqueue ' , $ driverInfo ['package ' ]);
61+ }
62+
63+ public function testShouldGetKnownDriversInExpectedFormat ()
64+ {
65+ $ knownDrivers = Resources ::getKnownDrivers ();
66+
67+ $ this ->assertInternalType ('array ' , $ knownDrivers );
68+ $ this ->assertArrayHasKey (RedisDriver::class, $ knownDrivers );
69+
70+ $ driverInfo = $ knownDrivers [RedisDriver::class];
71+ $ this ->assertArrayHasKey ('schemes ' , $ driverInfo );
72+ $ this ->assertSame (['redis ' ], $ driverInfo ['schemes ' ]);
73+
74+ $ this ->assertArrayHasKey ('requiredSchemeExtensions ' , $ driverInfo );
75+ $ this ->assertSame ([], $ driverInfo ['requiredSchemeExtensions ' ]);
76+ }
77+
78+ public function testThrowsIfDriverClassNotImplementDriverFactoryInterfaceOnAddDriver ()
79+ {
80+ $ this ->expectException (\InvalidArgumentException::class);
81+ $ this ->expectExceptionMessage ('The driver class "stdClass" must implement "Enqueue\Client\DriverInterface" interface. ' );
82+
83+ Resources ::addDriver (\stdClass::class, [], [], 'foo ' );
84+ }
85+
86+ public function testThrowsIfNoSchemesProvidedOnAddDriver ()
87+ {
88+ $ driverClass = $ this ->getMockClass (DriverInterface::class);
89+
90+ $ this ->expectException (\InvalidArgumentException::class);
91+ $ this ->expectExceptionMessage ('Schemes could not be empty. ' );
92+
93+ Resources ::addDriver ($ driverClass , [], [], 'foo ' );
94+ }
95+
96+ public function testThrowsIfNoPackageProvidedOnAddDriver ()
97+ {
98+ $ driverClass = $ this ->getMockClass (DriverInterface::class);
99+
100+ $ this ->expectException (\InvalidArgumentException::class);
101+ $ this ->expectExceptionMessage ('Package name could not be empty. ' );
102+
103+ Resources ::addDriver ($ driverClass , ['foo ' ], [], '' );
104+ }
105+
106+ public function testShouldAllowRegisterDriverThatIsNotInstalled ()
107+ {
108+ Resources ::addDriver ('theDriverClass ' , ['foo ' ], ['barExtension ' ], 'foo ' );
109+
110+ $ knownDrivers = Resources ::getKnownDrivers ();
111+ $ this ->assertInternalType ('array ' , $ knownDrivers );
112+ $ this ->assertArrayHasKey ('theDriverClass ' , $ knownDrivers );
113+
114+ $ availableDrivers = Resources ::getAvailableDrivers ();
115+
116+ $ this ->assertInternalType ('array ' , $ availableDrivers );
117+ $ this ->assertArrayNotHasKey ('theDriverClass ' , $ availableDrivers );
118+ }
119+
120+ public function testShouldAllowGetPreviouslyRegisteredDriver ()
121+ {
122+ $ driverClass = $ this ->getMockClass (DriverInterface::class);
123+
124+ Resources ::addDriver (
125+ $ driverClass ,
126+ ['fooscheme ' , 'barscheme ' ],
127+ ['fooextension ' , 'barextension ' ],
128+ 'foo/bar '
129+ );
130+
131+ $ availableDrivers = Resources ::getAvailableDrivers ();
132+
133+ $ this ->assertInternalType ('array ' , $ availableDrivers );
134+ $ this ->assertArrayHasKey ($ driverClass , $ availableDrivers );
135+
136+ $ driverInfo = $ availableDrivers [$ driverClass ];
137+ $ this ->assertArrayHasKey ('schemes ' , $ driverInfo );
138+ $ this ->assertSame (['fooscheme ' , 'barscheme ' ], $ driverInfo ['schemes ' ]);
139+
140+ $ this ->assertArrayHasKey ('requiredSchemeExtensions ' , $ driverInfo );
141+ $ this ->assertSame (['fooextension ' , 'barextension ' ], $ driverInfo ['requiredSchemeExtensions ' ]);
142+
143+ $ this ->assertArrayHasKey ('package ' , $ driverInfo );
144+ $ this ->assertSame ('foo/bar ' , $ driverInfo ['package ' ]);
145+ }
23146}
0 commit comments