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

drop and create tables with fixtures on cakephp #5693

Closed
AlessandroMinoccheri opened this issue Jan 18, 2015 · 12 comments
Closed

drop and create tables with fixtures on cakephp #5693

AlessandroMinoccheri opened this issue Jan 18, 2015 · 12 comments
Assignees
Milestone

Comments

@AlessandroMinoccheri
Copy link

I would like to create test where I'm loading fixtures so test drop tables and create it with records.

My problem is that: Tests drop tables but it doesn't recreate it.

This is my model where I'm loading fixtures:

App::uses('Post', 'Model');

class PostTest extends CakeTestCase {
    public $fixtures = array('app.post');

    public function setUp() {
        parent::setUp();
        $this->Post         = ClassRegistry::init('Post');
    }

    public function testSetTemplate() {

        $this->assertEquals('1', '1');
    }
}

And this is my fixture file:

class PostFixture extends CakeTestFixture {
    public  $import = array('model' => 'Post', 'records' => true);

    public $name = 'Post'; 
    public  $table = 'posts';

    public $fields = array(
        'id'                => array('type' => 'integer', 'length' => 36, 'key' => 'primary'),
        'created'        => array('type' => 'datetime'),
        'modifed'        => array('type' => 'datetime')
    );

    public $records = array(
        array(
            'id'                => '1',
            'created'           => '2014-01-17 12:31:41',
            'modified'          => '2014-07-22 09:50:42'
        ),
    );
}

I have also tried with:

public  $import = array('model' => 'Post', 'records' => false);

Same thing tests drop tables but doesn't create it.

I need to create it after drop with my records.

Where s the error? Why tests doesn't recreate it table posts but only drop it?

Thanks

@jadb
Copy link
Contributor

jadb commented Jan 18, 2015

@AlessandroMinoccheri in both cases you only tried importing from your database an not using the fixture records. Get rid of the import line or keep that and get rid of the rest (but if using import and no records in your database, then no records for tests).

@AlessandroMinoccheri
Copy link
Author

@jadb I have removed the line

public  $import = array('model' => 'Post', 'records' => true);

same problem: test drops tables and doesn't create it

@jadb jadb added the support label Jan 18, 2015
@ADmad ADmad added this to the 3.0.0 milestone Jan 18, 2015
@jadb jadb added the fixtures label Jan 18, 2015
@jadb jadb modified the milestones: 2.6.2, 3.0.0 Jan 18, 2015
@jadb
Copy link
Contributor

jadb commented Jan 18, 2015

@AlessandroMinoccheri - can you try an actual test? It could be that the fixture is not being triggered to create the tables yet.

Also, how do you know it's dropping any tables? Are you testing on your default database or test database?

@AlessandroMinoccheri
Copy link
Author

I'm testing on test database, I see from phpmyadmin that the table posts is dropped but not regenerated, if I launch test return me error because the tables couldn't be found

@jadb
Copy link
Contributor

jadb commented Jan 18, 2015

Have you checked logs for errors? Are you on IRC?

@AlessandroMinoccheri
Copy link
Author

No I'm not on IRC and I don't have check in error logs, I'll check it!

@AlessandroMinoccheri
Copy link
Author

Seems that there aren't errors

@AlessandroMinoccheri
Copy link
Author

There is an error in error.logs but I don't know if is the problem:

Request URL: /app/webroot/img/test-fail-icon.png
Stack Trace:
#0 /Users/alessandrominoccheri/Sites/mySite/lib/Cake/Routing/Dispatcher.php(191): Controller->invokeAction(Object(CakeRequest))
#1 /Users/alessandrominoccheri/Sites/mySite/lib/Cake/Routing/Dispatcher.php(165): Dispatcher->_invoke(Object(AppController), Object(CakeRequest))
#2 /Users/alessandrominoccheri/Sites/mySite/app/webroot/index.php(97): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#3 {main}
2015-01-18 23:00:01 Error: [MissingActionException] Action AppController::webroot() could not be found.
Exception Attributes: array (
  'controller' => 'AppController',
  'action' => 'webroot',
)

@markstory markstory self-assigned this Jan 19, 2015
@markstory
Copy link
Member

I get the following error when I run your test/fixture with the $import line commented out.

Error: Fixture invalid: Count of fields does not match count of values in PostFixture
The field 'modified' is in the data fixture but not in the schema.

You have a typo in your fixture data.

@AlessandroMinoccheri
Copy link
Author

I have fixed the typo but same error, It can't recreate tables

@AlessandroMinoccheri
Copy link
Author

So could you please reopen the issue?

@AD7six
Copy link
Member

AD7six commented Jan 19, 2015

@AlessandroMinoccheri you aren't demonstrating a problem with the framework, and are drawing false conclusions.

I see from phpmyadmin that the table posts is dropped but not regenerated

Fixtures are dropped at the end of a test - The only way you'll see them lying around after running a test is if the test case fails to complete (e.g. triggers a fatal error during a test). If you have a test case that is failing because fixture data is not being loaded - by all means please demonstrate that. But bear in mind the core test suite uses fixtures (extensively) and the tests work.

In future please keep support requests to an appropriate place =).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants