Visitar URL original
Don't get fixtures in unit testing running · Issue #24 · cakephp/codeception · GitHub
Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Don't get fixtures in unit testing running #24

Description

@JustDoItSascha

Hi jadb!

I'm struggling very hard with fixtures in unit testing.

I have my Testclass extend from Cake\TestSuite\TestCase. But when I try it with

public $fixtures = ['plugin.user_management.permissions'];

no fixtures are loaded. When I try

public function testAuthorize()
{
    $this->loadFixtures('Permissions');
...

I get an Exception: [PHPUnit_Framework_ExceptionWrapper] No fixture manager to load the test fixture

So how can I use the built in fixture functionality from cake in unit testing? Any ideas?

Activity

  1. JustDoItSascha commented on Sep 24, 2015

    @JustDoItSascha
    Author

    Ok, I don't know why I have to load the FixtureManager myself, but this is how it works, when I add it to the top line of setUp():

    $this->fixtureManager = new FixtureManager();
    $this->fixtureManager->fixturize($this);
    $this->loadFixtures('MyFixture');
    
  2. lorenzo commented on Sep 24, 2015

    @lorenzo
    Member

    @jadb Do you know if that is the right way to do it?

  3. jadb commented on Sep 25, 2015

    @jadb
    Contributor

    ATM, the only way is by adding it to your custom App\TestSuite\Codeception\AcceptanceHelper (or any other). I will try seeing if it can be included by default without raising errors (or act as a listener like with PHPUnit).

    <?php
    namespace App\TestSuite\Codeception;
    
    use App\TestSuite\Codeception\Helper\MailerTrait;
    use App\TestSuite\FixtureTrait;
    use Cake\Datasource\ConnectionManager;
    use Cake\TestSuite\Fixture\FixtureManager;
    use Codeception\Module;
    use Codeception\TestCase;
    
    class AcceptanceHelper extends Module
    {
        use MailerTrait;
    
        public $fixtures = ['app.users', 'app.posts'];
    
        /**
         * Load fixtures before scenario is run.
         *
         * @param TestCase $test Test case.
         * @return void
         */
        public function _before(TestCase $test)
        {
            $fixtureManager = new FixtureManager();
            $test->fixtures = $this->fixtures;
            $fixtureManager->fixturize($test);
            $fixtureManager->load($test);
        }
    
        /**
         * Drop tables after scenario is run.
         *
         * @param TestCase $test Test case.
         * @return void
         */
        public function _after(TestCase $test)
        {
            $connection = ConnectionManager::get('default');
            foreach ($this->fixtures as $fixture) {
                $parts = explode('.', $fixture);
                $connection->execute('DROP TABLE ' . array_pop($parts));
            }
        }
    }
    
  4. JustDoItSascha commented on Sep 28, 2015

    @JustDoItSascha
    Author

    @jadb But what exactly does the Framwork::_before() function? Because there you create a FixtureManager and load fixtures from TestCases...

  5. JustDoItSascha commented on Oct 7, 2015

    @JustDoItSascha
    Author

    Ok, I've found the trick. The Test Class must extend Codeception\TestCase\Test. My Class extended Cake\TestSuite\TestCase.

    So far I can see it, it works this way. One hint: I'm always talking about the Unit Testing at the moment. I see that you always focus on Acceptance Testing (in documentation and in discussions). But the Unit Testing is far more important (for me). I want to use codeception for the acceptance and functional testing as additional feature, but the most reliable feature is the unit testing.

  6. jadb commented on Oct 8, 2015

    @jadb
    Contributor

    You can continue writing the unit tests in phpunit or using codeception. Maybe I missed what you were trying to communicate?

  7. JustDoItSascha commented on Oct 8, 2015

    @JustDoItSascha
    Author

    When I want to use cakephp/codeception I'm replacing the old testing mechanism with this one. So I'm assuming that everything the cakephp unit testing provides on functionality also works with cakephp/codeception. Just with acceptance and functional testing on top.

    So when fixtures in unit testing don't work the way they worked in the classic cake unit testing, it's bad. Also the original assertions have to work.

    When people switch to this plugin their old unit tests have to work or they need an instruction what they have to change to make them work.

  8. jadb commented on Oct 8, 2015

    @jadb
    Contributor

    I hear you and understand better now. You are right.

  9. reopened this on Oct 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions