2020require_once (__DIR__ . '/behat_app_helper.php ' );
2121
2222use Behat \Behat \Hook \Scope \ScenarioScope ;
23+ use Behat \Behat \Hook \Scope \AfterStepScope ;
2324use Behat \Gherkin \Node \TableNode ;
2425use Behat \Mink \Exception \DriverException ;
2526use Behat \Mink \Exception \ExpectationException ;
@@ -44,7 +45,10 @@ class behat_app extends behat_app_helper {
4445 ],
4546 ];
4647
47- protected $ featurepath = '' ;
48+ protected $ featurepath ;
49+ protected $ coveragepath ;
50+ protected $ scenarioslug ;
51+ protected $ scenariolaststep ;
4852
4953 /**
5054 * @BeforeScenario
@@ -56,7 +60,35 @@ public function before_scenario(ScenarioScope $scope) {
5660 return ;
5761 }
5862
63+ $ steps = $ scope ->getScenario ()->getSteps ();
64+
65+ $ this ->scenarioslug = $ this ->get_scenario_slug ($ scope );
66+ $ this ->scenariolaststep = $ steps [count ($ steps ) - 1 ];
5967 $ this ->featurepath = dirname ($ feature ->getFile ());
68+ $ this ->coveragepath = get_config ('local_moodleappbehat ' , 'coverage_path ' ) ?: ($ this ->featurepath . DIRECTORY_SEPARATOR . 'coverage ' . DIRECTORY_SEPARATOR );
69+ }
70+
71+ /**
72+ * @AfterStep
73+ */
74+ public function after_step (AfterStepScope $ scope ) {
75+ $ step = $ scope ->getStep ();
76+
77+ if ($ step !== $ this ->scenariolaststep || empty ($ this ->coveragepath )) {
78+ return ;
79+ }
80+
81+ if (!is_dir ($ this ->coveragepath )) {
82+ if (!@mkdir ($ this ->coveragepath , 0777 , true )) {
83+ throw new Exception ("Cannot create {$ this ->coveragepath } directory. " );
84+ }
85+ }
86+
87+ $ coverage = $ this ->runtime_js ('getCoverage() ' );
88+
89+ if (!is_null ($ coverage )) {
90+ file_put_contents ($ this ->coveragepath . $ this ->scenarioslug . '.json ' , $ coverage );
91+ }
6092 }
6193
6294 /**
0 commit comments