Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Add the first integration test #62

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
composer.lock
phpunit.xml
/clover.xml
/vendor
/wordpress
vendor
composer.lock
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ php:
sudo: false

install:
- travis_retry composer install --no-interaction
# Install test WordPress instance dependencies
- travis_retry composer install --working-dir=tests --no-interaction

script:
- vendor/bin/phpunit --coverage-clover clover.xml
- tests/vendor/bin/phpunit

after_script:
- bash <(curl -s https://codecov.io/bash)
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fixes:
- "tests/vendor/wordplate/framework/src/::src/"
14 changes: 3 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
"symfony/var-dumper": "^4.0",
"vlucas/phpdotenv": "^2.4"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"WordPlate\\": "src/"
Expand All @@ -38,14 +35,6 @@
"src/helpers.php"
]
},
"autoload-dev": {
"files": [
"tests/helpers.php"
],
"psr-4": {
"WordPlate\\Tests\\": "tests/"
}
},
"config": {
"preferred-install": "dist"
},
Expand All @@ -54,6 +43,9 @@
"dev-master": "6.2-dev"
}
},
"scripts": {
"test": "@composer --working-dir=tests test"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this with PHPUnit in the .travis.yml file?

},
"minimum-stability": "dev",
"prefer-stable": true
}
10 changes: 7 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -17,15 +17,19 @@
>
<testsuites>
<testsuite name="WordPlate Test Suite">
<directory suffix="Test.php">./tests</directory>
<directory suffix="Test.php">.</directory>
<exclude>tests/vendor</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<directory suffix=".php">tests/vendor/wordplate/framework/src</directory>
</whitelist>
</filter>
<php>
<env name="WP_ENV" value="testing"/>
</php>
<logging>
<log type="coverage-clover" target="clover.xml"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to move this here? I would prefer if this was added to the .travis.yml file instead.

</logging>
</phpunit>
15 changes: 15 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
clover.xml

/vendor

# This is a workaround due to inability to set up a repository of type "path"
# targeting ../ directory in order to conduct integration tests with the package
# in its current state (before pushing to external repository).
# See https://github.com/composer/composer/issues/6085
/package

# These paths are specified in composer.json and used by composer/installers.
/public/wordpress
/public/mu-plugins
/public/plugins
/public/themes
2 changes: 2 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use PHPUnit\Framework\TestCase;
use WordPlate\Application;

require_once __DIR__.'/stubs.php';

/**
* This is the application test class.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use WordPlate\Application;
use WordPlate\Container;

require_once __DIR__.'/stubs.php';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe we can autoload this file instead of using require?


/**
* This is the container test class.
*
Expand Down
81 changes: 81 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of WordPlate.
*
* (c) Vincent Klaiber <hello@vinkla.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace WordPlate\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

/**
* This is the integration test class. It spins up WordPress using built-in
* server.
*
* @author Mikhail Vasin <michaelvasin@gmail.com>
*/
class IntegrationTest extends TestCase
{
use RetryTrait;

// Holds web server process, so it will be stopped when the testing is
// over.
private static $server;

// TODO: iterate ports to find a free one, there is
// no guarantee that a particular port will be available on a test
// machine
const HOST = 'localhost:12345';
const DOCROOT = __DIR__.'/public';

public static function setUpBeforeClass()
{
self::$server = new Process(
'php -S '.self::HOST.' -t '.self::DOCROOT
);

// Start web server in a separate process
self::$server->start();
}

/**
* @retry 200
* @sleep 0.01
*/
public function testSmokeTest()
{
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, self::HOST);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// Without a proper database connection it should currently look
// something like this
$this->assertRegExp(
'/Error establishing a database connection/', $output
);

// close curl resource to free up system resources
curl_close($ch);
}

public static function tearDownAfterClass()
{
self::$server->stop();
}
}
112 changes: 112 additions & 0 deletions tests/RetryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace WordPlate\Tests;

// Adapted from https://github.com/php-enqueue/enqueue-dev/blob/master/pkg/test/RetryTrait.php
// (fixed typos, added sleep time)
trait RetryTrait
{
public function runBare(): void
{
$e = null;
$numberOfRetries = $this->getNumberOfRetries();
$sleepTime = $this->getSleepTime();

for ($i = 0; $i < $numberOfRetries; $i++) {
try {
parent::runBare();

return;
} catch (\PHPUnit_Framework_IncompleteTestError $e) {
throw $e;
} catch (\PHPUnit_Framework_SkippedTestError $e) {
throw $e;
} catch (\Throwable $e) {
// last one thrown below
} catch (\Exception $e) {
// last one thrown below
}

usleep($sleepTime * 1000000);
}
if ($e) {
throw $e;
}
}

/**
* @return int
*/
private function getNumberOfRetries()
{
$numberOfRetries = 1;

if (isset($annotations['class']['retry'][0])) {
$numberOfRetries = $annotations['class']['retry'][0];
}

$annotations = $this->getAnnotations();

if (isset($annotations['method']['retry'][0])) {
$numberOfRetries = $annotations['method']['retry'][0];
}

if (false == is_numeric($numberOfRetries)) {
throw new \LogicException(
sprintf('The number of retries must be a number but got "%s"',
var_export($numberOfRetries, true))
);
}

$numberOfRetries = (int) $numberOfRetries;

if ($numberOfRetries <= 0) {
throw new \LogicException(
sprintf(
'The number of retries must be > 0 but got "%s".',
$numberOfRetries
)
);
}

return $numberOfRetries;
}

/**
* @return float
*/
private function getSleepTime()
{
$sleepTime = 0;

$annotations = $this->getAnnotations();

if (isset($annotations['class']['sleep'][0])) {
$sleepTime = $annotations['class']['sleep'][0];
}

if (isset($annotations['method']['sleep'][0])) {
$sleepTime = $annotations['method']['sleep'][0];
}

if (false == is_numeric($sleepTime)) {
throw new \LogicException(
sprintf('The sleep time must be a number but got "%s"',
var_export($sleepTime, true)
)
);
}

$sleepTime = (float) $sleepTime;

if ($sleepTime < 0) {
throw new \LogicException(
sprintf(
'The sleep time must be a >= 0 but got "%s".', $sleepTime
)
);
}

return $sleepTime;
}
}
4 changes: 3 additions & 1 deletion tests/HelpersTest.php → tests/StubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use PHPUnit\Framework\TestCase;
use WordPlate\Application;

require_once __DIR__.'/stubs.php';

/**
* This is the helpers test class.
*
* @author Vincent Klaiber <hello@vinkla.com>
*/
class HelpersTest extends TestCase
class StubsTest extends TestCase
{
public function testAsset()
{
Expand Down
43 changes: 43 additions & 0 deletions tests/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"description": "A WordPress site to test WordPlate with",
"type": "project",
"repositories": [
{
"type": "artifact",
"url": "package"
}
],
"require": {
"wordplate/framework": "@dev"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"symfony/process": "^4.0",
"psy/psysh": "^0.8",
"lib-curl": "*"
},
"autoload-dev": {
"psr-4": {
"WordPlate\\Tests\\": "."
}
},
"config": {
"preferred-install": "dist"
},
"scripts": {
"archive-package": "@composer archive --working-dir=../ --ignore-filters --dir=tests/package --file=wordplate-framework",
"test": "vendor/bin/phpunit",
"pre-install-cmd": "@archive-package",
"pre-update-cmd": "@archive-package"
},
"extra": {
"wordpress-install-dir": "public/wordpress",
"installer-paths": {
"public/mu-plugins/{$name}": ["type:wordpress-muplugin"],
"public/plugins/{$name}": ["type:wordpress-plugin"],
"public/themes/{$name}": ["type:wordpress-theme"]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Loading