Comments (7)
As I look deeper I see both this section on validation behaior which says:
all validations are deactivated when creating CakePHP entities
and persisting them to the database.
and the comment on setDefaultTemplate()
:
* Defines the default values of you factory. Useful for
* not nullable fields. You may use methods of the factory here too.
So my tests may have been previously passing due to some interaction related to the old cake factories because I had both listeners turned on at that point.
from cakephp-fixture-factories.
Let us know, if you are experiencing further issues there.
from cakephp-fixture-factories.
This is an ongoing issue. Where previously I did not have to create any code in setDefaultTemplate()
now I must define defaults for columns that don't otherwise have them.
from cakephp-fixture-factories.
setDefaultTemplate()
is there to assign default values of your factory, as you write.
What is the issue you are actually having? Is there anyting we can improve or solve?
from cakephp-fixture-factories.
This is my database schema. It has columns that do not have defaults but are not allowed to be NULL.
This is my test class:
<?php
namespace App\Test\TestCase\Model\Table;
use App\Test\Factory\PersonFactory;
class PeopleTableTest extends \Cake\TestSuite\TestCase
{
public function testMake()
{
$person = PersonFactory::make(1)->persist();
}
}
Running the test results in this error.
My expectation based on the documentation and my previous experience is that the entity could be created without error in this circumstance.
My factory is baked from the most recent version:
from cakephp-fixture-factories.
I do not know how your code was before, but this should normally not have worked before either.
In your case, you should have in your factory:
protected function setDefaultTemplate(): void
{
$this->setDefaultData(function(Generator $faker) {
return [
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'role' => $faker->text(50)
];
});
}
Your test could then look like this:
class PeopleTableTest extends \Cake\TestSuite\TestCase
{
public function testMake()
{
$person = PersonFactory::make()->persist();
}
}
or like that, if you want to control the value of the field role
:
class PeopleTableTest extends \Cake\TestSuite\TestCase
{
public function testMake()
{
$person = PersonFactory::make(['role' => 'Some role'])->persist();
}
}
Does this make sense?
from cakephp-fixture-factories.
Perfect sense, thanks.
from cakephp-fixture-factories.
Related Issues (20)
- Skip cake entity setter when set fields data HOT 5
- How to specify database connection other than `test`? HOT 1
- How to nest ->with() HOT 2
- Feature requests: Enhancements to configuration HOT 6
- Recurrent PersistenceExceptions thrown in factories due to Primary Key collisions HOT 6
- Fix Psalm
- Composer require no DB cleaner
- Calling fixture factories for a table breaks application code that uses that table downstream HOT 4
- Primary key of type text should generate UUID HOT 1
- What is the mergeAssociated(array $data) function for? HOT 1
- Testing simple method without persisting data HOT 1
- fakerphp 8.2 warnings HOT 1
- Creating multiple translations using shadowStrategy HOT 1
- Truncate Dirty Tables Slowness HOT 2
- Feature Request: Add back-reference property to sub-entities HOT 5
- Error: Column "id" is an identity column defined as GENERATED ALWAYS HOT 1
- Feature Request: Ability to set nested values using `make` method HOT 2
- Enhance array notation
- Calling `with()` inside `setDefaultTemplate()` occurs unexpected increase of the associated entities HOT 4
- phpdoc @param types of `FactoryAwareTrait::getFactory()` is too narrower than acceptable types HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cakephp-fixture-factories.