Skip to content

Commit

Permalink
#157 Apply CS check and fix to test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloelcolombiano committed May 27, 2022
1 parent dbd4059 commit 6de4c7b
Show file tree
Hide file tree
Showing 31 changed files with 253 additions and 274 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PHPStan
name: Static analysis

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"cakephp/bake": "^1.5",
"cakephp/migrations": "^2.3",
"josegonzalez/dotenv": "dev-master",
"phpstan/phpstan": "0.12.x-dev",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^6.1",
"vierge-noire/cakephp-test-migrator": "^1.1"
},
Expand Down
8 changes: 6 additions & 2 deletions src/Event/ModelEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ final public function __construct(array $listeningModelEvents, array $listeningB
$this->listeningBehaviors = $listeningBehaviors;
}

public static function handle(Table $table, array $listeningModelEvents = [], array $listeningBehaviors = [])
/**
* @param \Cake\ORM\Table $table Table
* @return void
*/
public function handle(Table $table): void
{
(new static($listeningModelEvents, $listeningBehaviors))->ignoreModelEvents($table);
$this->ignoreModelEvents($table);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/AssociationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function removeAssociationForToOneFactory(string $associationName, BaseFa
* Get the factory for the association
*
* @param string $associationName Association name
* @param scalar[]|\CakephpFixtureFactories\Factory\BaseFactory|\Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[] $data Injected data
* @param array|int|string|callable|\CakephpFixtureFactories\Factory\BaseFactory|\CakephpFixtureFactories\Factory\BaseFactory[]|\Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[] $data Injected data
* @return \CakephpFixtureFactories\Factory\BaseFactory
*/
public function getAssociatedFactory(string $associationName, $data = []): BaseFactory
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ protected function setDefaultData(callable $fn): self
/**
* Add associated entities to the fixtures generated by the factory
* The associated name can be of several depth, dot separated
* The data can be an array, an integer, an entity interface, a callable or a factory
* The data can be an integer, a string, an entity interface, a factory, an array of all that or a callable
*
* @param string $associationName Association name
* @param array|int|callable|\CakephpFixtureFactories\Factory\BaseFactory|\Cake\Datasource\EntityInterface|string $data Injected data
* @param int|string|array|callable|self|\Cake\Datasource\EntityInterface $data Injected data
* @return $this
*/
public function with(string $associationName, $data = []): self
Expand Down
7 changes: 2 additions & 5 deletions src/ORM/FactoryTableLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ protected function _create(array $options): Table

$behaviors = array_merge($options[EventCollector::MODEL_BEHAVIORS] ?? [], $defaultBehaviors);

ModelEventsHandler::handle(
$table,
$options[EventCollector::MODEL_EVENTS] ?? [],
$behaviors
);
(new ModelEventsHandler($options[EventCollector::MODEL_EVENTS] ?? [], $behaviors))
->handle($table);

$table->getEventManager()->on('Model.beforeSave', function ($event, $entity, $options) use ($table) {
FactoryTableBeforeSave::handle($table, $entity);
Expand Down
10 changes: 5 additions & 5 deletions tests/Scenario/NAustralianAuthorsScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
*/
namespace CakephpFixtureFactories\Test\Scenario;


use CakephpFixtureFactories\Scenario\FixtureScenarioInterface;
use CakephpFixtureFactories\Test\Factory\AuthorFactory;
use TestApp\Model\Entity\Author;

class NAustralianAuthorsScenario implements FixtureScenarioInterface
{
const COUNTRY_NAME = 'Australia';
public const COUNTRY_NAME = 'Australia';

/**
* @param int $n the number of authors
* @return Author|Author[]
* @param mixed $args the number of authors
* @return \TestApp\Model\Entity\Author|\TestApp\Model\Entity\Author[]
*/
public function load($n = 1, ...$args)
{
$n = $args[0];

return AuthorFactory::make($n)->fromCountry(self::COUNTRY_NAME)->persist();
}
}
1 change: 0 additions & 1 deletion tests/Scenario/SubFolder/SubFolderScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
namespace CakephpFixtureFactories\Test\Scenario\SubFolder;


use CakephpFixtureFactories\Scenario\FixtureScenarioInterface;

class SubFolderScenario implements FixtureScenarioInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
use Cake\ORM\Table;

abstract class AbstractPluginTable extends Table
{}
{
}
1 change: 0 additions & 1 deletion tests/TestApp/src/Model/Entity/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace TestApp\Model\Entity;

use Cake\ORM\Entity;
use CakephpFixtureFactories\Test\TestCase\Factory\BaseFactoryAssociationsTest;

/**
* Country Entity
Expand Down
3 changes: 2 additions & 1 deletion tests/TestApp/src/Model/Table/AbstractAppTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
use Cake\ORM\Table;

abstract class AbstractAppTable extends Table
{}
{
}
3 changes: 2 additions & 1 deletion tests/TestApp/src/Model/Table/ArticlesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public function initialize(array $config)
public function findPublished(Query $query, array $options): Query
{
$query->where([
$this->aliasField('published') => 1
$this->aliasField('published') => 1,
]);

return $query;
}
}
Empty file.
2 changes: 1 addition & 1 deletion tests/TestCase/Command/SetupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SetupCommandTest extends TestCase
public function setUp()
{
$this->setupCommand = new SetupCommand();
$this->io = new ConsoleIo();
$this->io = new ConsoleIo();
}

public function tearDown()
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function testArticlesFindPublished()
$result = ArticleFactory::find('published')
->find('list')
->toArray();

$expected = [
$articles[0]->id => $articles[0]->title,
$articles[1]->id => $articles[1]->title,
Expand Down
26 changes: 16 additions & 10 deletions tests/TestCase/Event/ModelEventsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ public function testBeforeMarshalOnTable()

public function testBeforeMarshalOnTableHandled()
{
ModelEventsHandler::handle($this->Countries);
$country = $this->Countries->newEntity(['name' => 'Foo']);
$Countries = TableRegistry::getTableLocator()->get('Countries');
(new ModelEventsHandler([], []))->handle($Countries);
$country = $Countries->newEntity(['name' => 'Foo']);
$this->assertNull($country->get('beforeMarshalTriggered'));
}
public function testBeforeMarshalOnTableHandledPermissive()
{
ModelEventsHandler::handle($this->Countries, ['Model.beforeMarshal']);
$country = $this->Countries->newEntity(['name' => 'Foo']);
$Countries = TableRegistry::getTableLocator()->get('Countries');
(new ModelEventsHandler(['Model.beforeMarshal'], []))->handle($Countries);
$country = $Countries->newEntity(['name' => 'Foo']);
$this->assertTrue($country->get('beforeMarshalTriggered'));
}

Expand All @@ -77,17 +79,21 @@ public function testBeforeSaveInBehaviorOnTable()

public function testBeforeSaveInBehaviorOnTableHandled()
{
ModelEventsHandler::handle($this->Articles);
$article = $this->Articles->newEntity(['title' => 'Foo']);
$this->Articles->saveOrFail($article);
$Articles = TableRegistry::getTableLocator()->get('Articles');
(new ModelEventsHandler([], []))->handle($Articles);

$article = $Articles->newEntity(['title' => 'Foo']);
$Articles->saveOrFail($article);
$this->assertNull($article->get('beforeSaveInBehaviorTriggered'));
}

public function testBeforeSaveInBehaviorOnTableHandledPermissive()
{
ModelEventsHandler::handle($this->Articles, [], ['Sluggable']);
$article = $this->Articles->newEntity(['title' => 'Foo']);
$this->Articles->saveOrFail($article);
$Articles = TableRegistry::getTableLocator()->get('Articles');
(new ModelEventsHandler([], ['Sluggable']))->handle($Articles);

$article = $Articles->newEntity(['title' => 'Foo']);
$Articles->saveOrFail($article);
$this->assertTrue($article->get('beforeSaveInBehaviorTriggered'));
}
}
6 changes: 3 additions & 3 deletions tests/TestCase/Factory/AssociationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ public function testCollectAssociatedFactoryDeep2()
$expected = [
'City' => CityFactory::make()->getMarshallerOptions() + [
'associated' => [
'Country' => CountryFactory::make()->getMarshallerOptions(),
]
]
'Country' => CountryFactory::make()->getMarshallerOptions(),
],
],
];
$this->assertSame($expected, $AddressFactory->getAssociated());
}
Expand Down
24 changes: 10 additions & 14 deletions tests/TestCase/Factory/BaseFactoryAssociationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
use CakephpFixtureFactories\Test\Factory\CountryFactory;
use CakephpFixtureFactories\Test\Factory\CustomerFactory;
use CakephpFixtureFactories\Test\Factory\SubDirectory\SubCityFactory;
use Exception;
use TestApp\Model\Entity\Address;
use TestApp\Model\Entity\Article;
use TestApp\Model\Entity\City;
use TestApp\Model\Entity\Country;
use TestApp\Model\Entity\PremiumAuthor;
Expand Down Expand Up @@ -232,9 +230,7 @@ public function testGetAssociatedFactoryWithMultipleDepthAndMultipleTimes()
$n = 10;
$country = 'Foo';
$path = 'BusinessAddress.City.Country';
$authors = AuthorFactory::make($n)->with($path, [
'name' => $country,
])->persist();
$authors = AuthorFactory::make($n)->with($country)->persist();

for ($i = 0; $i < $n; $i++) {
$this->assertInstanceOf(Country::class, $authors[$i]->business_address->city->country);
Expand Down Expand Up @@ -313,11 +309,11 @@ public function testGetAssociatedFactoryWithReversedAssociation()
{
$name1 = 'Bar';
$name2 = 'Foo';
AuthorFactory::make(['name' => $name1])
->with('Articles.Authors', ['name' => $name2])
AuthorFactory::make($name1)
->with('Articles.Authors', $name2)
->persist();

/** @var Article $article */
/** @var \TestApp\Model\Entity\Article $article */
$article = ArticleFactory::find()
->contain('Authors', function ($q) {
return $q->order('Authors.name');
Expand Down Expand Up @@ -365,9 +361,9 @@ public function testAssignWithoutToManyAssociation()
{
$countryExpected = 'Foo';
$countryNotExpected = 'Bar';
CountryFactory::make(['name' => $countryExpected])
CountryFactory::make($countryExpected)
->with('Cities', CityFactory::make()
->with('Country', ['name' => $countryNotExpected]))
->with('Country', $countryNotExpected))
->persist();

$this->assertSame(1, CityFactory::count());
Expand Down Expand Up @@ -403,7 +399,8 @@ public function testAssignWithBelongsToManyAssociation()
$nArticles = rand(3, 10);
$authorName = 'Foo';
$article = ArticleFactory::make()
->with('Authors', AuthorFactory::make(['name' => 'Foo'])->with('Articles', $nArticles))
->with('Authors', AuthorFactory::make('Foo')
->with('Articles', $nArticles))
->persist();

$authorsAssociatedToArticle = AuthorFactory::find()
Expand Down Expand Up @@ -518,8 +515,8 @@ public function testCountryWith2Cities()
$city2 = 'B city';

$country = CountryFactory::make()
->with('Cities', ['name' => $city1])
->with('Cities', ['name' => $city2])
->with('Cities', $city1)
->with('Cities', $city2)
->persist();

// Make sure that all was correctly persisted
Expand Down Expand Up @@ -599,7 +596,6 @@ public function testCountryWith4Cities()
* associated factory
*
* @see Country::_getVirtualCities()
* @throws Exception
*/
public function testAssociationWithVirtualFieldNamedIdentically()
{
Expand Down
18 changes: 0 additions & 18 deletions tests/TestCase/Factory/BaseFactoryDisplayFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,11 @@
use Cake\Datasource\EntityInterface;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Utility\Inflector;
use CakephpFixtureFactories\Error\FixtureFactoryException;
use CakephpFixtureFactories\Factory\BaseFactory;
use CakephpFixtureFactories\Test\Factory\AddressFactory;
use CakephpFixtureFactories\Test\Factory\ArticleFactory;
use CakephpFixtureFactories\Test\Factory\AuthorFactory;
use CakephpFixtureFactories\Test\Factory\BillFactory;
use CakephpFixtureFactories\Test\Factory\CityFactory;
use CakephpFixtureFactories\Test\Factory\CountryFactory;
use CakephpFixtureFactories\Test\Factory\CustomerFactory;
use Faker\Generator;
use TestApp\Model\Entity\Address;
use TestApp\Model\Entity\Article;
use TestApp\Model\Entity\Author;
use TestApp\Model\Entity\City;
use TestApp\Model\Entity\Country;
use TestApp\Model\Table\ArticlesTable;
use TestApp\Model\Table\CountriesTable;
use TestPlugin\Model\Entity\Bill;
use TestPlugin\Model\Table\BillsTable;
use function count;
use function is_array;
use function is_int;

class BaseFactoryDisplayFieldTest extends TestCase
{
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase/Factory/BaseFactoryHiddenPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function testHiddenPropertyInMainBuild(int $n, bool $persist)
*/
public function testHiddenPropertyInBelongsToManyAssociation(int $n, bool $persist)
{
$factory = AuthorFactory::make()->with('Articles',
$factory = AuthorFactory::make()->with(
'Articles',
ArticleFactory::make($n)->withHiddenBiography(self::DUMMY_HIDDEN_PARAGRAPH)
);

Expand All @@ -112,7 +113,8 @@ public function testHiddenPropertyInBelongsToManyAssociation(int $n, bool $persi
*/
public function testHiddenPropertyInBelongsToAssociation(int $n, bool $persist)
{
$factory = BillFactory::make($n)->with('Article',
$factory = BillFactory::make($n)->with(
'Article',
ArticleFactory::make()->withHiddenBiography(self::DUMMY_HIDDEN_PARAGRAPH)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function testLoadAssociationOnTheFly_BelongsToMany_With_Magic_Persist($cl
$factory->getTable()->belongsToMany('ParallelArticles', [
'className' => $className,
'joinTable' => 'articles_authors',
'targetForeignKey' => 'article_id'
'targetForeignKey' => 'article_id',
]);

$name = 'Foo';
Expand Down
6 changes: 2 additions & 4 deletions tests/TestCase/Factory/BaseFactoryMakeWithEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

namespace CakephpFixtureFactories\Test\TestCase\Factory;


use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use CakephpFixtureFactories\Test\Factory\AddressFactory;
use CakephpFixtureFactories\Test\Factory\ArticleFactory;
use CakephpFixtureFactories\Test\Factory\AuthorFactory;


class BaseFactoryMakeWithEntityTest extends TestCase
{
public static function setUpBeforeClass(): void
Expand Down Expand Up @@ -122,8 +120,8 @@ public function testWithEntitiesAndTimes()
$authors = AuthorFactory::make($authors1, $m)->persist();

$count = 0;
for ($i=0; $i<$m; $i++) {
for ($j=0; $j<$n; $j++) {
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
$this->assertSame($authors1[$j], $authors[$count]);
$count++;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/TestCase/Factory/BaseFactoryPrimaryKeyOffsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use CakephpFixtureFactories\Test\Factory\BillFactory;
use CakephpFixtureFactories\Test\Factory\CityFactory;
use CakephpFixtureFactories\Test\Factory\CountryFactory;
use TestApp\Model\Entity\Country;

class BaseFactoryPrimaryKeyOffsetTest extends TestCase
{
Expand Down Expand Up @@ -92,7 +91,7 @@ public function testSetPrimaryKeyOffsetInMultipleAssociationAndBase()
$cityOffset = rand(1, 100000);
$countryOffset = rand(1, 100000);

/** @var Country $country */
/** @var \TestApp\Model\Entity\Country $country */
$country = CountryFactory::make()
->with('Cities', CityFactory::make($nCities)->setPrimaryKeyOffset($cityOffset))
->setPrimaryKeyOffset($countryOffset)
Expand Down
Loading

0 comments on commit 6de4c7b

Please sign in to comment.