Search code examples
shopware

Shopware 6 : how to route shopware unit test output from fixture to original folder?


I'm building unit test for feature of get files from downloads folder under public directory. But in the unit test , i'm using fixture for test files path. how i can mockup directory in my common function beetween fixture and the original pub directory?


Solution

  • Have a look at this core test: \Shopware\CI\Test\Service\ReleasePrepareServiceTest::setUp

    They use the following code to simulate folder contents:

    public function setUp(): void
    {
        $this->artifactsFilesystem = new Filesystem(new MemoryAdapter());
    
        [...]
    
        $this->artifactsFilesystem->put('install.zip', random_bytes(1024 * 1024 * 2 + 11));
        $this->artifactsFilesystem->put('install.tar.xz', random_bytes(1024 + 11));
        $this->artifactsFilesystem->put('update.zip', random_bytes(1024 * 1024 + 13));
    }
    

    The unit tests creates some in-memory file system here and fills it with some files with random data.

    I found this by checking the usages of the Filesystem class in the test folders of Shopware.

    This filesystem can be injected into the service or code you are testing.

    For example:

    public function setUp(): void
    {
        $this->myMockFileSystem = new Filesystem(new MemoryAdapter());
        $this->myMockFileSystem->put('file_we_need_in_the_test.pdf', random_bytes(1024 * 1024 * 2 + 11));
    }
    
    
    public function testSomething(): void
    {
        $service = new MyService($this->myMockFilesystem);
        $this->assertEquals('some result', $service->doSomething());
    }