-
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 pull request #68 from vierge-noire/next
Postgres update
- Loading branch information
Showing
15 changed files
with
306 additions
and
186 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
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,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 FactoryNotFoundException 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
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,88 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
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 Additionnal arguments for `BaseFactory::make` | ||
* @return \CakephpFixtureFactories\Factory\BaseFactory | ||
* @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::read('TestFixtureNamespace')) { | ||
return Configure::read('TestFixtureNamespace'); | ||
} else { | ||
return ( | ||
$plugin ? | ||
str_replace('/', '\\', $plugin) : | ||
Configure::read('App.namespace', 'App') | ||
) . '\Test\Factory'; | ||
} | ||
} | ||
} |
Oops, something went wrong.