Support the ongoing development of Laravel.io →
posted 10 years ago
Testing
Last updated 2 years ago.
0
Solution

I finally ended up inheriting my test class from that class:

<?php

class MultiDomainCase extends TestCase
{
    protected $domain;
    
    public function setUp()
    {
        if (!empty($this->domain) || !is_null($this->domain))
        {
            $configuration = $this->getConfiguration();
            $configuration['url'] = $this->domain;
            $this->saveConfiguration($configuration);
        }
        parent::setUp();
    }
    
    public function tearDown()
    {
        if (!empty($this->domain) || !is_null($this->domain))
        {
            $configuration = $this->getConfiguration();
            
            // TODO revert to initial version
            $configuration['url'] = 'http://localhost/';
            $this->saveConfiguration($configuration);
        }
        parent::tearDown();
    }
    
    private function getConfiguration()
    {
        $configurationFilePath = __DIR__.'/../config/testing/app.php';
        if (file_exists($configurationFilePath))
            return require $configurationFilePath;
        
        return null;
    }
    
    private function saveConfiguration($configuration)
    {        
        $configurationFilePath = __DIR__.'/../config/testing/app.php';
        $content = '<?php return ' . var_export($configuration, true) . ';';
        
        file_put_contents($configurationFilePath, $content);
    }
    
}

The test class would look like this:

<?php

require_once(__DIR__.'/../MultiDomainCase.php');
class DomainTest extends MultiDomainCase 
{
    protected $domain = 'http://some.website.com/';

    public function testDomainHomepage()
    {
        // Given
        
        // When
        $crawler = $this->client->request('GET', 'http://some.website.com/');
        
        // Then
        $this->assertTrue($this->client->getResponse()->isOk());
    }

}

Now I can test my subdomains.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Spir spir Joined 28 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.