Skip to content

Commit

Permalink
Get more tests passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 2, 2024
1 parent a5b433d commit f84bf98
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
21 changes: 12 additions & 9 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(array $configArray, ?string $configFilePath = null)
*
* @param string $configFilePath Path to the Yaml File
* @throws \RuntimeException
* @return \Phinx\Config\ConfigInterface
* @return \Migrations\Config\ConfigInterface
* @deprecated 4.2 To be removed in 5.x
*/
public static function fromYaml(string $configFilePath): ConfigInterface
Expand Down Expand Up @@ -95,7 +95,7 @@ public static function fromYaml(string $configFilePath): ConfigInterface
*
* @param string $configFilePath Path to the JSON File
* @throws \RuntimeException
* @return \Phinx\Config\ConfigInterface
* @return \Migrations\Config\ConfigInterface
* @deprecated 4.2 To be removed in 5.x
*/
public static function fromJson(string $configFilePath): ConfigInterface
Expand All @@ -106,7 +106,7 @@ public static function fromJson(string $configFilePath): ConfigInterface
// @codeCoverageIgnoreEnd
}

$configArray = json_decode(file_get_contents($configFilePath), true);
$configArray = json_decode((string)file_get_contents($configFilePath), true);
if (!is_array($configArray)) {
throw new RuntimeException(sprintf(
'File \'%s\' must be valid JSON',
Expand All @@ -122,7 +122,7 @@ public static function fromJson(string $configFilePath): ConfigInterface
*
* @param string $configFilePath Path to the PHP File
* @throws \RuntimeException
* @return \Phinx\Config\ConfigInterface
* @return \Migrations\Config\ConfigInterface
* @deprecated 4.2 To be removed in 5.x
*/
public static function fromPhp(string $configFilePath): ConfigInterface
Expand Down Expand Up @@ -231,13 +231,14 @@ public function getDefaultEnvironment(): string

throw new RuntimeException(sprintf(
'The environment configuration for \'%s\' is missing',
$this->values['environments']['default_environment']
(string)$this->values['environments']['default_environment']
));
}

// else default to the first available one
if (is_array($this->getEnvironments()) && count($this->getEnvironments()) > 0) {
$names = array_keys($this->getEnvironments());
$environments = $this->getEnvironments();

Check failure on line 239 in src/Config/Config.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

DeprecatedMethod

src/Config/Config.php:239:32: DeprecatedMethod: The method Migrations\Config\Config::getEnvironments has been marked as deprecated (see https://psalm.dev/001)
if (is_array($environments) && count($environments) > 0) {
$names = array_keys($environments);

return $names[0];
}
Expand Down Expand Up @@ -433,6 +434,8 @@ protected function replaceTokens(array $arr): array
// Depending on configuration of server / OS and variables_order directive,
// environment variables either end up in $_SERVER (most likely) or $_ENV,
// so we search through both

/** @var array<string, string> $tokens */
$tokens = [];
foreach (array_merge($_ENV, $_SERVER) as $varname => $varvalue) {
if (strpos($varname, 'PHINX_') === 0) {
Expand All @@ -442,7 +445,7 @@ protected function replaceTokens(array $arr): array

// Phinx defined tokens (override env tokens)
$tokens['%%PHINX_CONFIG_PATH%%'] = $this->getConfigFilePath();
$tokens['%%PHINX_CONFIG_DIR%%'] = $this->getConfigFilePath() !== null ? dirname($this->getConfigFilePath()) : '';
$tokens['%%PHINX_CONFIG_DIR%%'] = $this->getConfigFilePath() !== null ? dirname((string)$this->getConfigFilePath()) : '';

// Recurse the array and replace tokens
return $this->recurseArrayForTokens($arr, $tokens);

Check failure on line 451 in src/Config/Config.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

InvalidArgument

src/Config/Config.php:451:51: InvalidArgument: Argument 2 of Migrations\Config\Config::recurseArrayForTokens expects array<array-key, null>|string, but array{'%%PHINX_CONFIG_DIR%%': string, '%%PHINX_CONFIG_PATH%%': null|string, ...<string, string>} provided (see https://psalm.dev/004)
Expand All @@ -452,7 +455,7 @@ protected function replaceTokens(array $arr): array
* Recurse an array for the specified tokens and replace them.
*
* @param array $arr Array to recurse
* @param string[] $tokens Array of tokens to search for
* @param string|null[] $tokens Array of tokens to search for
* @return array
*/
protected function recurseArrayForTokens(array $arr, array $tokens): array

Check failure on line 461 in src/Config/Config.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PHPDoc tag @param for parameter $tokens with type array<null>|string is not subtype of native type array.
Expand Down
1 change: 1 addition & 0 deletions src/ConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function getConfig(bool $forceRefresh = false): ConfigInterface
mkdir($seedsPath, 0777, true);
}

// TODO this should use Migrations\Config
$phinxTable = $this->getPhinxTable($plugin);

$connection = $this->getConnectionName($this->input());
Expand Down
33 changes: 21 additions & 12 deletions src/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use DateTime;
use Exception;
use InvalidArgumentException;
use Phinx\Config\ConfigInterface;
use Phinx\Config\NamespaceAwareInterface;
use Migrations\Config\Config;
use Migrations\Config\ConfigInterface;
use Phinx\Migration\AbstractMigration;
use Phinx\Migration\MigrationInterface;
use Phinx\Seed\AbstractSeed;
Expand All @@ -30,7 +30,7 @@ class Manager
public const BREAKPOINT_UNSET = 3;

/**
* @var \Phinx\Config\ConfigInterface
* @var \Migrations\Config\ConfigInterface
*/
protected ConfigInterface $config;

Expand Down Expand Up @@ -70,7 +70,7 @@ class Manager
private int $verbosityLevel = OutputInterface::OUTPUT_NORMAL | OutputInterface::VERBOSITY_NORMAL;

/**
* @param \Phinx\Config\ConfigInterface $config Configuration Object
* @param \Migrations\Config\ConfigInterface $config Configuration Object
* @param \Symfony\Component\Console\Input\InputInterface $input Console Input
* @param \Symfony\Component\Console\Output\OutputInterface $output Console Output
*/
Expand Down Expand Up @@ -740,18 +740,21 @@ public function getEnvironment(string $name): Environment
return $this->environments[$name];
}

$config = $this->getConfig();
// check the environment exists
if (!$this->getConfig()->hasEnvironment($name)) {
if ($config instanceof Config && !$config->hasEnvironment($name)) {
throw new InvalidArgumentException(sprintf(
'The environment "%s" does not exist',
$name
));
}

// create an environment instance and cache it
$envOptions = $this->getConfig()->getEnvironment($name);
$envOptions['version_order'] = $this->getConfig()->getVersionOrder();
$envOptions['data_domain'] = $this->getConfig()->getDataDomain();
$envOptions = $config->getEnvironment($name);
$envOptions['version_order'] = $config->getVersionOrder();
if ($config instanceof Config) {
$envOptions['data_domain'] = $config->getDataDomain();
}

$environment = new Environment($name, $envOptions);
$this->environments[$name] = $environment;
Expand Down Expand Up @@ -876,7 +879,10 @@ function ($phpFile) {
}

$config = $this->getConfig();
$namespace = $config instanceof NamespaceAwareInterface ? $config->getMigrationNamespaceByPath(dirname($filePath)) : null;
$namespace = null;
if ($config instanceof Config) {
$namespace = $config->getMigrationNamespaceByPath(dirname($filePath));
}

// convert the filename to a class name
$class = ($namespace === null ? '' : $namespace . '\\') . Util::mapFileNameToClassName(basename($filePath));
Expand Down Expand Up @@ -1025,7 +1031,10 @@ public function getSeeds(string $environment): array
foreach ($phpFiles as $filePath) {
if (Util::isValidSeedFileName(basename($filePath))) {
$config = $this->getConfig();
$namespace = $config instanceof NamespaceAwareInterface ? $config->getSeedNamespaceByPath(dirname($filePath)) : null;
$namespace = null;
if ($config instanceof Config) {
$namespace = $config->getSeedNamespaceByPath(dirname($filePath));
}

// convert the filename to a class name
$class = ($namespace === null ? '' : $namespace . '\\') . pathinfo($filePath, PATHINFO_FILENAME);
Expand Down Expand Up @@ -1101,7 +1110,7 @@ protected function getSeedFiles(): array
/**
* Sets the config.
*
* @param \Phinx\Config\ConfigInterface $config Configuration Object
* @param \Migrations\Config\ConfigInterface $config Configuration Object
* @return $this
*/
public function setConfig(ConfigInterface $config)
Expand All @@ -1114,7 +1123,7 @@ public function setConfig(ConfigInterface $config)
/**
* Gets the config.
*
* @return \Phinx\Config\ConfigInterface
* @return \Migrations\Config\ConfigInterface
*/
public function getConfig(): ConfigInterface
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Migration/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use Cake\Datasource\ConnectionManager;
use DateTime;
use InvalidArgumentException;
use Migrations\Config\Config;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Migration\Environment;
use Migrations\Migration\Manager;
use Phinx\Config\Config;
use Phinx\Console\Command\AbstractCommand;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand Down

0 comments on commit f84bf98

Please sign in to comment.