FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

Uploadbehavior generates an error when a Mocked model is initialized in CakePHP 3.3 #412

Closed roelsadza closed 7 years ago

roelsadza commented 8 years ago

Hello,

I've encountered an error while adding a test case in which I mock a model that has the UploadBehavior in CakePHP 3.3.

When using getMockForModel, the testcase initializes the model. Then line 40 of UploadBehavior.php (UploadBehavior::initialize) tries to access the _table property. $schema = $this->_table->schema();

It tries to access the table schema, which is not available for the mocked model.

The error is: PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.mock__users_table_b4e' doesn't exist

The stack trace:

/var/www/cake-project/vendor/cakephp/cakephp/src/Database/Statement/MysqlStatement.php:36
/var/www/cake-project/vendor/cakephp/cakephp/src/Database/Connection.php:290
/var/www/cake-project/vendor/cakephp/cakephp/src/Database/Connection.php:246
/var/www/cake-project/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:129
/var/www/cake-project/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:97
/var/www/cake-project/vendor/cakephp/cakephp/src/Database/Schema/CachedCollection.php:63
/var/www/cake-project/vendor/cakephp/cakephp/src/ORM/Table.php:440
/var/www/cake-project/vendor/josegonzalez/cakephp-upload/src/Model/Behavior/UploadBehavior.php:40
/var/www/cake-project/vendor/cakephp/cakephp/src/ORM/Behavior.php:166
/var/www/cake-project/vendor/cakephp/cakephp/src/ORM/BehaviorRegistry.php:130
/var/www/cake-project/vendor/cakephp/cakephp/src/Core/ObjectRegistry.php:93
/var/www/cake-project/vendor/cakephp/cakephp/src/ORM/Table.php:618
/var/www/cake-project/src/Model/Table/UsersTable.php:120
/var/www/cake-project/vendor/cakephp/cakephp/src/ORM/Table.php:290
/var/www/cake-project/vendor/cakephp/cakephp/src/TestSuite/TestCase.php:657

For now I have worked around this by also mocking the addBehavior method, so that the uploadbehavior is not loaded in this test. However, this means that also other behaviors are not loaded such as timestamp behavior, etc.

josegonzalez commented 8 years ago

Mock the associated table for this behavior?

roelsadza commented 8 years ago

What do you mean by associated table?

Currently I have the users table, which has the Upload behavior for the avatar field.

When I use getMockForModel('Users') I get this error.

davidyell commented 7 years ago

You will struggle to mock these, as you'll mock the table, then the schema, then you'll need a mock connection as well, and so on.

Better to just use an actual instance, to save on code imho.