Skip to content

Commit 8ebffb4

Browse files
committed
Improve client error message + add missing factory in facade + more tests
1 parent 71a8bdc commit 8ebffb4

15 files changed

+308
-41
lines changed

Makefile

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: analyze fix-code test coverage validation integration-test
1+
.PHONY: analyze fix-code test test-with-integration coverage coverage-with-integration validation integration-test integration-coverage
22

33
analyze: | vendor
44
$(COMPOSER) exec -v parallel-lint -- src
@@ -18,14 +18,25 @@ fix-code: | vendor
1818
test: | vendor
1919
$(COMPOSER) exec -v phpunit
2020

21+
test-with-integration: | vendor
22+
$(COMPOSER) exec -v phpunit -- --group default,integration
23+
2124
coverage: | vendor
2225
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
2326
$(COMPOSER) exec -v phpunit -- --coverage-text --color
2427

28+
coverage-with-integration: | vendor
29+
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
30+
$(COMPOSER) exec -v phpunit -- --group default,integration --coverage-text --color
31+
2532
integration-test: | vendor
2633
$(COMPOSER) exec -v phpunit -- --group integration
2734

28-
validation: fix-code analyze test coverage integration-test
35+
integration-coverage: | vendor
36+
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
37+
$(COMPOSER) exec -v phpunit -- --group integration --coverage-text --color
38+
39+
validation: fix-code analyze test-with-integration coverage-with-integration
2940

3041
vendor: composer.json
3142
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist

src/Redis/Client/AbstractClient.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use function count;
2525
use MacFJA\RediSearch\Redis\Client;
2626
use MacFJA\RediSearch\Redis\Command;
27+
use function strlen;
2728

2829
abstract class AbstractClient implements Client
2930
{
@@ -71,7 +72,7 @@ protected function getMissingMessage(string $name, bool $isExtension, array $cla
7172
if (true === $isExtension) {
7273
$message = 'The extension '.$name.' is missing.'.PHP_EOL.'Install the extension';
7374
}
74-
$message .= ' or use a polyfill that provide ';
75+
$message .= ' or use a polyfill';
7576

7677
$classesMessage = null;
7778
if (1 === count($classes)) {
@@ -92,8 +93,11 @@ protected function getMissingMessage(string $name, bool $isExtension, array $cla
9293
$functionsMessage = 'the functions "'.implode('", "', $functions).'"';
9394
}
9495

95-
$message .= implode(' and ', array_filter([$classesMessage, $methodsMessage, $functionsMessage]));
96-
$message .= '.';
96+
$additional = implode(' and ', array_filter([$classesMessage, $methodsMessage, $functionsMessage]));
97+
if (strlen($additional) > 0) {
98+
$additional = ' that provide '.$additional;
99+
}
100+
$message .= $additional.'.';
97101

98102
return $message;
99103
}

src/Redis/Client/AmpRedisClient.php

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
use MacFJA\RediSearch\Redis\Command;
3030
use RuntimeException;
3131

32-
/**
33-
* @codeCoverageIgnore
34-
*/
3532
class AmpRedisClient extends AbstractClient
3633
{
3734
/** @var Redis */

src/Redis/Client/CheprasovRedisClient.php

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
use RedisClient\Pipeline\PipelineInterface;
2929
use RuntimeException;
3030

31-
/**
32-
* @codeCoverageIgnore
33-
*/
3431
class CheprasovRedisClient extends AbstractClient
3532
{
3633
/** @var AbstractRedisClient */

src/Redis/Client/ClientFacade.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
use Rediska as RediskaRedis;
3232
use RuntimeException;
3333

34-
/**
35-
* @codeCoverageIgnore
36-
*/
3734
class ClientFacade
3835
{
3936
/** @var array<string> */
@@ -46,6 +43,7 @@ class ClientFacade
4643
RedisentClient::class,
4744
RediskaClient::class,
4845
AmpRedisClient::class,
46+
TinyRedisClient::class,
4947
];
5048

5149
/**

src/Redis/Client/CredisClient.php

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
use MacFJA\RediSearch\Redis\Command;
2828
use RuntimeException;
2929

30-
/**
31-
* @codeCoverageIgnore
32-
*/
3330
class CredisClient extends AbstractClient
3431
{
3532
/** @var Credis_Client */

src/Redis/Client/PredisClient.php

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
use Predis\Command\RawCommand;
3030
use RuntimeException;
3131

32-
/**
33-
* @codeCoverageIgnore
34-
*/
3532
class PredisClient extends AbstractClient
3633
{
3734
/** @var ClientInterface */

src/Redis/Client/RedisentClient.php

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
use redisent\Redis;
2727
use RuntimeException;
2828

29-
/**
30-
* @codeCoverageIgnore
31-
*/
3229
class RedisentClient extends AbstractClient
3330
{
3431
/** @var Redis */

src/Redis/Client/RediskaClient.php

-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
use Rediska_Connection_Exec;
3232
use RuntimeException;
3333

34-
/**
35-
* @codeCoverageIgnore
36-
*/
3734
class RediskaClient extends AbstractClient
3835
{
3936
/** @var Rediska */

src/Redis/Client/TinyRedisClient.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
use MacFJA\RediSearch\Redis\Command;
2626
use RuntimeException;
2727

28-
/**
29-
* @codeCoverageIgnore
30-
*/
3128
class TinyRedisClient extends AbstractClient
3229
{
3330
/** @var \TinyRedisClient */
@@ -80,6 +77,9 @@ public static function make($redis): Client
8077
return new self($redis);
8178
}
8279

80+
/**
81+
* @codeCoverageIgnore
82+
*/
8383
protected function doPipeline(Command ...$commands): array
8484
{
8585
return [];

src/Redis/Response/PaginatedResponse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function getTotalCount(): int
128128

129129
public function count()
130130
{
131-
return $this->totalCount;
131+
return $this->getTotalCount();
132132
}
133133

134134
private function updateWithLimit(int $offset, int $size): void
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\Redis\Client;
23+
24+
use MacFJA\RediSearch\tests\fixtures\Redis\Client\FakeAbstractClient;
25+
use PHPUnit\Framework\TestCase;
26+
27+
/**
28+
* @covers \MacFJA\RediSearch\Redis\Client\AbstractClient
29+
*
30+
* @internal
31+
*/
32+
class AbstractClientTest extends TestCase
33+
{
34+
public function testGetMissingMessage(): void
35+
{
36+
$fake = new FakeAbstractClient();
37+
38+
static::assertSame(
39+
'The dependency foo is missing.'.PHP_EOL.'Install the dependency or use a polyfill.',
40+
$fake->exposeGetMissingMessage('foo', false, [])
41+
);
42+
43+
static::assertSame(
44+
'The dependency bar is missing.'.PHP_EOL.'Install the dependency or use a polyfill that provide the class "baz" and the method "baz::hello".',
45+
$fake->exposeGetMissingMessage('bar', false, ['baz' => ['hello']])
46+
);
47+
48+
static::assertSame(
49+
'The dependency baz is missing.'.PHP_EOL.'Install the dependency or use a polyfill that provide the function "foobar".',
50+
$fake->exposeGetMissingMessage('baz', false, [], ['foobar'])
51+
);
52+
53+
static::assertSame(
54+
'The dependency DEP is missing.'.PHP_EOL.'Install the dependency or use a polyfill that provide the classes "C", "CL" and the methods "C::m", "C::e", "CL::t", "CL::h" and the functions "f", "u".',
55+
$fake->exposeGetMissingMessage('DEP', false, ['C' => ['m', 'e'], 'CL' => ['t', 'h']], ['f', 'u'])
56+
);
57+
58+
static::assertSame(
59+
'The extension foobar is missing.'.PHP_EOL.'Install the extension or use a polyfill.',
60+
$fake->exposeGetMissingMessage('foobar', true, [])
61+
);
62+
}
63+
}
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\Redis\Client;
23+
24+
use function get_class;
25+
use MacFJA\RediSearch\Redis\Client;
26+
use MacFJA\RediSearch\Redis\Client\ClientFacade;
27+
use MacFJA\RediSearch\Redis\Command;
28+
use PHPUnit\Framework\TestCase;
29+
use RuntimeException;
30+
use stdClass;
31+
32+
/**
33+
* @covers \MacFJA\RediSearch\Redis\Client\ClientFacade
34+
*
35+
* @uses \MacFJA\RediSearch\Redis\Client\AbstractClient
36+
* @uses \MacFJA\RediSearch\Redis\Client\AmpRedisClient
37+
* @uses \MacFJA\RediSearch\Redis\Client\CheprasovRedisClient
38+
* @uses \MacFJA\RediSearch\Redis\Client\PredisClient
39+
* @uses \MacFJA\RediSearch\Redis\Client\RedisentClient
40+
* @uses \MacFJA\RediSearch\Redis\Client\RediskaClient
41+
* @uses \MacFJA\RediSearch\Redis\Client\CredisClient
42+
* @uses \MacFJA\RediSearch\Redis\Client\TinyRedisClient
43+
*
44+
* @internal
45+
*/
46+
class ClientFacadeTest extends TestCase
47+
{
48+
public function testGetClientError(): void
49+
{
50+
$this->expectException(RuntimeException::class);
51+
$this->expectExceptionMessage('Unable to handle the Redis connection');
52+
53+
$facade = new ClientFacade();
54+
$facade->getClient(new stdClass());
55+
}
56+
57+
public function testAddFactory(): void
58+
{
59+
$facade = new ClientFacade();
60+
static::assertFalse($facade->addFactory('Foobar'));
61+
62+
static::assertTrue($facade->addFactory(get_class(new class() implements Client {
63+
public static function make($redis): Client
64+
{
65+
return new self();
66+
}
67+
68+
public function execute(Command $command): void
69+
{
70+
// Do nothing
71+
}
72+
73+
public function executeRaw(...$args): void
74+
{
75+
// Do nothing
76+
}
77+
78+
public function pipeline(Command ...$commands): array
79+
{
80+
return [];
81+
}
82+
83+
public static function supports($redis): bool
84+
{
85+
return false;
86+
}
87+
})));
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\fixtures\Redis\Client;
23+
24+
use MacFJA\RediSearch\Redis\Client;
25+
use MacFJA\RediSearch\Redis\Command;
26+
27+
class FakeAbstractClient extends \MacFJA\RediSearch\Redis\Client\AbstractClient
28+
{
29+
public static function make($redis): Client
30+
{
31+
return new self();
32+
}
33+
34+
public function execute(Command $command): void
35+
{
36+
// Do nothing
37+
}
38+
39+
public function executeRaw(...$args): void
40+
{
41+
// Do nothing
42+
}
43+
44+
public static function supports($redis): bool
45+
{
46+
return false;
47+
}
48+
49+
/**
50+
* @param array<string,array<string>> $classesMethods
51+
* @param array<string> $functions
52+
*/
53+
public function exposeGetMissingMessage(string $name, bool $isExtension, array $classesMethods, array $functions = []): string
54+
{
55+
return $this->getMissingMessage($name, $isExtension, $classesMethods, $functions);
56+
}
57+
58+
protected function doPipeline(Command ...$commands): array
59+
{
60+
return [];
61+
}
62+
}

0 commit comments

Comments
 (0)