Skip to content

Commit 14e638e

Browse files
committed
added AsJson step decorator
1 parent 29aa927 commit 14e638e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Codeception/Step/AsJson.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Codeception\Step;
4+
5+
use Codeception\Lib\ModuleContainer;
6+
use Codeception\Util\Template;
7+
8+
class AsJson extends Action implements GeneratedStep
9+
{
10+
public function run(ModuleContainer $container = null)
11+
{
12+
$container->getModule('REST')->haveHttpHeader('Content-Type', 'application/json');
13+
$resp = parent::run($container);
14+
return json_decode($resp, true);
15+
}
16+
17+
public static function getTemplate(Template $template)
18+
{
19+
$action = $template->getVar('action');
20+
21+
// should only be applied to send* methods
22+
if (strpos($action, 'send') !== 0) return;
23+
24+
$conditionalDoc = "* JSON response will be automatically decoded \n " . $template->getVar('doc');
25+
26+
return $template
27+
->place('doc', $conditionalDoc)
28+
->place('action', $action . 'AsJson')
29+
->place('step', 'AsJson');
30+
}
31+
}

0 commit comments

Comments
 (0)