1
votes

I'm using phpunit with cakephp for testing but when I try to run some test class is throw a Fatal Error but this class is inserted correctly and is not even accused missing file Error by PHPStorm.

The command to execute a testcase:

c:\xampp\htdocs\PROJETOS\Shopping\vendor\phpunit>phpunit "C:/xampp/htdocs/PROJETOS/Shopping/tests/TestCase/Model/Table/UsersTableTest.php"

The Fatal Error:

Fatal error: Class 'Cake\TestSuite\TestCase' not found in C:\xampp\htdocs\PROJET OS\Shopping\tests\TestCase\Model\Table\UsersTableTest.php on line 12

The test case:

<?php
namespace App\Test\TestCase\Model\Table;

use App\Model\Table\UsersTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
 * App\Model\Table\UsersTable Test Case
 */
class UsersTableTest extends TestCase
{

    /**
     * Fixtures
     *
     * @var array
     */
    public $fixtures = [
        'app.users',
        'app.user_types',
        'app.bookings',
        'app.stores'
    ];

    /**
     * setUp method
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        $config = TableRegistry::exists('Users') ? [] : ['className' => 'App\Model\Table\UsersTable'];
        $this->Users = TableRegistry::get('Users', $config);
    }

    /**
     * tearDown method
     *
     * @return void
     */
    public function tearDown()
    {
        unset($this->Users);

        parent::tearDown();
    }

    /**
     * Test initialize method
     *
     * @return void
     */
    public function testInitialize()
    {
        $this->markTestIncomplete('Not implemented yet.');
    }

    /**
     * Test validationDefault method
     *
     * @return void
     */
    // Método que deverá testar o método "validationDefault()" de "UsersTable",
    // mas como e quando este método será chamado?
    // A ferramenta seleciona o método a ser executado
    public function testValidationDefault()
    {
        $this->markTestIncomplete('Not implemented yet.');
    }

    /**
     * Test buildRules method
     *
     * @return void
     */
    public function testBuildRules()
    {
        $this->markTestIncomplete('Not implemented yet.');
    }

    public function testFindUserById(){
        $query = $this->Users->find('userById', [
            'conditions' => ['Users.id' => 900000],
            'fields' => ['Users.id', 'Users.email', 'Users.password',
                'Users.username', 'Users.user_type_id', 'Users.created',
                'Users.modified']
        ]);
        $this->assertInstanceOf('Cake\ORM\Query', $query);
        $result = $query->hydrate(false)->toArray();

        $expected = [
            [
                'id' => 900000,
                'email' => '[email protected]',
                'password' => 'usuariocomum1senha',
                'username' => 'usuariocomum1username',
                'user_type_id' => 900000,
                'created' => '2015-07-17 18:46:47',
                'modified' => '2015-07-17 18:46:47'
            ]
        ];

        $this->assertEquals($expected, $result);
    }
}

In folder: [ProjectName]/tests/ has 2 folders Fixture and TestCase and 1 file bootstrap.php

My folder Structure:

inserir a descrição da imagem aqui

config and tests folders:

enter image description here

phpunit folder (inside vendor folder (installed with composer))

enter image description here

bin folder in vendor folder

enter image description here

When I remove phpunit with composer follow that answer: How to remove unused dependencies from composer? still got a folder phpunit with a strange file (It is OK?)

enter image description here

NOTE: I'm using CakePHP 3.0.11 and PHPHUnit 4.8

1
Where do you do the bootstrapping / autoloading?Halcyon
In folder: [ProjectName]/tests/ has 2 folders Fixture and TestCase and 1 file bootstrap.php, how can I bootstrap?Ricardo
You can pass a configuration file to phpunit that tells it how to bootstrap. I don't know how it works. Here is the doc: phpunit.de/manual/current/en/appendixes.configuration.htmlHalcyon
@Halcyon Where I put this XML config? I put my foldersRicardo
@ricardo If you did not get any error when removing phpunit using composer, you can remove the vendor/phpunit folder.Holt

1 Answers

3
votes

According to CakePHP documentation, you should run the phpunit executable located in the vendor/bin folder (there is a phpunit.xml.dist file in your CakePHP app so you should run the executable from your root folder):

C:\...\Shopping> vendor\bin\phpunit tests\TestCase\Model\Table\UsersTableTest.php

If you don't have such executable, you probably have a "bad" phpunit installation. You should remove it (and the vendor/phpunit folder) and reinstall it using the following:

php composer.phar require phpunit/phpunit:4.8