Skip to content

Commit

Permalink
Merge pull request #210 from LordSimal/cake5
Browse files Browse the repository at this point in the history
cakephp 5 update
  • Loading branch information
pabloelcolombiano authored Jul 4, 2023
2 parents bf24dee + 8cf3804 commit 5f7473f
Show file tree
Hide file tree
Showing 64 changed files with 459 additions and 609 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4']
php-version: ['8.1']

name: PHP ${{ matrix.php-version }}

Expand All @@ -41,7 +41,7 @@ jobs:
run: composer update --prefer-dist --no-progress --no-suggest --prefer-stable

- name: Bake factories to perform analysis in the following steps
run : vendor/bin/phpunit vendor/bin/phpunit tests/TestCase/Command/BakeFixtureFactoryCommandTest.php
run : vendor/bin/phpunit tests/TestCase/Command/BakeFixtureFactoryCommandTest.php

- name: Run phpstan
run: composer phpstan
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests_composer2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.2']
php-version: ['8.1', '8.2']
db-type: [sqlite, mysql, pgsql]
composer-type: [lowest, stable, dev]

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tmp/
.idea
composer.lock
**/test_fixture_factories
.phpunit.result.cache
.phpunit.cache
tests/.env

# These factories are baked by TestFixtureFactoryTaskTest
Expand Down
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
}
],
"license": "MIT",
"minimum-stability": "dev",
"require": {
"php": ">=7.2",
"cakephp/orm": "^4.2",
"php": ">=8.1",
"cakephp/orm": "5.x-dev",
"fakerphp/faker": "^1.15",
"vierge-noire/cakephp-test-suite-light": "^2.1"
"vierge-noire/cakephp-test-suite-light": "dev-next"
},
"require-dev": {
"cakephp/bake": "^2.0",
"cakephp/cakephp-codesniffer": "^4.0",
"cakephp/migrations": "^3.7",
"cakephp/bake": "3.x-dev as 3.0.0",
"cakephp/cakephp-codesniffer": "^5.1",
"cakephp/migrations": "4.x-dev as 4.0.0",
"josegonzalez/dotenv": "dev-master",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^8.5.31",
"vierge-noire/cakephp-test-migrator": "^2.1",
"vimeo/psalm": "^4.0"
"phpunit/phpunit": "^10.1",
"vimeo/psalm": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -60,5 +58,7 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
36 changes: 17 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="./tests/bootstrap.php"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="./tests/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
cacheDirectory=".phpunit.cache">

<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>

<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Default">
<directory>./tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="CakephpTestSuiteLight\FixtureInjector">
<arguments>
<object class="CakephpTestSuiteLight\FixtureManager" />
</arguments>
</listener>
</listeners>

<!-- Ignore vendor tests in code coverage reports -->
<filter>
<whitelist>
<source>
<include>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
6 changes: 4 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0"?>
<psalm
phpVersion="7.4"
errorLevel="4"
findUnusedPsalmSuppress="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
allowStringToStandInForClass="true"
usePhpDocMethodsWithoutMagicCall="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -11,7 +14,6 @@
>
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
</projectFiles>
<issueHandlers>
<InvalidScalarArgument errorLevel="suppress" />
Expand Down
88 changes: 61 additions & 27 deletions src/Command/BakeFixtureFactoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Filesystem\Folder;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use CakephpFixtureFactories\Factory\FactoryAwareTrait;
use Exception;
use ReflectionClass;
use ReflectionException;

class BakeFixtureFactoryCommand extends BakeCommand
{
Expand All @@ -33,17 +35,17 @@ class BakeFixtureFactoryCommand extends BakeCommand
/**
* @var string path to the Table dir
*/
public $pathToTableDir = 'Model' . DS . 'Table' . DS;
public string $pathToTableDir = 'Model' . DS . 'Table' . DS;

/**
* @var string
*/
private $modelName;
private string $modelName;

/**
* @var \Cake\ORM\Table
*/
private $table;
private Table $table;

/**
* @return string Name of the command
Expand Down Expand Up @@ -90,7 +92,7 @@ public function setTable(string $tableName, ConsoleIo $io)
$this->table = TableRegistry::getTableLocator()->get($tableName);
try {
$this->table->getSchema();
} catch (\Exception $e) {
} catch (Exception $e) {
$io->warning("The table $tableName could not be found... in " . $this->getModelPath());
$io->abort($e->getMessage());
}
Expand Down Expand Up @@ -119,9 +121,9 @@ public function getPath(Arguments $args): string
/**
* Locate tables
*
* @return string|string[]
* @return array<string>|string
*/
public function getModelPath()
public function getModelPath(): string|array
{
if (!empty($this->plugin)) {
$path = $this->_pluginPath($this->plugin) . APP_DIR . DS . $this->pathToTableDir;
Expand All @@ -140,21 +142,23 @@ public function getModelPath()
*/
public function getTableList(ConsoleIo $io): array
{
$dir = new Folder($this->getModelPath());
$tables = $dir->find('.*Table.php', true);
$tables = glob($this->getModelPath() . '*Table.php') ?: [];

$tables = array_map(function ($a) {
return preg_replace('/Table.php$/', '', $a);
}, $tables);

foreach ($tables as $i => $table) {
$return = [];
foreach ($tables as $table) {
$table = str_replace($this->getModelPath(), '', $table);
if (!$this->thisTableShouldBeBaked($table, $io)) {
unset($tables[$i]);
$io->warning("{$table} ignored");
} else {
$return[] = $table;
}
}

return $tables;
return $return;
}

/**
Expand All @@ -166,12 +170,12 @@ public function getTableList(ConsoleIo $io): array
*/
public function thisTableShouldBeBaked(string $table, ConsoleIo $io): bool
{
$tableClassName = $this->plugin ? $this->plugin : Configure::read('App.namespace');
$tableClassName = $this->plugin ?: Configure::read('App.namespace');
$tableClassName .= "\Model\Table\\{$table}Table";

try {
$class = new ReflectionClass($tableClassName);
} catch (\ReflectionException $e) {
} catch (ReflectionException $e) {
$io->error($e->getMessage());

return false;
Expand All @@ -189,7 +193,7 @@ public function thisTableShouldBeBaked(string $table, ConsoleIo $io): bool
* @param \Cake\Console\ConsoleIo $io Console
* @return string
*/
private function bakeAllModels(Arguments $args, ConsoleIo $io)
private function bakeAllModels(Arguments $args, ConsoleIo $io): string
{
$tables = $this->getTableList($io);
if (empty($tables)) {
Expand Down Expand Up @@ -255,9 +259,9 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
* @param string $modelName Name of the model
* @param \Cake\Console\Arguments $args Arguments
* @param \Cake\Console\ConsoleIo $io Console
* @return bool|int
* @return int|bool
*/
public function bakeFixtureFactory(string $modelName, Arguments $args, ConsoleIo $io)
public function bakeFixtureFactory(string $modelName, Arguments $args, ConsoleIo $io): bool|int
{
$this->modelName = $modelName;

Expand Down Expand Up @@ -291,22 +295,42 @@ public function templateData(Arguments $arg): array
'factory' => Inflector::singularize($this->modelName) . 'Factory',
'namespace' => $this->getFactoryNamespace($this->plugin),
];
$useStatements = $methods = [];
if ($arg->getOption('methods')) {
$associations = $this->getAssociations();

$data['toOne'] = $associations['toOne'];
$methods = array_keys($associations['toOne']);
if ($associations['toOne']) {
$data['toOne'] = $associations['toOne'];
$useStatements[] = Hash::extract($associations['toOne'], '{s}.fqcn');
$methods = array_keys($associations['toOne']);
}

$data['oneToMany'] = $associations['oneToMany'];
$methods = array_merge(array_keys($associations['oneToMany']), $methods);
if ($associations['oneToMany']) {
$data['oneToMany'] = $associations['oneToMany'];
$useStatements[] = Hash::extract($associations['oneToMany'], '{s}.fqcn');
$methods = array_merge(array_keys($associations['oneToMany']), $methods);
}

$data['manyToMany'] = $associations['manyToMany'];
$methods = array_merge(array_keys($associations['manyToMany']), $methods);
if ($associations['manyToMany']) {
$data['manyToMany'] = $associations['manyToMany'];
$useStatements[] = Hash::extract($associations['manyToMany'], '{s}.fqcn');
$methods = array_merge(array_keys($associations['manyToMany']), $methods);
}

array_walk($methods, function (&$value) {
array_walk($methods, function (&$value): void {
$value = "with$value";
});
$data['methods'] = $methods;
$data['useStatements'] = array_unique(array_values(Hash::flatten($useStatements)));
}

if ($data['useStatements']) {
foreach ($data['useStatements'] as $index => $useStatement) {
$nameSpaceCheck = str_replace($data['namespace'] . '\\', '', $useStatement);
if (!str_contains($nameSpaceCheck, '\\')) {
unset($data['useStatements'][$index]);
}
}
}

return $data;
Expand All @@ -328,16 +352,26 @@ public function getAssociations(): array
foreach ($this->getTable()->associations() as $association) {
$modelName = $association->getClassName();
$factory = $this->getFactoryClassName($modelName);
$factoryClassName = $this->getFactorySimpleClassName($modelName);
switch ($association->type()) {
case 'oneToOne':
case 'manyToOne':
$associations['toOne'][$association->getName()] = $factory;
$associations['toOne'][$association->getName()] = [
'fqcn' => $factory,
'className' => $factoryClassName,
];
break;
case 'oneToMany':
$associations['oneToMany'][$association->getName()] = $factory;
$associations['oneToMany'][$association->getName()] = [
'fqcn' => $factory,
'className' => $factoryClassName,
];
break;
case 'manyToMany':
$associations['manyToMany'][$association->getName()] = $factory;
$associations['manyToMany'][$association->getName()] = [
'fqcn' => $factory,
'className' => $factoryClassName,
];
break;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Command/PersistCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function attachMethod(Arguments $args, BaseFactory $factory, ConsoleIo $i
* @param \CakephpFixtureFactories\Factory\BaseFactory $factory Factory
* @return \CakephpFixtureFactories\Factory\BaseFactory
*/
public function with(Arguments $args, BaseFactory $factory)
public function with(Arguments $args, BaseFactory $factory): BaseFactory
{
$with = $args->getOption('with');

Expand Down Expand Up @@ -233,7 +233,6 @@ public function dryRun(BaseFactory $factory, ConsoleIo $io): void
$factory = get_class($factory);

$io->success("{$times} {$factory} generated on dry run.");
$eol = PHP_EOL;
foreach ($entities as $i => $entity) {
$io->hr();
$io->info("[$i]");
Expand Down
Loading

0 comments on commit 5f7473f

Please sign in to comment.