Skip to content

Commit f16fcc8

Browse files
committed
Add files
0 parents  commit f16fcc8

39 files changed

+2218
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vendor/
2+
.phpunit.result.cache
3+
composer.lock
4+
coverage*
5+
phpunit.xml

.php_cs.dist

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tarantool\PhpUnit;
6+
7+
use PhpCsFixer\Config;
8+
9+
$header = <<<EOF
10+
This file is part of the tarantool/phpunit-extras package.
11+
12+
(c) Eugene Leonovich <[email protected]>
13+
14+
For the full copyright and license information, please view the LICENSE
15+
file that was distributed with this source code.
16+
EOF;
17+
18+
return Config::create()
19+
->setUsingCache(false)
20+
->setRiskyAllowed(true)
21+
->setRules([
22+
'@Symfony' => true,
23+
'@Symfony:risky' => true,
24+
'array_syntax' => ['syntax' => 'short'],
25+
'binary_operator_spaces' => ['operators' => ['=' => null, '=>' => null]],
26+
'declare_strict_types' => true,
27+
'no_useless_else' => true,
28+
'no_useless_return' => true,
29+
'ordered_imports' => [
30+
'sort_algorithm' => 'alpha',
31+
'imports_order' => ['class', 'function', 'const'],
32+
],
33+
'phpdoc_align' => false,
34+
'phpdoc_order' => true,
35+
'phpdoc_to_comment' => false,
36+
'phpdoc_separation' => false, // do not separate @param and @psalm-param
37+
'return_type_declaration' => ['space_before' => 'one'],
38+
'strict_comparison' => true,
39+
'header_comment' => [
40+
'comment_type' => 'PHPDoc',
41+
'header' => $header,
42+
'location' => 'after_open',
43+
'separate' => 'both',
44+
],
45+
])
46+
;

composer.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "tarantool/phpunit-extras",
3+
"description": "A collection of helpers for PHPUnit to ease testing Tarantool libraries.",
4+
"keywords": ["tarantool", "phpunit", "annotations", "assertions", "expectations", "extensions", "mocking"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Eugene Leonovich",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.1",
15+
"composer/semver": "^1.5",
16+
"ocramius/package-versions": "^1.4",
17+
"rybakit/msgpack": "^0.7.0",
18+
"rybakit/phpunit-extras": "dev-master",
19+
"symfony/expression-language": "^3|^4|^5",
20+
"tarantool/client": "^0.7.0"
21+
},
22+
"require-dev": {
23+
"php": "^7.1.3",
24+
"friendsofphp/php-cs-fixer": "^2.14",
25+
"vimeo/psalm": "^3.9"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Tarantool\\PhpUnit\\": "src/"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Tarantool\\PhpUnit\\Tests\\": "tests/"
35+
}
36+
},
37+
"config": {
38+
"preferred-install": {
39+
"*": "dist"
40+
},
41+
"sort-packages": true
42+
}
43+
}

phpunit-extension.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
colors="true"
6+
verbose="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<php>
10+
<ini name="date.timezone" value="UTC" />
11+
<ini name="display_errors" value="On" />
12+
<ini name="display_startup_errors" value="On" />
13+
<ini name="error_reporting" value="E_ALL" />
14+
</php>
15+
16+
<testsuites>
17+
<testsuite name="annotation">
18+
<file>tests/Annotation/AnnotationExtensionTest.php</file>
19+
</testsuite>
20+
</testsuites>
21+
22+
<filter>
23+
<whitelist>
24+
<directory>src</directory>
25+
</whitelist>
26+
</filter>
27+
28+
<extensions>
29+
<extension class="Tarantool\PhpUnit\Tests\Annotation\MockAnnotationExtension">
30+
<arguments><array>
31+
<element key="bool">
32+
<boolean>true</boolean>
33+
</element>
34+
<element key="int">
35+
<integer>42</integer>
36+
</element>
37+
<element key="str">
38+
<string>foobar</string>
39+
</element>
40+
</array></arguments>
41+
</extension>
42+
</extensions>
43+
</phpunit>

phpunit.xml.dist

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
colors="true"
6+
verbose="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<php>
10+
<ini name="date.timezone" value="UTC" />
11+
<ini name="display_errors" value="On" />
12+
<ini name="display_startup_errors" value="On" />
13+
<ini name="error_reporting" value="E_ALL" />
14+
</php>
15+
16+
<testsuites>
17+
<testsuite name="unit">
18+
<directory>tests</directory>
19+
<exclude>tests/Annotation/AnnotationExtensionTest.php</exclude>
20+
</testsuite>
21+
</testsuites>
22+
23+
<filter>
24+
<whitelist>
25+
<directory>src</directory>
26+
</whitelist>
27+
</filter>
28+
</phpunit>

