1

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' => 'usuariocomum1@gmail.com',
                '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

Community
  • 1
  • 1
Ricardo
  • 677
  • 4
  • 11
  • 35
  • Where do you do the bootstrapping / autoloading? – Halcyon Aug 31 '15 at 12:30
  • In folder: [ProjectName]/tests/ has 2 folders Fixture and TestCase and 1 file bootstrap.php, how can I bootstrap? – Ricardo Aug 31 '15 at 12:42
  • 1
    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: https://phpunit.de/manual/current/en/appendixes.configuration.html – Halcyon Aug 31 '15 at 12:45
  • @Halcyon Where I put this XML config? I put my folders – Ricardo Aug 31 '15 at 12:53
  • How did you install phpunit? – Holt Aug 31 '15 at 13:08
  • @Holt with composer (add images) – Ricardo Aug 31 '15 at 13:09
  • @ricardo Then you should use `phpunit` by running `vendor/bin/phpunit` from your `Shopping` folder because the xml config file is located in the root (`Shopping`) folder. – Holt Aug 31 '15 at 13:11
  • @Holt the only way to execute phpunit is the way in my question: c:\xampp\htdocs\PROJETOS\Shopping\vendor\phpunit> (any possible to change ?) – Ricardo Aug 31 '15 at 13:13
  • @ricardo Where did you get this information? See [Cakephp doc](http://book.cakephp.org/3.0/en/development/testing.html#install-phpunit-with-composer) which call it my way. – Holt Aug 31 '15 at 13:16
  • @Holt but this way doesn't work, I use composer normally and generated the phpunit folder in vendor but nothing about phpunit in vendor/bin folder (add Image) – Ricardo Aug 31 '15 at 13:18
  • @ricardo If you don't have `phpunit` in `vendor/bin` folder then you probably have a bad `phpunit` installation, you should remove it and install it using `php composer.phar require phpunit/phpunit:4.8` from your `Shopping` folder. – Holt Aug 31 '15 at 13:20
  • @Holt I will remove phpunit with composer and reinstall and put the result here – Ricardo Aug 31 '15 at 13:21
  • @Holt When I remove phpunit with composer follow that answer: http://stackoverflow.com/questions/26930816/how-to-remove-unused-dependencies-from-composer/29437976#29437976 still got a folder phpunit with a strange file (It is OK?) (Add image in question) – Ricardo Aug 31 '15 at 13:37
  • 1
    @ricardo If you did not get any error when removing `phpunit` using `composer`, you can remove the `vendor/phpunit` folder. – Holt Aug 31 '15 at 13:41
  • @Holt Thank you!! I reinstall phpunit and in folder vendor/bin was generated a phpunit file and the test works. Make an answer so I can give +25 – Ricardo Aug 31 '15 at 13:58

1 Answers1

3

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
Community
  • 1
  • 1
Holt
  • 36,600
  • 7
  • 92
  • 139