88
99final class PDOFixtureLoader implements FixtureLoader
1010{
11- const EXTENSION_SQL = 'sql ' ;
12-
1311 private $ fileSystem ;
1412 private $ config ;
1513 private $ connection ;
@@ -26,8 +24,8 @@ public function __construct(FileSystem $fileSystem, PDOFixtureConfig $config, PD
2624 */
2725 public function executeBeforeFirstTest (): void
2826 {
29- $ path = $ this ->config ->getParam (PDOFixtureConfig:: BEFORE_FIRST_TEST_PDO_FIXTURES_PATH );
30- $ this ->runFixturesUnderPath ( $ path );
27+ $ params = $ this ->config ->getBeforeFirstTest ( );
28+ $ this ->runFixturesUnderPathWithExtension ( $ params [ ' path ' ], $ params [ ' extension ' ] );
3129 }
3230
3331 /**
@@ -36,18 +34,11 @@ public function executeBeforeFirstTest(): void
3634 */
3735 public function executeBeforeTest (string $ test ): void
3836 {
39- $ stage = PDOFixtureConfig::BEFORE_TEST_PDO_FIXTURES_PATH ;
40- $ path = $ this ->config ->getParam ($ stage );
41- $ this ->runFixturesUnderPath ($ path );
37+ $ params = $ this ->config ->getBeforeTest ();
38+ $ this ->runFixturesUnderPathWithExtension ($ params ['path ' ], $ params ['extension ' ]);
4239 $ className = '\\' . substr ($ test , 0 , strpos ($ test , ': ' ));
4340 $ methodName = 'getBeforeTestFixtureName ' ;
44- if (method_exists ($ className , $ methodName )) {
45- $ fixtureNameFunc = "$ className:: $ methodName " ;
46- $ path = $ this ->config ->getParam ($ stage )
47- . DIRECTORY_SEPARATOR
48- . $ fixtureNameFunc ();
49- $ this ->runFixturesUnderPath ($ path );
50- }
41+ $ this ->runSpecificFixturesFromStatic ($ className , $ methodName , $ params );
5142 }
5243
5344 /**
@@ -57,38 +48,34 @@ public function executeBeforeTest(string $test): void
5748 */
5849 public function executeAfterTest (string $ test , float $ time ): void
5950 {
60- $ stage = PDOFixtureConfig::AFTER_TEST_PDO_FIXTURES_PATH ;
61- $ path = $ this ->config ->getParam ($ stage );
62- $ this ->runFixturesUnderPath ($ path );
51+ $ params = $ this ->config ->getAfterTest ();
52+ $ this ->runFixturesUnderPathWithExtension ($ params ['path ' ], $ params ['extension ' ]);
6353 $ className = '\\' . substr ($ test , 0 , strpos ($ test , ': ' ));
6454 $ methodName = 'getAfterTestFixtureName ' ;
65- if (method_exists ($ className , $ methodName )) {
66- $ fixtureNameFunc = "$ className:: $ methodName " ;
67- $ path = $ this ->config ->getParam ($ stage ) . DIRECTORY_SEPARATOR . $ fixtureNameFunc ();
68- $ this ->runFixturesUnderPath ($ path );
69- }
55+ $ this ->runSpecificFixturesFromStatic ($ className , $ methodName , $ params );
7056 }
7157
7258 /**
7359 * @throws TestingException
7460 */
7561 public function executeAfterLastTest (): void
7662 {
77- $ path = $ this ->config ->getParam (PDOFixtureConfig:: AFTER_LAST_TEST_PDO_FIXTURES_PATH );
78- $ this ->runFixturesUnderPath ( $ path );
63+ $ params = $ this ->config ->getAfterLastTest ( );
64+ $ this ->runFixturesUnderPathWithExtension ( $ params [ ' path ' ], $ params [ ' extension ' ] );
7965 }
8066
8167 /**
8268 * @param string $path
69+ * @param string $extension
8370 * @throws TestingException
8471 */
85- public function runFixturesUnderPath (string $ path ): void
72+ public function runFixturesUnderPathWithExtension (string $ path, string $ extension ): void
8673 {
8774 try {
8875 $ this ->connection ->PDO ()->beginTransaction ();
8976 $ iterator = $ this ->fileSystem ->getFileListIteratorFromPathByExtension (
9077 $ path ,
91- self :: EXTENSION_SQL
78+ $ extension
9279 );
9380 $ this ->fileSystem ->runCallbackOnEachFileIteratorContents (
9481 $ iterator ,
@@ -102,4 +89,21 @@ function (string $contents) {
10289 throw $ exception ;
10390 }
10491 }
92+
93+ /**
94+ * @param string $className
95+ * @param string $methodName
96+ * @param array $params
97+ * @throws TestingException
98+ */
99+ public function runSpecificFixturesFromStatic (string $ className , string $ methodName , array $ params ): void
100+ {
101+ if (method_exists ($ className , $ methodName )) {
102+ $ fixtureNameFunc = "$ className:: $ methodName " ;
103+ $ this ->runFixturesUnderPathWithExtension (
104+ $ params ['path ' ] . DIRECTORY_SEPARATOR . $ fixtureNameFunc (),
105+ $ params ['extension ' ]
106+ );
107+ }
108+ }
105109}
0 commit comments