Giter VIP home page Giter VIP logo

Comments (4)

pabloelcolombiano avatar pabloelcolombiano commented on May 28, 2024

@jamisonbryant thank for opening the issue. A sample app would be definitely appreciated, in order to reproduce the issue.

from cakephp-fixture-factories.

jamisonbryant avatar jamisonbryant commented on May 28, 2024

I haven't had time to put together a sample app, but I do think I have discovered something relevant. Adding these two lines:

// vendor/vierge-noire/cakephp-fixture-factories/src/Factory/EventCollector.php:91
$conn = $table->getConnection();
echo __CLASS__ . ' table connection = ' . PHP_EOL . $conn->configName() . PHP_EOL;

prints "foo" which is one of my database connections instead of "test_foo" as I would expect.

Is it my understanding that when the Cake test suite is running, the defaultConnectionName() of each table is supposed to be prepended by "test_" forming the complete connection name of "test_foo".

I'm not sure what an event collector does, but is there any chance it is not respecting the default cake convention for table names when running unit tests? Could that be the source of this problem?

from cakephp-fixture-factories.

jamisonbryant avatar jamisonbryant commented on May 28, 2024

Here is more evidence that the BaseFactory's $eventCollector is flipping back and forth between connections:

CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (bottom check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = test_saas
CakephpFixtureFactories\Factory\EventCollector table connection (bottom check) = saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = saas
CakephpFixtureFactories\Factory\EventCollector table connection (top check) = saas

Top check = EventCollector::getTable() line 78, inside the if block
Bottom check = EventCollector::getTable() line 99, right before the function return

from cakephp-fixture-factories.

jamisonbryant avatar jamisonbryant commented on May 28, 2024

Mar 2023 update: I was able to properly resolve this issue, at long last!

I was right about application code trying to access the table registry at the same time as the unit tests, and using different connections to do so. E.g. my fixture factories would create and persist entities using the test connection, and my app would do the same except using the app connection. For my project, these are two different databases.

The solution is to alias the connections in the test bootstrapper:

// file: tests/bootstrap.php
$testDatasource = 'foo';
try {
	ConnectionManager::get($testDatasource);
	ConnectionManager::alias($testDatasource, 'default');
	ConnectionManager::alias($testDatasource, 'test');
	ConnectionManager::alias($testDatasource, 'other');

	//(new SchemaLoader())->loadSqlFiles(TESTS . 'schema.sql', $testConnection);
	(new Migrator())->run(['connection' => $testDatasource]);
} catch (MissingDatasourceConfigException $e) {
	trigger_error("Cannot run the test suite: missing connection '{$testDatasource}'. Check main config file.");
}

Now, even when the application tries to use the "wrong" database connection, it will go to the correct database.

I'm not sure there's a change to be made to Fixture Factories unless it's a docs clarification, do you agree @pabloelcolombiano?

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.