File tree Expand file tree Collapse file tree 2 files changed +37
-18
lines changed
Expand file tree Collapse file tree 2 files changed +37
-18
lines changed Original file line number Diff line number Diff line change 3131trait RecordedTestTrait {
3232
3333 use IsRecordingTrait;
34+ use RecordingsPathTrait;
3435
3536 private ?AssertionRecorderInterface $ recorder = null ;
3637
@@ -145,25 +146,8 @@ protected function createRecorder(): AssertionRecorderInterface {
145146 * Creates a storage for the assertion recorder.
146147 */
147148 protected function createAssertionStore (): AssertionValueStoreInterface {
148- $ reflection_class = new \ReflectionClass (static ::class);
149- $ class_file = $ reflection_class ->getFileName ();
150- if ($ class_file === false ) {
151- throw new \RuntimeException (sprintf ("No class file for '%s'. " , static ::class));
152- }
153- /** @var non-empty-list<string> $parts */
154- $ parts = \explode ('\\' , static ::class);
155- $ parts_count = count ($ parts );
156- if ($ parts_count < 3 ) {
157- throw new \RuntimeException (sprintf ("Namespace of class '%s' is too shallow. " , static ::class));
158- }
159- $ package_dir = dirname ($ class_file , $ parts_count - 2 );
160- $ base = implode ('/ ' , [
161- $ package_dir ,
162- 'recordings ' ,
163- ...array_slice ($ parts , 3 ),
164- ]) . '- ' ;
165149 return new AssertionValueStore_Yaml (
166- $ base ,
150+ $ this -> getClassRecordingsPath () . ' - ' ,
167151 $ this ->buildYamlHeader (...),
168152 );
169153 }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Ock \Testing ;
6+
7+ use Ock \ClassDiscovery \NamespaceDirectory ;
8+
9+ trait RecordingsPathTrait {
10+
11+ /**
12+ * Gets a path to use for recordings for this test class.
13+ *
14+ * If the test class path is 'tests/src/Subdir/MyTest.php', then this method
15+ * would return 'tests/recordings/Subdir/MyTest'.
16+ *
17+ * The calling code can decide whether to use this as a directory, or as a
18+ * prefix, where the last fragment might be part of a file name.
19+ *
20+ * The idea is that anything under 'tests/recordings/' can be deleted and
21+ * regenerated, by running the test in recording mode.
22+ *
23+ * @return string
24+ * Path without ending '/'.
25+ */
26+ protected function getClassRecordingsPath (): string {
27+ $ reflection_class = new \ReflectionClass (static ::class);
28+ $ class_dir = NamespaceDirectory::fromReflectionClass ($ reflection_class );
29+ return $ class_dir ->getPackageDirectory (level: 3 )
30+ . '/recordings '
31+ . $ class_dir ->getRelativePath ('' , 3 )
32+ . '/ ' . $ reflection_class ->getShortName ();
33+ }
34+
35+ }
You can’t perform that action at this time.
0 commit comments