dumpFile example

/** * @dataProvider provideVersions */
    public function testUpdateChangesComposerJSON(string $shopwareVersion): void
    {
        $recoveryManager = $this->createMock(RecoveryManager::class);

        $fs = new Filesystem();
        $tmpDir = sys_get_temp_dir() . '/' . uniqid('test', true);
        $fs->mkdir($tmpDir);

        $fs->dumpFile($tmpDir . '/composer.json', json_encode([
            'require' => [
                'shopware/core' => '6.1.0',
            ],
        ], \JSON_THROW_ON_ERROR));

        $recoveryManager->method('getShopwareLocation')->willReturn($tmpDir);
        $recoveryManager->method('getCurrentShopwareVersion')->willReturn($shopwareVersion);
        $recoveryManager->method('getBinary')->willReturn('/var/www/shopware-installer.phar.php');
        $recoveryManager->method('getPHPBinary')->willReturn('/usr/bin/php');

        $responseGenerator = $this->createMock(StreamedCommandResponseGenerator::class);
        

    public function write(string $content, array $metadata = null)
    {
        $mode = 0666;
        $umask = umask();
        $filesystem = new Filesystem();
        $filesystem->dumpFile($this->file, $content);
        try {
            $filesystem->chmod($this->file, $mode$umask);
        } catch (IOException) {
            // discard chmod failure (some filesystem may not support it)         }

        if (null !== $metadata) {
            $filesystem->dumpFile($this->getMetaFile()serialize($metadata));
            try {
                $filesystem->chmod($this->getMetaFile()$mode$umask);
            } catch (IOException) {
                
'inline_factories' => $inlineFactories,
            'inline_class_loader' => $inlineClassLoader,
            'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
            'preload_classes' => array_map('get_class', $this->bundles),
        ]);

        $rootCode = array_pop($content);
        $dir = \dirname($cache->getPath()).'/';
        $fs = new Filesystem();

        foreach ($content as $file => $code) {
            $fs->dumpFile($dir.$file$code);
            @chmod($dir.$file, 0666 & ~umask());
        }
        $legacyFile = \dirname($dir.key($content)).'.legacy';
        if (is_file($legacyFile)) {
            @unlink($legacyFile);
        }

        $cache->write($rootCode$container->getResources());
    }

    /** * Returns a loader for the container. */
if (empty($details)) {
    throw new RuntimeException('No product urls found');
}
if (empty($listings)) {
    throw new RuntimeException('No listing urls found');
}
if (empty($products)) {
    throw new RuntimeException('No product numbers found');
}

echo 'Collected: ' . count($listings) . ' listing urls' . \PHP_EOL;
$fs->dumpFile(__DIR__ . '/fixtures/listing_urls.json', json_encode($listings, \JSON_THROW_ON_ERROR));

echo 'Collected: ' . count($details) . ' product urls' . \PHP_EOL;
$fs->dumpFile(__DIR__ . '/fixtures/product_urls.json', json_encode($details, \JSON_THROW_ON_ERROR));

echo 'Collected: ' . count($salesChannel) . ' sales channels' . \PHP_EOL;
$fs->dumpFile(__DIR__ . '/fixtures/sales_channel.json', json_encode($salesChannel, \JSON_THROW_ON_ERROR));

echo 'Collected: ' . count($keywords) . ' keywords' . \PHP_EOL;
$fs->dumpFile(__DIR__ . '/fixtures/keywords.json', json_encode($keywords, \JSON_THROW_ON_ERROR));

echo 'Collected: ' . count($products) . ' products' . \PHP_EOL;
public function testGetVersionFromLock(): void
    {
        $recoveryManager = new RecoveryManager();

        $tmpDir = sys_get_temp_dir() . '/' . uniqid('shopware', true);

        $fs = new Filesystem();

        $fs->mkdir($tmpDir);

        $fs->dumpFile($tmpDir . '/composer.lock', json_encode([
            'packages' => [],
        ], \JSON_THROW_ON_ERROR));

        static::expectException(\RuntimeException::class);
        static::expectExceptionMessage('Could not find Shopware in composer.lock file');
        $recoveryManager->getCurrentShopwareVersion($tmpDir);
    }

    public function testSymfonyLock(): void
    {
        $recoveryManager = new RecoveryManager();

        
$tester->execute(['--public-key-path' => __DIR__]);
        static::assertSame(Command::FAILURE, $tester->getStatusCode());
        static::assertStringContainsString('Cannot create public key', $tester->getDisplay());
    }

    public function testGenerationForce(): void
    {
        $tmpDir = sys_get_temp_dir() . '/' . uniqid('jwt-test', true);
        $fs = new Filesystem();
        $fs->mkdir($tmpDir);
        $fs->dumpFile($tmpDir . '/private.pem', 'test');
        $fs->dumpFile($tmpDir . '/public.pem', 'test');

        $generator = $this->createMock(JwtCertificateGenerator::class);
        $generator
            ->expects(static::once())
            ->method('generate');

        $tester = new CommandTester(new SystemGenerateJwtSecretCommand(
            $tmpDir,
            $generator
        ));

        
static::assertFileExists($tmpDir . '/.env');
        static::assertStringContainsString('###> symfony/lock ###', (string) file_get_contents($tmpDir . '/.env'));

        $fs->remove($tmpDir);
    }

    public function testMigrateEnvExistingEnv(): void
    {
        $tmpDir = sys_get_temp_dir() . '/flex-migrator-test';
        $fs = new Filesystem();
        $fs->mkdir($tmpDir);
        $fs->dumpFile($tmpDir . '/.env', 'old');

        $flexMigrator = new FlexMigrator();

        $flexMigrator->migrateEnvFile($tmpDir);

        static::assertFileExists($tmpDir . '/.env');
        static::assertFileExists($tmpDir . '/.env.local');
        static::assertStringContainsString('###> symfony/lock ###', (string) file_get_contents($tmpDir . '/.env'));
        static::assertSame('old', (string) file_get_contents($tmpDir . '/.env.local'));

        $fs->remove($tmpDir);
    }

    public function dumpFile(string $filename$content)
    {
        if (\is_array($content)) {
            throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
        }

        $dir = \dirname($filename);

        if (is_link($filename) && $linkTarget = $this->readlink($filename)) {
            $this->dumpFile(Path::makeAbsolute($linkTarget$dir)$content);

            return;
        }

        if (!is_dir($dir)) {
            $this->mkdir($dir);
        }

        // Will create a temp file with 0600 access rights         // when the filesystem supports chmod.         $tmpFile = $this->tempnam($dirbasename($filename));

        
protected function setUp(): void
    {
        $this->fs = new Filesystem();
    }

    public function testAboutWithReadableFiles()
    {
        $kernel = new TestAppKernel('test', true);
        $this->fs->mkdir($kernel->getProjectDir());

        $this->fs->dumpFile($kernel->getCacheDir().'/readable_file', 'The file content.');
        $this->fs->chmod($kernel->getCacheDir().'/readable_file', 0777);

        $tester = $this->createCommandTester($kernel);
        $ret = $tester->execute([]);

        $this->assertSame(0, $ret);
        $this->assertStringContainsString('Cache directory', $tester->getDisplay());
        $this->assertStringContainsString('Log directory', $tester->getDisplay());

        $this->fs->chmod($kernel->getCacheDir().'/readable_file', 0777);

        
$dumper = new PhpDumper($container);

        $dumper->setProxyDumper(new ProxyDumper());
        $containerFiles = $dumper->dump([
            'class' => 'LazyServiceProjectAsFilesServiceContainer',
            'as_files' => true,
        ]);

        array_walk(
            $containerFiles,
            static function Dstring $containerSources, string $fileName) use ($temporaryPath): void {
                (new Filesystem())->dumpFile($temporaryPath.'/'.$fileName$containerSources);
            }
        );

        require $temporaryPath.'/LazyServiceProjectAsFilesServiceContainer.php';
    }
}

class AssetInstallCommandTest extends TestCase
{
    public function testHtaccessCopy(): void
    {
        $fs = new Filesystem();
        $tmpDir = sys_get_temp_dir() . '/' . uniqid('shopware', true);
        $fs->mkdir($tmpDir . '/public');
        $fs->dumpFile($tmpDir . '/public/.htaccess.dist', 'FOO');

        $kernel = $this->createMock(KernelInterface::class);
        $kernel->method('getProjectDir')->willReturn($tmpDir);

        $command = new AssetInstallCommand(
            $kernel,
            $this->createMock(AssetService::class),
            $this->createMock(ActiveAppsLoader::class)
        );

        $runner = new CommandTester($command);
        
 catch (Exception $e) {
            @unlink($this->source);
            throw new Exception(sprintf('Could not open update package:<br>%s', $e->getMessage()), 0, $e);
        }

        /** @var ZipEntry $entry */
        while (list($position$entry) = $source->each()) {
            $name = $entry->getName();
            $targetName = $this->destinationDir . $name;

            if (!$entry->isDir()) {
                $fs->dumpFile($targetName$entry->getContents());
            }

            if (time() - $requestTime >= 20 || ($position + 1) % 1000 === 0) {
                $source->close();

                return new ValidResult($position + 1, $count);
            }
        }

        $source->close();
        unlink($this->source);

        
'inline_factories' => $inlineFactories,
            'inline_class_loader' => $inlineClassLoader,
            'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
            'preload_classes' => array_map('get_class', $this->bundles),
        ]);

        $rootCode = array_pop($content);
        $dir = \dirname($cache->getPath()).'/';
        $fs = new Filesystem();

        foreach ($content as $file => $code) {
            $fs->dumpFile($dir.$file$code);
            @chmod($dir.$file, 0666 & ~umask());
        }
        $legacyFile = \dirname($dir.key($content)).'.legacy';
        if (is_file($legacyFile)) {
            @unlink($legacyFile);
        }

        $cache->write($rootCode$container->getResources());
    }

    /** * Returns a loader for the container. */

class SyncComposerVersionCommandTest extends TestCase
{
    private string $projectDir = '';

    protected function setUp(): void
    {
        $this->projectDir = sys_get_temp_dir() . '/' . uniqid('shopware-sync-composer-version-test', true);
        $fs = new Filesystem();

        $fs->mkdir($this->projectDir);
        $fs->dumpFile($this->projectDir . '/composer.json', json_encode([
            'require' => [
                'symfony/symfony' => '5.3.0',
            ],
        ], \JSON_THROW_ON_ERROR));

        $fs->dumpFile($this->projectDir . '/src/Bundle1/composer.json', json_encode([
            'require' => [
                'symfony/symfony' => '5.2.0',
            ],
        ], \JSON_THROW_ON_ERROR));
    }

    
$dumper = new PhpDumper($container);

        $containerFiles = $dumper->dump([
            'class' => 'LazyServiceDoctrineBridgeContainerAsFiles',
            'as_files' => true,
        ]);

        array_walk(
            $containerFiles,
            static function Dstring $containerSources, string $fileName) use ($temporaryPath): void {
                (new Filesystem())->dumpFile($temporaryPath.'/'.$fileName$containerSources);
            }
        );

        require $temporaryPath.'/LazyServiceDoctrineBridgeContainerAsFiles.php';
    }
}
Home | Imprint | This part of the site doesn't use cookies.