psalm.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
totallyTyped="false"
4+
errorLevel="3"
5+
resolveFromConfigFile="true"
6+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
xmlns="https://getpsalm.org/schema/config"
8+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
9+
>
10+
<projectFiles>
11+
<directory name="src" />
12+
<ignoreFiles>
13+
<directory name="vendor" />
14+
</ignoreFiles>
15+
</projectFiles>
16+
</psalm>
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/phpunit-extras package.
5+
*
6+
* (c) Eugene Leonovich <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\PhpUnit\Annotation;
15+
16+
use PHPUnit\Exception;
17+
use PHPUnitExtras\Annotation\AnnotationExtension as BaseAnnotationExtension;
18+
use Tarantool\Client\Client;
19+
20+
class AnnotationExtension extends BaseAnnotationExtension
21+
{
22+
use Annotations;
23+
24+
/** @var array<string, string|int|bool>|string */
25+
private $clientConfig;
26+
27+
/** @var Client|null */
28+
private $client;
29+
30+
/**
31+
* @param array<string, string|int|bool>|string $clientConfig
32+
*/
33+
public function __construct($clientConfig = 'tcp://127.0.0.1:3301')
34+
{
35+
$this->clientConfig = $clientConfig;
36+
}
37+
38+
protected function getClient() : Client
39+
{
40+
if ($this->client) {
41+
return $this->client;
42+
}
43+
44+
$config = $this->getClientConfig();
45+
46+
return $this->client = \is_string($config)
47+
? Client::fromDsn($config)
48+
: Client::fromOptions($config);
49+
}
50+
51+
/**
52+
* @return array<string, string|int|bool>|string
53+
*/
54+
final protected function getClientConfig(bool $resolveEnvVars = true)
55+
{
56+
if (!$resolveEnvVars) {
57+
return $this->clientConfig;
58+
}
59+
60+
if (\is_string($this->clientConfig)) {
61+
return self::resolveEnvValues($this->clientConfig);
62+
}
63+
64+
$clientConfig = $this->clientConfig;
65+
foreach ($clientConfig as $key => $value) {
66+
$clientConfig[$key] = \is_string($value) ? self::resolveEnvValues($value) : $value;
67+
}
68+
69+
return $clientConfig;
70+
}
71+
72+
private static function resolveEnvValues(string $configValue) : string
73+
{
74+
return preg_replace_callback('/%env\((?P<name>.+?)\)%/', static function (array $matches) : string {
75+
if (false !== $value = getenv($matches['name'])) {
76+
return $value;
77+
}
78+
79+
$errorMessage = sprintf('Environment variable "%s" does not exist', $matches['name']);
80+
throw new class($errorMessage) extends \RuntimeException implements Exception {
81+
};
82+
}, $configValue);
83+
}
84+
}

src/Annotation/Annotations.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/phpunit-extras package.
5+
*
6+
* (c) Eugene Leonovich <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\PhpUnit\Annotation;
15+
16+
use PHPUnitExtras\Annotation\AnnotationProcessorBuilder;
17+
use PHPUnitExtras\Annotation\Annotations as BaseAnnotations;
18+
use Tarantool\Client\Client;
19+
use Tarantool\PhpUnit\Annotation\Processor\LuaProcessor;
20+
use Tarantool\PhpUnit\Annotation\Processor\SqlProcessor;
21+
use Tarantool\PhpUnit\Annotation\Requirement\ClientPackerRequirement;
22+
use Tarantool\PhpUnit\Annotation\Requirement\LuaConditionRequirement;
23+
use Tarantool\PhpUnit\Annotation\Requirement\TarantoolVersionRequirement;
24+
25+
trait Annotations
26+
{
27+
use BaseAnnotations {
28+
BaseAnnotations::createAnnotationProcessorBuilder as createBaseAnnotationProcessorBuilder;
29+
}
30+
31+
protected function createAnnotationProcessorBuilder() : AnnotationProcessorBuilder
32+
{
33+
$client = $this->getClient();
34+
35+
return $this->createBaseAnnotationProcessorBuilder()
36+
->addProcessor(new LuaProcessor($client))
37+
->addProcessor(new SqlProcessor($client))
38+
->addRequirement(new ClientPackerRequirement($client))
39+
->addRequirement(new LuaConditionRequirement($client))
40+
->addRequirement(new TarantoolVersionRequirement($client))
41+
;
42+
}
43+
44+
abstract protected function getClient() : Client;
45+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/phpunit-extras package.
5+
*
6+
* (c) Eugene Leonovich <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\PhpUnit\Annotation\Processor;
15+
16+
use PHPUnitExtras\Annotation\Processor\Processor;
17+
use Tarantool\Client\Client;
18+
19+
final class LuaProcessor implements Processor
20+
{
21+
private $client;
22+
23+
public function __construct(Client $client)
24+
{
25+
$this->client = $client;
26+
}
27+
28+
public function getName() : string
29+
{
30+
return 'lua';
31+
}
32+
33+
public function process(string $value) : void
34+
{
35+
$this->client->evaluate($value);
36+
}
37+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/phpunit-extras package.
5+
*
6+
* (c) Eugene Leonovich <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\PhpUnit\Annotation\Processor;
15+
16+
use PHPUnitExtras\Annotation\Processor\Processor;
17+
use Tarantool\Client\Client;
18+
19+
final class SqlProcessor implements Processor
20+
{
21+
private $client;
22+
23+
public function __construct(Client $client)
24+
{
25+
$this->client = $client;
26+
}
27+
28+
public function getName() : string
29+
{
30+
return 'sql';
31+
}
32+
33+
public function process(string $value) : void
34+
{
35+
$this->client->executeUpdate($value);
36+
}
37+
}

0 commit comments

Comments
 (0)