Skip to content

Commit

Permalink
Merge pull request #9 from hrodic/1.x-dev
Browse files Browse the repository at this point in the history
1.x dev
  • Loading branch information
hrodic authored Apr 29, 2020
2 parents 7643b64 + a7ae457 commit c650ccf
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 102 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ install:
script:
- make test-unit
- make test-integration
- make merge-coverage

after_script:
- cp build/coverage/merged.xml clover.xml
- cp build/coverage/unit.xml clover.xml
- ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ test-unit:
vendor/bin/phpunit --testdox --verbose --color
test-integration: up
sleep 20
@-vendor/bin/phpunit --color --testdox --verbose -c phpunit-integration.xml.dist
@-vendor/bin/phpunit --no-coverage --color --testdox --verbose -c phpunit-integration.xml.dist
make down
merge-coverage:
vendor/bin/phpcov merge --clover build/coverage/merged.xml build/coverage
debug-test-integration:
test-group-integration:
php vendor/bin/phpunit --no-coverage --color --testdox --group debug -c phpunit-integration.xml.dist
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"ext-xml": "*",
"friendsofphp/php-cs-fixer": "^2.16",
"php-amqplib/php-amqplib": "^2.11",
"phpunit/phpcov": "^6.0",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
},
Expand Down
9 changes: 0 additions & 9 deletions phpunit-integration.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,4 @@
</arguments>
</extension>
</extensions>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-php" target="build/coverage/integration.cov"/>
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
</logging>
</phpunit>
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
</whitelist>
</filter>
<logging>
<log type="coverage-php" target="build/coverage/unit.cov"/>
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
<log type="coverage-php" target="build/coverage/unit.cov"/>
<log type="coverage-clover" target="build/coverage/unit.xml"/>
<log type="coverage-html" target="build/coverage/unit"/>
</logging>
</phpunit>
20 changes: 18 additions & 2 deletions src/PHPUnit/Runner/Extension/AMQPFixtureConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace IntegrationTesting\PHPUnit\Runner\Extension;

use IntegrationTesting\Exception\TestingException;
use IntegrationTesting\PHPUnit\Runner\Extension\AMQP\PublishMessageConfig;
use Iterator;

Expand All @@ -10,7 +11,7 @@ class AMQPFixtureConfig
private const BEFORE_FIRST_TEST_KEY = 'beforeFirstTest';
private const BEFORE_TEST_KEY = 'beforeTest';
private const AFTER_TEST_KEY = 'afterTest';
private const AFTER_LAST_TEST_KEY = 'afterLastKey';
private const AFTER_LAST_TEST_KEY = 'afterLastTest';
private const PURGE_QUEUES_KEY = 'purgeQueues';
private const PUBLISH_MESSAGES_KEY = 'publishMessages';
private static $defaultParams = [
Expand All @@ -29,11 +30,26 @@ class AMQPFixtureConfig
self::PURGE_QUEUES_KEY => []
]
];
private $params;
private $params = [];

public function __construct(array $params)
{
if ($invalidConfigParams = array_diff_key($params, self::$defaultParams)) {
throw new TestingException(
'The following elements are not valid AMQP configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->params = array_merge(self::$defaultParams, $params);
foreach ($this->params as $key => $value) {
if (isset($params[$key])) {
if ($invalidConfigParams = array_diff_key($params[$key], self::$defaultParams[$key])) {
throw new TestingException(
'The following elements are not valid AMQP configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->params[$key] = array_merge(self::$defaultParams[$key], $params[$key]);
}
}
}

/**
Expand Down
30 changes: 16 additions & 14 deletions src/PHPUnit/Runner/Extension/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ public function __construct(array $params)
if (empty($params)) {
throw new TestingException('Configuration parameters are empty');
}
if (isset($params[self::PDO_KEY])) {
if ($invalidConfigParams = array_diff_key($params[self::PDO_KEY], self::$defaultPDOParams)) {
throw new TestingException(
'The following elements are not valid PDO configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->PDOParams = array_merge(self::$defaultPDOParams, $params[self::PDO_KEY]);
if (!isset($params[self::PDO_KEY])) {
$params[self::PDO_KEY] = [];
}
if (isset($params[self::AMQP_KEY])) {
if ($invalidConfigParams = array_diff_key($params[self::AMQP_KEY], self::$defaultAMQPParams)) {
throw new TestingException(
'The following elements are not valid AMQP configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->AMQPParams = array_merge(self::$defaultAMQPParams, $params[self::AMQP_KEY]);
if ($invalidConfigParams = array_diff_key($params[self::PDO_KEY], self::$defaultPDOParams)) {
throw new TestingException(
'The following elements are not valid PDO configuration params: ' . json_encode($invalidConfigParams)
);
}
if (!isset($params[self::AMQP_KEY])) {
$params[self::AMQP_KEY] = [];
}
if ($invalidConfigParams = array_diff_key($params[self::AMQP_KEY], self::$defaultAMQPParams)) {
throw new TestingException(
'The following elements are not valid AMQP configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->PDOParams = array_merge(self::$defaultPDOParams, $params[self::PDO_KEY]);
$this->AMQPParams = array_merge(self::$defaultAMQPParams, $params[self::AMQP_KEY]);
}

public function getPDODSN(): string
Expand Down
17 changes: 7 additions & 10 deletions src/PHPUnit/Runner/Extension/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ final class Handler implements BeforeFirstTestHook, BeforeTestHook, AfterTestHoo
*/
private $fileSystem;

/**
* Handler constructor.
* @param string $configurationFileName
* @param null $fileSystem
* @throws TestingException
*/
public function __construct(string $configurationFileName = '', $fileSystem = null)
{
if (null === $fileSystem) {
Expand Down Expand Up @@ -83,16 +89,7 @@ private function getConfigurationFromFileName(string $fileName): Configuration
public function initPDOFixtureLoader(Configuration $configuration): void
{
if ($configuration->getPDODSN()) {
$pdoFixtureConfig = new PDOFixtureConfig([
PDOFixtureConfig::BEFORE_FIRST_TEST_PDO_FIXTURES_PATH =>
$configuration->getPDOFixtures()['beforeFirstTest']['path'],
PDOFixtureConfig::BEFORE_TEST_PDO_FIXTURES_PATH =>
$configuration->getPDOFixtures()['beforeTest']['path'],
PDOFixtureConfig::AFTER_TEST_PDO_FIXTURES_PATH =>
$configuration->getPDOFixtures()['afterTest']['path'],
PDOFixtureConfig::AFTER_LAST_TEST_PDO_FIXTURES_PATH =>
$configuration->getPDOFixtures()['afterLastTest']['path']
]);
$pdoFixtureConfig = new PDOFixtureConfig($configuration->getPDOFixtures());
$pdoConnection = new PDOConnection(
$configuration->getPDODSN(),
$configuration->getPDOUser(),
Expand Down
75 changes: 52 additions & 23 deletions src/PHPUnit/Runner/Extension/PDOFixtureConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,77 @@

class PDOFixtureConfig
{
const BEFORE_FIRST_TEST_PDO_FIXTURES_PATH = 'BEFORE_FIRST_TEST_PDO_FIXTURES_PATH';
const BEFORE_TEST_PDO_FIXTURES_PATH = 'BEFORE_TEST_PDO_FIXTURES_PATH';
const AFTER_TEST_PDO_FIXTURES_PATH = 'AFTER_TEST_PDO_FIXTURES_PATH';
const AFTER_LAST_TEST_PDO_FIXTURES_PATH = 'AFTER_LAST_TEST_PDO_FIXTURES_PATH';
private const BEFORE_FIRST_TEST_KEY = 'beforeFirstTest';
private const BEFORE_TEST_KEY = 'beforeTest';
private const AFTER_TEST_KEY = 'afterTest';
private const AFTER_LAST_TEST_KEY = 'afterLastTest';
private const DEFAULT_EXTENSION = 'sql';

public static $defaultParams = [
self::BEFORE_FIRST_TEST_PDO_FIXTURES_PATH => '',
self::BEFORE_TEST_PDO_FIXTURES_PATH => '',
self::AFTER_TEST_PDO_FIXTURES_PATH => '',
self::AFTER_LAST_TEST_PDO_FIXTURES_PATH => '',
self::BEFORE_FIRST_TEST_KEY => [
'path' => '',
'extension' => self::DEFAULT_EXTENSION
],
self::BEFORE_TEST_KEY => [
'path' => '',
'extension' => self::DEFAULT_EXTENSION
],
self::AFTER_TEST_KEY => [
'path' => '',
'extension' => self::DEFAULT_EXTENSION
],
self::AFTER_LAST_TEST_KEY => [
'path' => '',
'extension' => self::DEFAULT_EXTENSION
]
];
private $params = [];

/**
* PDODatabaseExtensionConfig constructor.
*
* @param array $params
* PDOFixtureConfig constructor.
* @param array $params
* @throws TestingException
*/
public function __construct(array $params)
{
if (empty($params)) {
throw new TestingException('Configuration parameters are empty');
}
if ($invalidConfigParams = array_diff_key($params, self::$defaultParams)) {
throw new TestingException(
'The following elements are not valid configuration params: ' . json_encode($invalidConfigParams)
'The following elements are not valid PDO configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->params = array_merge(self::$defaultParams, $params);
foreach ($this->params as $key => $value) {
if (isset($params[$key])) {
if ($invalidConfigParams = array_diff_key($params[$key], self::$defaultParams[$key])) {
throw new TestingException(
'The following elements are not valid PDO configuration params: ' . json_encode($invalidConfigParams)
);
}
$this->params[$key] = array_merge(self::$defaultParams[$key], $params[$key]);
}
}
}

/**
* @param string $key
* @return string
* @throws TestingException
* @return array['path' => 'extension']
*/
public function getParam(string $key): string
public function getBeforeFirstTest(): array
{
if (isset($this->params[$key])) {
return $this->params[$key];
}
throw new TestingException("Parameter [$key] does not exists");
return $this->params[self::BEFORE_FIRST_TEST_KEY];
}

public function getBeforeTest(): array
{
return $this->params[self::BEFORE_TEST_KEY];
}

public function getAfterTest(): array
{
return $this->params[self::AFTER_TEST_KEY];
}

public function getAfterLastTest(): array
{
return $this->params[self::AFTER_LAST_TEST_KEY];
}
}
56 changes: 30 additions & 26 deletions src/PHPUnit/Runner/Extension/PDOFixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

final class PDOFixtureLoader implements FixtureLoader
{
const EXTENSION_SQL = 'sql';

private $fileSystem;
private $config;
private $connection;
Expand All @@ -26,8 +24,8 @@ public function __construct(FileSystem $fileSystem, PDOFixtureConfig $config, PD
*/
public function executeBeforeFirstTest(): void
{
$path = $this->config->getParam(PDOFixtureConfig::BEFORE_FIRST_TEST_PDO_FIXTURES_PATH);
$this->runFixturesUnderPath($path);
$params = $this->config->getBeforeFirstTest();
$this->runFixturesUnderPathWithExtension($params['path'], $params['extension']);
}

/**
Expand All @@ -36,18 +34,11 @@ public function executeBeforeFirstTest(): void
*/
public function executeBeforeTest(string $test): void
{
$stage = PDOFixtureConfig::BEFORE_TEST_PDO_FIXTURES_PATH;
$path = $this->config->getParam($stage);
$this->runFixturesUnderPath($path);
$params = $this->config->getBeforeTest();
$this->runFixturesUnderPathWithExtension($params['path'], $params['extension']);
$className = '\\' . substr($test, 0, strpos($test, ':'));
$methodName = 'getBeforeTestFixtureName';
if (method_exists($className, $methodName)) {
$fixtureNameFunc = "$className::$methodName";
$path = $this->config->getParam($stage)
. DIRECTORY_SEPARATOR
. $fixtureNameFunc();
$this->runFixturesUnderPath($path);
}
$this->runSpecificFixturesFromStatic($className, $methodName, $params);
}

/**
Expand All @@ -57,38 +48,34 @@ public function executeBeforeTest(string $test): void
*/
public function executeAfterTest(string $test, float $time): void
{
$stage = PDOFixtureConfig::AFTER_TEST_PDO_FIXTURES_PATH;
$path = $this->config->getParam($stage);
$this->runFixturesUnderPath($path);
$params = $this->config->getAfterTest();
$this->runFixturesUnderPathWithExtension($params['path'], $params['extension']);
$className = '\\' . substr($test, 0, strpos($test, ':'));
$methodName = 'getAfterTestFixtureName';
if (method_exists($className, $methodName)) {
$fixtureNameFunc = "$className::$methodName";
$path = $this->config->getParam($stage) . DIRECTORY_SEPARATOR . $fixtureNameFunc();
$this->runFixturesUnderPath($path);
}
$this->runSpecificFixturesFromStatic($className, $methodName, $params);
}

/**
* @throws TestingException
*/
public function executeAfterLastTest(): void
{
$path = $this->config->getParam(PDOFixtureConfig::AFTER_LAST_TEST_PDO_FIXTURES_PATH);
$this->runFixturesUnderPath($path);
$params = $this->config->getAfterLastTest();
$this->runFixturesUnderPathWithExtension($params['path'], $params['extension']);
}

/**
* @param string $path
* @param string $extension
* @throws TestingException
*/
public function runFixturesUnderPath(string $path): void
public function runFixturesUnderPathWithExtension(string $path, string $extension): void
{
try {
$this->connection->PDO()->beginTransaction();
$iterator = $this->fileSystem->getFileListIteratorFromPathByExtension(
$path,
self::EXTENSION_SQL
$extension
);
$this->fileSystem->runCallbackOnEachFileIteratorContents(
$iterator,
Expand All @@ -102,4 +89,21 @@ function (string $contents) {
throw $exception;
}
}

/**
* @param string $className
* @param string $methodName
* @param array $params
* @throws TestingException
*/
public function runSpecificFixturesFromStatic(string $className, string $methodName, array $params): void
{
if (method_exists($className, $methodName)) {
$fixtureNameFunc = "$className::$methodName";
$this->runFixturesUnderPathWithExtension(
$params['path'] . DIRECTORY_SEPARATOR . $fixtureNameFunc(),
$params['extension']
);
}
}
}
Loading

0 comments on commit c650ccf

Please sign in to comment.