moltin / laravel-cart

Laravel Facade and Service Provider for Moltin\Cart
276 stars 87 forks source link

while using unit test getting error #41

Open washeul opened 9 years ago

washeul commented 9 years ago

i may main layout i am using "{{Cart::totalItems(true)}} - {{ Cart::total() }}" getting error "cannot modify header information, header already sent " error in "moltin/cart/identifier/cookie.php:46" ,if i am removing the "{{Cart::totalItems(true)}} - {{ Cart::total() }}" , not getting the error & test successfully run

washeul commented 9 years ago

wherever i am using "Cart::something" in controller also getting same error & if remove that cart error solved.is there any way to fix this .

dmlogic commented 8 years ago

Came across the same problem and I have a fix.

The cause is the use of the Cookie Identifier during testing, it needs to be changed to the Runtime Identifier. You can see them doing this in the tests.

One option is to bypass the Laravel Service Provider and new-up the Cart as per the test, this is annoying though.

I settled on the following:

First publish the config file for the cart and then change the identifier value to:

'identifier' => env('CART_IDENTIFIER', 'requestcookie') (or "cookie") if you prefer.

Then add an override for the environment variable in "phpunit.xml" as follows:

<env name="CART_IDENTIFIER" value="runtime"/>

Next we need a new Service Provider as the Moltin one doesn't support the "runtime" config value. My full provider is below. Notice the addition of the check for "runtime" in the switch statement.

<?php

namespace App\Providers;

use Moltin\Cart\Identifier\Cookie as CookieIdentifier;
use Moltin\Cart\Identifier\RequestCookie as CookieRequestIdentifier;
use Moltin\Cart\Identifier\Runtime as RuntimeIdentifier;

use Moltin\Cart\CartServiceProvider as BaseProvider;

class CartServiceProvider extends BaseProvider
{
    public function getIdentifierService()
    {
        $class = $this->app['config']->get('moltincart.identifier', 'cookie');
        switch ($class) {
            case 'requestcookie':
                return new CookieRequestIdentifier;
            break;

            case 'cookie':
                return new CookieIdentifier;
            break;

            case 'runtime':
                return new RuntimeIdentifier;
            break;

            default:
                return $this->app->make($class);
            break;
        }
    }
}

Finally, change the reference from Moltin\Cart\CartServiceProvider::class to App\Providers\CartServiceProvider::class in your config\app.php file.

Tests now pass.