-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'cake3_next' into cake3
- Loading branch information
Showing
18 changed files
with
607 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson | ||
* @link https://webrider.de/ | ||
* @since 1.0.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace CakephpFixtureFactories\Error; | ||
|
||
use Cake\Core\Exception\Exception; | ||
|
||
class FixtureScenarioException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson | ||
* @link https://webrider.de/ | ||
* @since 2.3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
namespace CakephpFixtureFactories\Factory; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Utility\Inflector; | ||
use CakephpFixtureFactories\Error\FactoryNotFoundException; | ||
|
||
trait FactoryAwareTrait | ||
{ | ||
/** | ||
* Returns a factory instance from factory or model name | ||
* | ||
* Additionnal arguments are passed *as is* to `BaseFactory::make` | ||
* | ||
* @param string $name Factory or model name | ||
* @param string|array[] ...$arguments Additional arguments for `BaseFactory::make` | ||
* @return \CakephpFixtureFactories\Factory\BaseFactory | ||
* @throws \CakephpFixtureFactories\Error\FactoryNotFoundException if the factory could not be found | ||
* @see \CakephpFixtureFactories\Factory\BaseFactory::make | ||
*/ | ||
public function getFactory(string $name, ...$arguments): BaseFactory | ||
{ | ||
$factoryClassName = $this->getFactoryClassName($name); | ||
|
||
if (class_exists($factoryClassName)) { | ||
return $factoryClassName::make(...$arguments); | ||
} | ||
|
||
throw new FactoryNotFoundException("Unable to locate factory class $factoryClassName"); | ||
} | ||
|
||
/** | ||
* Converts factory or model name to a fully qualified factory class name | ||
* | ||
* @param string $name Factory or model name | ||
* @return string Fully qualified class name | ||
*/ | ||
public function getFactoryClassName(string $name): string | ||
{ | ||
// phpcs:disable | ||
@[$modelName, $plugin] = array_reverse(explode('.', $name)); | ||
// phpcs:enable | ||
|
||
return $this->getFactoryNamespace($plugin) . '\\' . $this->getFactoryNameFromModelName($modelName); | ||
} | ||
|
||
/** | ||
* Returns the factory file name | ||
* | ||
* @param string $name [description] | ||
* @return string [description] | ||
*/ | ||
public function getFactoryFileName(string $name): string | ||
{ | ||
return $this->getFactoryNameFromModelName($name) . '.php'; | ||
} | ||
|
||
/** | ||
* Return the name of the factory from a model name | ||
* | ||
* @param string $modelName Name of the model | ||
* @return string | ||
*/ | ||
public static function getFactoryNameFromModelName(string $modelName): string | ||
{ | ||
return Inflector::singularize(ucfirst($modelName)) . 'Factory'; | ||
} | ||
|
||
/** | ||
* Namespace where the factory belongs | ||
* | ||
* @param string|null $plugin name of the plugin, or null if no plugin | ||
* @return string | ||
*/ | ||
public function getFactoryNamespace(?string $plugin = null): string | ||
{ | ||
if (Configure::check('TestFixtureNamespace')) { | ||
return Configure::read('TestFixtureNamespace'); | ||
} else { | ||
return ( | ||
$plugin ? | ||
str_replace('/', '\\', $plugin) : | ||
Configure::read('App.namespace', 'App') | ||
) . '\Test\Factory'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson | ||
* @link https://webrider.de/ | ||
* @since 2.3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
namespace CakephpFixtureFactories\Scenario; | ||
|
||
interface FixtureScenarioInterface | ||
{ | ||
/** | ||
* Create your bunch of test fixtures in this method. | ||
* | ||
* @return void | ||
*/ | ||
public function load(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson | ||
* @link https://webrider.de/ | ||
* @since 2.3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
namespace CakephpFixtureFactories\Scenario; | ||
|
||
use CakephpFixtureFactories\Error\FixtureScenarioException; | ||
use CakephpFixtureFactories\Factory\FactoryAwareTrait; | ||
|
||
trait ScenarioAwareTrait | ||
{ | ||
use FactoryAwareTrait; | ||
|
||
/** | ||
* Load a given fixture scenario | ||
* | ||
* @param string $scenario Name of the scenario or fully qualified class. | ||
* @return void | ||
*/ | ||
public function loadFixtureScenario(string $scenario): void | ||
{ | ||
if (!class_exists($scenario)) { | ||
// phpcs:disable | ||
@[$scenarioName, $plugin] = array_reverse(explode('.', $scenario)); | ||
// phpcs:enable | ||
$scenarioNamespace = trim($this->getFactoryNamespace($plugin), 'Factory') . 'Scenario'; | ||
$scenario = $scenarioNamespace . '\\' . $scenarioName . 'Scenario'; | ||
} | ||
|
||
if (!is_subclass_of($scenario, FixtureScenarioInterface::class)) { | ||
$msg = "The class {$scenario} must implement " . FixtureScenarioInterface::class; | ||
throw new FixtureScenarioException($msg); | ||
} | ||
|
||
/** @var \CakephpFixtureFactories\Scenario\FixtureScenarioInterface $scenario */ | ||
$scenario = new $scenario(); | ||
$scenario->load(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson | ||
* @link https://webrider.de/ | ||
* @since 1.0.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace CakephpFixtureFactories\Test\Factory; | ||
|
||
use CakephpFixtureFactories\Factory\BaseFactory; | ||
use Faker\Generator; | ||
|
||
class ArticlesAuthorFactory extends BaseFactory | ||
{ | ||
/** | ||
* Defines the Table Registry used to generate entities with | ||
* | ||
* @return string | ||
*/ | ||
protected function getRootTableRegistryName(): string | ||
{ | ||
return 'ArticlesAuthors'; | ||
} | ||
|
||
/** | ||
* Defines the default values of you factory. Useful for | ||
* not nullable fields. | ||
* Use the patchData method to set the field values. | ||
* You may use methods of the factory here | ||
* | ||
* @return void | ||
*/ | ||
protected function setDefaultTemplate(): void | ||
{ | ||
$this->setDefaultData(function (Generator $faker) { | ||
return []; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.