2

When I run the service from browser it works, but if i execute the test file i get this error:

1) Api\Domain\Tests\ServiceTest::testSetUp
RuntimeException: A facade root has not been set.

/WWW/api/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:210
/WWW/api/src/Api/Infrastructure/Domain/Model/SolrBaseRepository.php:30
/WWW/api/src/Api/Infrastructure/Domain/Model/SolrBaseRepository.php:30
/WWW/api/src/Api/Domain/Tests/ServiceTest.php:80

File confguration: config/solr.php

return array(
    'endpoint' => array(
        'localhost' => array(
            'host' => '192.168.1.100',
            'port' => 8080,
            'path' => '/solr/clients/',
        )
    )
);

Solr base repository:

use Illuminate\Support\Facades\Config;

abstract class SolrBaseRepository
protected $client;


    /**
     * SolrBaseRepository constructor.
     */
    public function __construct(){
        $this->client = new \Solarium\Client(Config::get('solr'));
    }
}

if I change

$this->client = new \Solarium\Client(Config::get('solr'));

by

$this->client = new \Solarium\Client(array(
            'endpoint' => array(
                'localhost' => array(
                    'host' => '192.168.1.100',
                    'port' => 8080,
                    'path' => '/solr/clients/',
                )
            )
        ));

The test works. ServiceTest: Api/Domain/Tests/ServiceTest.php

class ServiceTest extends \PHPUnit_Framework_TestCase{
public function testSetUp()
    {
        $this->setUp();
        $this->solrServicesRepository = New SolrServicesRepository();
    }
}

I have tried to resolve it adding from this page to my ServiceTest.php:

use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;

/**
* Setup a new app instance container
* 
* @var Illuminate\Container\Container
*/
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');

/**
* Set $app as FacadeApplication handler
*/
Facade::setFacadeApplication($app);

But this does not work for mi.

mstafkmx
  • 419
  • 5
  • 17

1 Answers1

2

It's been a while, but I found the answer as I got stuck with the same problem.

In your test, you need to call the parent setup function, as it initializes:

class ExampleTest extends TestCase
{

    public function setUp()
    {
        parent::setUp();
        $this->faker = Faker\Factory::create();
        // other lines of code that you need
    }

    public function testWithFacades()
    {
         //now it works
    }
}