Coder Social home page Coder Social logo

Comments (7)

dreamingmind avatar dreamingmind commented on May 18, 2024

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.

pabloelcolombiano avatar pabloelcolombiano commented on May 18, 2024

Let us know, if you are experiencing further issues there.

from cakephp-fixture-factories.

dreamingmind avatar dreamingmind commented on May 18, 2024

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.

pabloelcolombiano avatar pabloelcolombiano commented on May 18, 2024

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.

dreamingmind avatar dreamingmind commented on May 18, 2024

This is my database schema. It has columns that do not have defaults but are not allowed to be NULL.

Screen Shot 2020-06-04 at 11 41 27 AM

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.

Screen Shot 2020-06-04 at 11 47 55 AM

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:

Screen Shot 2020-06-04 at 11 50 55 AM

from cakephp-fixture-factories.

pabloelcolombiano avatar pabloelcolombiano commented on May 18, 2024

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.

dreamingmind avatar dreamingmind commented on May 18, 2024

Perfect sense, thanks.

from cakephp-fixture-factories.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.