Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixture refs and sqlite caching #19

Open
tiberiu89 opened this issue Jun 26, 2019 · 10 comments
Open

Fixture refs and sqlite caching #19

tiberiu89 opened this issue Jun 26, 2019 · 10 comments
Labels
bug Something isn't working

Comments

@tiberiu89
Copy link

tiberiu89 commented Jun 26, 2019

It seems that loading fixtures and setting references are not working when the sqlite caching is enabled. It gives an array to string conversion error.

/app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:3034
/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:491
/app/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php:78
/app/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php:105
/app/vendor/liip/test-fixtures-bundle/src/Services/DatabaseBackup/SqliteDatabaseBackup.php:68
/app/vendor/liip/test-fixtures-bundle/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php:52
/app/vendor/liip/test-fixtures-bundle/src/Test/FixturesTrait.php:110

@alexislefebvre alexislefebvre added the bug Something isn't working label Jun 30, 2019
@magnetik
Copy link
Contributor

magnetik commented Jul 2, 2019

Cannot confirm as I'm using sqlite cache & reference extensively.

Maybe you can share more code?

@tiberiu89
Copy link
Author

Not much code to share. So I'm using standard doctrine fixtures for a symfony app, set some references in there(I'm using value objects and custom doctrine types if relevant) call ->persist() and then ->setReference(...)

Then in my tests, I have a base class web test case extending Liip\FunctionalTestBundle\Test\WebTestCase with the following setup(taken from the class)

    use FixturesTrait;

    /** @var KernelBrowser */
    protected $client;

    /** @var ReferenceRepository */
    protected static $fixtures;

    public static function setUpBeforeClass()
    {
        static::$fixtures = null;
    }

    public function setUp()
    {
        $this->client = $this->makeClient();

        // we load fixtures once per test suite so data is kept between tests
        // might as well work with `@depends`  annotations
        if (!static::$fixtures) {
            static::$fixtures = $this->loadFixtures([TestFixtures::class])
                ->getReferenceRepository();
        }
    }

I keep fxtures static there and try to instantiate it only once for each test case. Pretty much else is standard functional tests. The doctrine test config is

doctrine:
  dbal:
    driver: 'pdo_sqlite'
    path: '%kernel.cache_dir%/test.db'
    charset: utf8

Tests run fine as long as I use it without db cache, but once I add this to the config

liip_functional_test: ~
liip_test_fixtures:
  cache_db:
    sqlite: liip_test_fixtures.services_database_backup.sqlite

it will crash subsequent tests. I can see some files created in the cache directory(test_sqlite_somehash.db and .db.ser)

The error is as described above and it happens on this line
static::$fixtures = $this->loadFixtures([TestFixtures::class]), and by the looks of it, only when trying to recreate it from the cached db files

@alexislefebvre
Copy link
Collaborator

Please share the exception name. You only pasted the stack trace.

Anyway, if you use the cache you don't need to load the fixtures only once. So, try to load fixtures in setUp and tell us if the error is still present.

@mtanasiewicz
Copy link

Same happens to me. Stack trace:

Array to string conversion
 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:3036
 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:491
 /var/www/symfony/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php:78
 /var/www/symfony/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php:105
 /var/www/symfony/vendor/liip/test-fixtures-bundle/src/Services/DatabaseBackup/SqliteDatabaseBackup.php:68
 /var/www/symfony/vendor/liip/test-fixtures-bundle/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php:52
 /var/www/symfony/vendor/liip/test-fixtures-bundle/src/Test/FixturesTrait.php:87

@mtanasiewicz
Copy link

mtanasiewicz commented Sep 25, 2019

I use postgreSql with id as UuidType, and those id's are being somehow lost on backup and restore of cache. When I load the fixtures with doctrine:fixtures:load --env=test where i have the sqlite driver configred it generates proper Uuid id's in the sqlite database file. When I debug the test exception with xdebug, I can see that in Doctrine\Common\DataFixtures\ProxyReferenceRepository::unserialize() the variable $serializedData comes with such values:

{"references":{"breakfast":["App\\Recipe\\Domain\\Entity\\Category",{"id":{}}],"main course":["App\\Recipe\\Domain\\Entity\\Category",{"id":{}}],"snack":["App\\Recipe\\Domain\\Entity\\Category",{"id":{}}],"ingredient_0":["App\\Recipe\\Domain\\Entity\\Ingredient",{"id":{}}],"ingredient_1":

This indicates that there might be some problem wtith id's during backup and restore of sqlite files or serialization.

@alexislefebvre
Copy link
Collaborator

Thanks for the information. The problem with uuid was also reported in liip/LiipFunctionalTestBundle#422 (comment)

We need to add a test with fixtures that use uuid.

@bropp
Copy link

bropp commented Sep 25, 2019

I also get this error when testing fixtures who have a composite primary key (multiple fields combined are the primary key).

i.e. (https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/composite-primary-keys.html#use-case-3-join-table-with-metadata):

/** @Entity */ 
class OrderItem {     
/** @Id @ManyToOne(targetEntity="Order") */     
private $order;      

/** @Id @ManyToOne(targetEntity="Product") */     
private $product;     

 /** @Column(type="integer") */     
private $amount = 1;      

/** @Column(type="decimal") */     
private $offeredPrice;      

public function __construct(Order $order, Product $product, $amount = 1)     {         
    $this->order = $order;         
    $this->product = $product;        
    $this->offeredPrice = $product->getCurrentPrice();     
}
}

@mtanasiewicz
Copy link

@alexislefebvre thank you for the answer! I will check tomorrow if this solves my problem.

@mtanasiewicz
Copy link

@alexislefebvre Unfortunately changing Uuid generation to auto and setting proper metadata doesn't work at all. I still get the exception "array to string conversion"

@mtanasiewicz
Copy link

Also when I try to implement my own Backup with \Liip\TestFixturesBundle\Services\DatabaseBackup\DatabaseBackupInterface and connect it according to docs, the library still doesn't use it. Completely nothing happens there as it falled back to default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants