Skip to content

Commit

Permalink
Fixed tests by changing their order
Browse files Browse the repository at this point in the history
The reason behind is that Laravel 6.18.35 & 7.24.0
introduced a security improvement that prevents from
mass assigning non-db fields to an Eloquent model.

The implementation is holding a reference to the list
of the table fields in the newly introduced
`guardableColumns` static property. It gets populated
with the fields from the db for the first time a mass
assignment happens on a model.

In our tests, we add fields to the cart_items table
during testing. If we hit the model first with the
original amount of fields, the second call - having the
new fields in the underlying table - won't repopulate
the `guardableColumns` static property - for obvious
reasons. However, there's no facility to reset this
list, therefore we can't reboot this property of a
model within a single run of a PHP process (namely tests).

A dirty fix was to move the test with the extended
number of columns ahead, thus those columns will be
populated. Cheap fix, but actually in a real application
it's not the case, it's only causing problems with
testing. Thus, we get away with it. We have more
important stuff to take care of ;)

- See: https://blog.laravel.com/security-release-laravel-61835-7240
- See: laravel/framework#33777
  • Loading branch information
fulopattila122 committed Sep 24, 2020
1 parent 45705e7 commit add7520
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"ext-sqlite3": "*",
"phpunit/phpunit": "~8.0|~9.0",
"orchestra/testbench": "~4.0|~5.0|~6.0",
"konekt/enum": "~3.0"
"konekt/enum": "~3.0",
"laravel/legacy-factories": "^1.0"
},
"autoload": {
"psr-4": { "Vanilo\\Cart\\": "src/" }
Expand Down
4 changes: 2 additions & 2 deletions tests/AddItemsTest.php → tests/CartItemsAddTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Contains the AddItems Test class.
* Contains the CartItemsAdd Test class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
Expand All @@ -15,7 +15,7 @@
use Vanilo\Cart\Facades\Cart;
use Vanilo\Cart\Tests\Dummies\Product;

class AddItemsTest extends TestCase
class CartItemsAddTest extends TestCase
{
/** @var Product */
protected $product1;
Expand Down

0 comments on commit add7520

Please sign in to comment.