This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,126 @@
<?php
namespace Codeception\Template;
use Codeception\InitTemplate;
use Codeception\Util\Template;
use Symfony\Component\Yaml\Yaml;
class Acceptance extends InitTemplate
{
protected $configTemplate = <<<EOF
# suite config
suites:
acceptance:
actor: AcceptanceTester
path: .
modules:
enabled:
- WebDriver:
url: {{url}}
browser: {{browser}}
- \Helper\Acceptance
extensions:
enabled: [Codeception\Extension\RunFailed]
params:
- env
gherkin: []
# additional paths
paths:
tests: {{baseDir}}
output: {{baseDir}}/_output
data: {{baseDir}}/_data
support: {{baseDir}}/_support
envs: {{baseDir}}/_envs
settings:
shuffle: false
lint: true
EOF;
protected $firstTest = <<<EOF
<?php
class LoginCest
{
public function _before(AcceptanceTester \$I)
{
\$I->amOnPage('/');
}
public function loginSuccessfully(AcceptanceTester \$I)
{
// write a positive login test
}
public function loginWithInvalidPassword(AcceptanceTester \$I)
{
// write a negative login test
}
}
EOF;
public function setup()
{
$this->checkInstalled();
$this->say("Let's prepare Codeception for acceptance testing");
$this->say("Create your tests and run them in real browser");
$this->say("");
$dir = $this->ask("Where tests will be stored?", 'tests');
$browser = $this->ask("Select a browser for testing", ['chrome', 'phantomjs', 'firefox']);
if ($browser === 'phantomjs') {
$this->sayInfo("Ensure that you have Phantomjs running before starting tests");
}
if ($browser === 'chrome') {
$this->sayInfo("Ensure that you have Selenium Server and ChromeDriver installed before running tests");
}
if ($browser === 'firefox') {
$this->sayInfo("Ensure that you have Selenium Server and GeckoDriver installed before running tests");
}
$url = $this->ask("Start url for tests", "http://localhost");
$this->createEmptyDirectory($outputDir = $dir . DIRECTORY_SEPARATOR . '_output');
$this->createEmptyDirectory($dir . DIRECTORY_SEPARATOR . '_data');
$this->createDirectoryFor($supportDir = $dir . DIRECTORY_SEPARATOR . '_support');
$this->createDirectoryFor($supportDir . DIRECTORY_SEPARATOR . '_generated');
$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('browser', $browser)
->place('baseDir', $dir)
->produce();
if ($this->namespace) {
$namespace = rtrim($this->namespace, '\\');
$configFile = "namespace: $namespace\n" . $configFile;
}
$this->createFile('codeception.yml', $configFile);
$this->createHelper('Acceptance', $supportDir);
$this->createActor('AcceptanceTester', $supportDir, Yaml::parse($configFile)['suites']['acceptance']);
$this->sayInfo("Created global config codeception.yml inside the root directory");
$this->createFile($dir . DIRECTORY_SEPARATOR . 'LoginCest.php', $this->firstTest);
$this->sayInfo("Created a demo test LoginCest.php");
$this->say();
$this->saySuccess("INSTALLATION COMPLETE");
$this->say();
$this->say("<bold>Next steps:</bold>");
$this->say('1. Launch Selenium Server or PhantomJS and webserver');
$this->say("2. Edit <bold>$dir/LoginCest.php</bold> to test login of your application");
$this->say("3. Run tests using: <comment>codecept run</comment>");
$this->say();
$this->say("<bold>Happy testing!</bold>");
}
}

View File

@@ -0,0 +1,94 @@
<?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->createDirectoryFor($supportDir . DIRECTORY_SEPARATOR . '_generated');
$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>");
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Codeception\Template;
use Codeception\InitTemplate;
use Symfony\Component\Yaml\Yaml;
class Bootstrap extends InitTemplate
{
// defaults
protected $supportDir = 'tests/_support';
protected $outputDir = 'tests/_output';
protected $dataDir = 'tests/_data';
protected $envsDir = 'tests/_envs';
public function setup()
{
$this->checkInstalled($this->workDir);
$input = $this->input;
if ($input->getOption('namespace')) {
$this->namespace = trim($input->getOption('namespace'), '\\') . '\\';
}
if ($input->hasOption('actor') && $input->getOption('actor')) {
$this->actorSuffix = $input->getOption('actor');
}
$this->say(
"<fg=white;bg=magenta> Bootstrapping Codeception </fg=white;bg=magenta>\n"
);
$this->createGlobalConfig();
$this->say("File codeception.yml created <- global configuration");
$this->createDirs();
if ($input->hasOption('empty') && $input->getOption('empty')) {
return;
}
$this->createUnitSuite();
$this->say("tests/unit created <- unit tests");
$this->say("tests/unit.suite.yml written <- unit tests suite configuration");
$this->createFunctionalSuite();
$this->say("tests/functional created <- functional tests");
$this->say("tests/functional.suite.yml written <- functional tests suite configuration");
$this->createAcceptanceSuite();
$this->say("tests/acceptance created <- acceptance tests");
$this->say("tests/acceptance.suite.yml written <- acceptance tests suite configuration");
$this->say(" --- ");
$this->say();
$this->saySuccess('Codeception is installed for acceptance, functional, and unit testing');
$this->say();
$this->say("<bold>Next steps:</bold>");
$this->say('1. Edit <bold>tests/acceptance.suite.yml</bold> to set url of your application. Change PhpBrowser to WebDriver to enable browser testing');
$this->say("2. Edit <bold>tests/functional.suite.yml</bold> to enable a framework module. Remove this file if you don't use a framework");
$this->say("3. Create your first acceptance tests using <comment>codecept g:cest acceptance First</comment>");
$this->say("4. Write first test in <bold>tests/acceptance/FirstCest.php</bold>");
$this->say("5. Run tests using: <comment>codecept run</comment>");
}
protected function createDirs()
{
$this->createDirectoryFor('tests');
$this->createEmptyDirectory($this->outputDir);
$this->createEmptyDirectory($this->dataDir);
$this->createDirectoryFor($this->supportDir . DIRECTORY_SEPARATOR . '_generated');
$this->createDirectoryFor($this->supportDir . DIRECTORY_SEPARATOR . "Helper");
$this->gitIgnore('tests/_output');
$this->gitIgnore('tests/_support/_generated');
}
protected function createFunctionalSuite($actor = 'Functional')
{
$suiteConfig = <<<EOF
# Codeception Test Suite Configuration
#
# Suite for functional tests
# Emulate web requests and make application process them
# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it
# Remove this suite if you don't use frameworks
actor: $actor{$this->actorSuffix}
modules:
enabled:
# add a framework module here
- \\{$this->namespace}Helper\Functional
EOF;
$this->createSuite('functional', $actor, $suiteConfig);
}
protected function createAcceptanceSuite($actor = 'Acceptance')
{
$suiteConfig = <<<EOF
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
actor: $actor{$this->actorSuffix}
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
- \\{$this->namespace}Helper\Acceptance
EOF;
$this->createSuite('acceptance', $actor, $suiteConfig);
}
protected function createUnitSuite($actor = 'Unit')
{
$suiteConfig = <<<EOF
# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.
actor: $actor{$this->actorSuffix}
modules:
enabled:
- Asserts
- \\{$this->namespace}Helper\Unit
EOF;
$this->createSuite('unit', $actor, $suiteConfig);
}
public function createGlobalConfig()
{
$basicConfig = [
'paths' => [
'tests' => 'tests',
'output' => $this->outputDir,
'data' => $this->dataDir,
'support' => $this->supportDir,
'envs' => $this->envsDir,
],
'actor_suffix' => 'Tester',
'extensions' => [
'enabled' => ['Codeception\Extension\RunFailed']
]
];
$str = Yaml::dump($basicConfig, 4);
if ($this->namespace) {
$namespace = rtrim($this->namespace, '\\');
$str = "namespace: $namespace\n" . $str;
}
$this->createFile('codeception.yml', $str);
}
protected function createSuite($suite, $actor, $config)
{
$this->createDirectoryFor("tests/$suite", "$suite.suite.yml");
$this->createHelper($actor, $this->supportDir);
$this->createActor($actor . $this->actorSuffix, $this->supportDir, Yaml::parse($config));
$this->createFile('tests' . DIRECTORY_SEPARATOR . "$suite.suite.yml", $config);
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace Codeception\Template;
use Codeception\InitTemplate;
use Codeception\Util\Template;
use Symfony\Component\Yaml\Yaml;
class Unit extends InitTemplate
{
protected $configTemplate = <<<EOF
suites:
unit:
path: .
{{tester}}
settings:
shuffle: true
lint: true
paths:
tests: {{dir}}
output: {{dir}}/_output
support: {{dir}}/_support
data: {{dir}}
EOF;
protected $testerAndModules = <<<EOF
actor: UnitTester
modules:
enabled:
# add more modules here
- Asserts
EOF;
public function setup()
{
$this->sayInfo('This will install Codeception for unit testing only');
$this->say();
$dir = $this->ask("Where tests will be stored?", 'tests');
if (!$this->namespace) {
$this->namespace = $this->ask("Enter a default namespace for tests (or skip this step)");
}
$this->say();
$this->say("Codeception provides additional features for integration tests");
$this->say("Like accessing frameworks, ORM, Database.");
$haveTester = $this->ask("Do you wish to enable them?", false);
$this->createEmptyDirectory($outputDir = $dir . DIRECTORY_SEPARATOR . '_output');
$this->createEmptyDirectory($supportDir = $dir . DIRECTORY_SEPARATOR . '_support');
$configFile = (new Template($this->configTemplate))
->place('dir', $dir)
->place('tester', $haveTester ? $this->testerAndModules : '')
->produce();
if ($this->namespace) {
$namespace = rtrim($this->namespace, '\\');
$configFile = "namespace: $namespace\n" . $configFile;
}
$this->createFile('codeception.yml', $configFile);
if ($haveTester) {
$this->createHelper('Unit', $supportDir);
$this->createActor('UnitTester', $supportDir, Yaml::parse($configFile)['suites']['unit']);
}
$this->gitIgnore($outputDir);
$this->sayInfo("Created test directory inside at $dir");
$this->say();
$this->saySuccess("INSTALLATION COMPLETE");
$this->say();
$this->say('Unit tests will be executed in random order');
$this->say('Use @depends annotation to change the order of tests');
if ($haveTester) {
$this->say('To access DI, ORM, Database enable corresponding modules in codeception.yml');
$this->say('Use <bold>$this->tester</bold> object inside Codeception\Test\Unit to call their methods');
$this->say("For example: \$this->tester->seeInDatabase('users', ['name' => 'davert'])");
}
$this->say();
$this->say("<bold>Next steps:</bold>");
$this->say("Create the first test using <comment>codecept g:test unit MyTest</comment>");
$this->say("Run tests with <comment>codecept run</comment>");
$this->say("<bold>Happy testing!</bold>");
}
}