• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 14049246919

24 Mar 2025 09:42PM UTC coverage: 79.353% (-0.04%) from 79.391%
14049246919

push

github

web-flow
feat(support): support array parameters in string manipulations (#1073)

48 of 48 new or added lines in 2 files covered. (100.0%)

735 existing lines in 126 files now uncovered.

10492 of 13222 relevant lines covered (79.35%)

90.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/src/Tempest/Framework/Testing/ViteTester.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Framework\Testing;
6

7
use InvalidArgumentException;
8
use Tempest\Container\Container;
9
use Tempest\Container\Exceptions\ContainerException;
10
use Tempest\Core\Kernel;
11
use Tempest\Filesystem\LocalFilesystem;
12
use Tempest\Vite\TagsResolver\NullTagsResolver;
13
use Tempest\Vite\TagsResolver\TagsResolver;
14
use Tempest\Vite\Vite;
15
use Tempest\Vite\ViteConfig;
16

17
final class ViteTester
18
{
19
    private ?string $root = null;
20

21
    public function __construct(
658✔
22
        private readonly Container $container,
23
    ) {}
658✔
24

25
    /**
26
     * Sets the root directory for subsequent {@see \Tempest\Framework\Testing\ViteTester::call()} calls.
27
     */
28
    public function setRootDirectory(string $directory): self
19✔
29
    {
30
        $this->root = $directory;
19✔
31

32
        return $this;
19✔
33
    }
34

35
    /**
36
     * Clears the manifest and bridge cache.
37
     */
38
    public function clearCaches(): self
658✔
39
    {
40
        $this->container
658✔
41
            ->get(Vite::class)
658✔
42
            ->clearManifestCache()
658✔
43
            ->clearBridgeCache();
658✔
44

45
        return $this;
658✔
46
    }
47

48
    /**
49
     * Instructs Vite to not resolve tags during tests.
50
     */
51
    public function preventTagResolution(): self
658✔
52
    {
53
        $this->container->register(TagsResolver::class, fn () => new NullTagsResolver());
658✔
54

55
        return $this;
658✔
56
    }
57

58
    /**
59
     * Allows Vite to resolve tags normally.
60
     */
61
    public function allowTagResolution(): self
2✔
62
    {
63
        $this->container->unregister(TagsResolver::class);
2✔
64

65
        return $this;
2✔
66
    }
67

68
    /**
69
     * Allows Vite to try reading the manifest during tests.
70
     */
71
    public function allowUsingManifest(): self
1✔
72
    {
73
        $config = $this->container->get(ViteConfig::class);
1✔
74
        $config->useManifestDuringTesting = true;
1✔
75

76
        $this->container->config($config);
1✔
77

78
        return $this;
1✔
79
    }
80

81
    /**
82
     * Instructs Vite to not read the manifest during tests.
83
     */
84
    public function preventUsingManifest(): self
1✔
85
    {
86
        $config = $this->container->get(ViteConfig::class);
1✔
87
        $config->useManifestDuringTesting = false;
1✔
88

89
        $this->container->config($config);
1✔
90

91
        return $this;
1✔
92
    }
93

94
    /**
95
     * Creates a temporary environment with the specified `$root` and `$files`, so Vite can read a manifest or a bridge file.
96
     *
97
     * ```
98
     * $this->vite->callWithFiles(
99
     *     callback: function (string $bridgeFilePath): void {
100
     *         // Do something with Vite
101
     *         // $vite = $this->container->get(Vite::class);
102
     *     },
103
     *     files: [
104
     *         'public/vite-tempest' => ['url' => 'http://localhost:5173'],
105
     *         'public/build/manifest.json' => [ (manifest content) ],
106
     *     ],
107
     *     root: __DIR__ . '/tmp',
108
     * );
109
     * ```
110
     */
111
    public function call(callable $callback, array $files, bool $manifest = false, ?string $root = null): void
21✔
112
    {
113
        $actualViteConfig = $this->container->get(ViteConfig::class);
21✔
114
        $temporaryViteConfig = clone $actualViteConfig;
21✔
115

116
        if (! $manifest) {
21✔
117
            $temporaryViteConfig->useManifestDuringTesting = true;
21✔
118
        }
119

120
        $actualRootDirectory = $this->container->get(Kernel::class)->root;
21✔
121
        $temporaryRootDirectory = $root ?? $this->root ?? throw new InvalidArgumentException('`callWithFiles` requires a temporary root directory.');
21✔
122

123
        try {
124
            $tagsResolver = $this->container->get(TagsResolver::class);
20✔
UNCOV
125
        } catch (ContainerException) {
×
126
            $tagsResolver = null;
×
127
        }
128

129
        $filesystem = new LocalFilesystem();
20✔
130
        $filesystem->deleteDirectory($temporaryRootDirectory, recursive: true);
20✔
131
        $filesystem->ensureDirectoryExists($temporaryRootDirectory);
20✔
132

133
        $paths = [];
20✔
134

135
        foreach ($files as $path => $content) {
20✔
136
            $path = "{$temporaryRootDirectory}/{$path}";
19✔
137
            $paths[] = $path;
19✔
138
            $filesystem->ensureDirectoryExists(dirname($path));
19✔
139
            $filesystem->write($path, is_array($content) ? json_encode($content, flags: JSON_UNESCAPED_SLASHES) : $content);
19✔
140
        }
141

142
        $this->container->get(Kernel::class)->root = $temporaryRootDirectory;
20✔
143
        $this->container->config($temporaryViteConfig);
20✔
144
        $this->container->unregister(TagsResolver::class);
20✔
145
        $callback(...$paths);
20✔
146
        $this->container->get(Kernel::class)->root = $actualRootDirectory;
18✔
147
        $this->container->config($actualViteConfig);
18✔
148

149
        if ($tagsResolver) {
18✔
150
            $this->container->register(TagsResolver::class, fn () => $tagsResolver);
18✔
151
        }
152

153
        $filesystem->deleteDirectory($temporaryRootDirectory, recursive: true);
18✔
154
    }
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc