Skip to content

Commit

Permalink
Merge pull request #61 from vierge-noire/cake3_next
Browse files Browse the repository at this point in the history
Merge for v1.2
  • Loading branch information
pabloelcolombiano authored Feb 9, 2021
2 parents ff6e181 + 9071c5e commit 376a650
Show file tree
Hide file tree
Showing 35 changed files with 2,334 additions and 990 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,5 @@ jobs:
- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-suggest --prefer-stable

- name: Composer install phpstan
run: composer require phpstan/phpstan "^0.12.48@dev"

- name: Run phpstan
run: composer run-phpstan
run: composer phpstan
72 changes: 0 additions & 72 deletions .github/workflows/test_composer1.yml

This file was deleted.

13 changes: 5 additions & 8 deletions .github/workflows/tests_composer2.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: PHPUnit Tests

on:
push:
Expand All @@ -17,12 +17,9 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.0', '7.4']
php-version: ['7.1', '7.4']
db-type: [sqlite, mysql, pgsql]
composer-type: [stable, dev]
exclude:
- php-version: '7.0'
db-type: sqlite
composer-type: [lowest, stable, dev]

name: PHP ${{ matrix.php-version }} & ${{ matrix.db-type }} & ${{ matrix.composer-type }}

Expand Down Expand Up @@ -55,7 +52,7 @@ jobs:
- name: Install dependencies
run: |
if [[ ${{ matrix.composer-type }} == 'lowest' ]]; then
composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest
composer self-update --1 && composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest
elif [[ ${{ matrix.composer-type }} == 'stable' ]]; then
composer update --prefer-dist --no-progress --no-suggest --prefer-stable
else
Expand All @@ -67,4 +64,4 @@ jobs:
if [ ${{ matrix.db-type }} == 'mysql' ]; then
sudo service mysql start && mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE IF NOT EXISTS test_fixture_factories;';
fi
composer run-tests-${{ matrix.db-type }}
composer ${{ matrix.db-type }}
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
"license": "MIT",
"minimum-stability": "dev",
"require": {
"php": ">=7.0.0",
"php": ">=7.1.0",
"cakephp/cakephp": "^3.7",
"fzaninotto/faker": "^1.9@dev",
"fakerphp/faker": "^1.13",
"vierge-noire/cakephp-test-suite-light": "^1.1"
},
"require-dev": {
"cakephp/bake": "^1.5",
"josegonzalez/dotenv": "dev-master",
"phpstan/phpstan": "0.12.x-dev",
"phpunit/phpunit": "^6.1",
"vierge-noire/cakephp-test-migrator": "^1.1"

},
"autoload": {
"psr-4": {
Expand All @@ -43,10 +43,10 @@
}
},
"scripts": {
"run-tests-mysql": "bash run_tests.sh Mysql",
"run-tests-pgsql": "bash run_tests.sh Postgres",
"run-tests-sqlite": "bash run_tests.sh Sqlite",
"run-phpstan": "vendor/bin/phpstan analyse"
"mysql": "bash run_tests.sh Mysql",
"pgsql": "bash run_tests.sh Postgres",
"sqlite": "bash run_tests.sh Sqlite",
"phpstan": "vendor/bin/phpstan analyse --memory-limit=-1"
},
"config": {
"sort-packages": true
Expand Down
43 changes: 41 additions & 2 deletions docs/factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,46 @@ $article = ArticleFactory::makeWithModelEvents()->persist();
Or for a plugin Foo, in `Foo\Test\Factory`.

You may change that by setting in your configuration the key `TestFixtureNamespace` to the desired namespace.

### Next

### Property uniqueness

It is not rare to have to create entities associated with an entity that should remain
constant and should not be recreated once it was already persisted. For example, if you create
5 cities within a country, you will not want to have 5 countries created. This might
collide with the constrains of your schema. The same goes of course with primary keys.

The fixture factories offer to define unique properties, under the protected property
$uniqueProperties. For example given a country factory.

```$xslt
namespace App\Test\Factory;
...
class CountryFactory extends BaseFactory
{
protected $uniqueProperties = [
'name',
];
...
}
```

Knowing the property `name` is unique, the country factory
will be cautious whenever the property `name` is set by the developer.

Executing `CityFactory::make(5)->with('Country', ['name' => 'Foo'])->persist()` will create
5 cities all associated to one unique country. If you perform that same operation again,
you will have 10 cities, all associated to one single country.

### Primary keys uniqueness

The uniqueness of the primary keys is handled exactely the same way as described above,
with the particularity that you do not have to define them as unique. The factory
cannot read the uniqueness of a property in the schema, but it knows which properties
are primary keys. Therefore, executing
`CityFactory::make(5)->with('Country', ['myPrimaryKey' => 1])->persist()` will behave the
same as if the primary key `myPrimaryKey` had been defined unique. In short, the factories
do the job for you.

### Next

Let us now see [how to use them](examples.md)...
20 changes: 20 additions & 0 deletions src/Error/UniquenessException.php
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 UniquenessException extends Exception
{
}
1 change: 1 addition & 0 deletions src/Event/ModelEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ModelEventsHandler
'Model.beforeFind',
'Model.buildValidator',
'Model.buildRules',
'Model.beforeFind',
'Model.beforeRules',
'Model.afterRules',
'Model.beforeSave',
Expand Down
Loading

0 comments on commit 376a650

Please sign in to comment.