Api.php
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Codeception\Template;
use Codeception\InitTemplate;
use Codeception\Util\Template;
use Symfony\Component\Yaml\Yaml;
class Api extends InitTemplate
{
protected $configTemplate = <<<EOF
# suite config
suites:
api:
actor: ApiTester
path: .
modules:
enabled:
- REST:
url: {{url}}
depends: PhpBrowser
paths:
tests: {{baseDir}}
output: {{baseDir}}/_output
data: {{baseDir}}/_data
support: {{baseDir}}/_support
settings:
shuffle: false
lint: true
EOF;
protected $firstTest = <<<EOF
<?php
class ApiCest
{
public function tryApi(ApiTester \$I)
{
\$I->sendGET('/');
\$I->seeResponseCodeIs(200);
\$I->seeResponseIsJson();
}
}
EOF;
public function setup()
{
$this->checkInstalled();
$this->say("Let's prepare Codeception for REST API testing");
$this->say("");
$dir = $this->ask("Where tests will be stored?", 'tests');
$url = $this->ask("Start url for tests", "http://localhost/api");
$this->createEmptyDirectory($outputDir = $dir . DIRECTORY_SEPARATOR . '_output');
$this->createEmptyDirectory($dir . DIRECTORY_SEPARATOR . '_data');
$this->createDirectoryFor($supportDir = $dir . DIRECTORY_SEPARATOR . '_support');
$this->gitIgnore($outputDir);
$this->gitIgnore($supportDir . DIRECTORY_SEPARATOR . '_generated');
$this->sayInfo("Created test directories inside at $dir");
$configFile = (new Template($this->configTemplate))
->place('url', $url)
->place('baseDir', $dir)
->produce();
if ($this->namespace) {
$namespace = rtrim($this->namespace, '\\');
$configFile = "namespace: $namespace\n" . $configFile;
}
$this->createFile('codeception.yml', $configFile);
$this->createHelper('Api', $supportDir);
$this->createActor('ApiTester', $supportDir, Yaml::parse($configFile)['suites']['api']);
$this->sayInfo("Created global config codeception.yml inside the root directory");
$this->createFile($dir . DIRECTORY_SEPARATOR . 'ApiCest.php', $this->firstTest);
$this->sayInfo("Created a demo test ApiCest.php");
$this->say();
$this->saySuccess("INSTALLATION COMPLETE");
$this->say();
$this->say("<bold>Next steps:</bold>");
$this->say("1. Edit <bold>$dir/ApiCest.php</bold> to write first API tests");
$this->say("2. Run tests using: <comment>codecept run</comment>");
$this->say();
$this->say("<bold>Happy testing!</bold>");
}
}