Commit 56ba8b57 authored by tibo's avatar tibo

Test job avec phpunit

parent cf6d0715
Pipeline #174135 failed with stage
in 1 minute and 56 seconds
test:
script:
- echo "Tests en cours"
# Select image from https://hub.docker.com/_/php/
image: php:5.6
# Select what we should cache
cache:
paths:
- vendor/
before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq
# Install mysql driver
- docker-php-ext-install pdo_mysql
# Install composer
- curl -sS https://getcomposer.org/installer | php
# Install all project dependencies
- php composer.phar install
services:
- mysql
variables:
# Configure mysql service (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: hello_world_test
MYSQL_ROOT_PASSWORD: mysql
build:
# We test PHP5.6 (the default) with MySQL
test:mysql:
script:
- echo "Build en cours"
- vendor/bin/phpunit --configuration phpunit_mysql.xml --coverage-text
deploy:
# We test PHP7 with MySQL, but we allow it to fail
test:php7:mysql:
image: php:7
script:
- echo "Déploiement de l'application"
- vendor/bin/phpunit --configuration phpunit_mysql.xml --coverage-text
allow_failure: true
<?php
class HelloWorld
{
/**
* @var PDO
*/
private $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function hello($what = 'World')
{
$sql = "INSERT INTO hello VALUES (" . $this->pdo->quote($what) . ")";
$this->pdo->query($sql);
return "Hello $what";
}
public function what()
{
$sql = "SELECT what FROM hello";
$stmt = $this->pdo->query($sql);
return $stmt->fetchColumn();
}
}
Example PHP project
===================
The purpose of this repository is to show how to use GitLab to do
Continuous Integration with a PHP project. It serves as a companion project for
<https://docs.gitlab.com/ce/ci/examples/php.html>.
In order to run this project just fork it on GitLab.com.
Every push will then trigger a new build on GitLab.
Source
------
This project was taken from: https://github.com/travis-ci-examples/php.
\ No newline at end of file
<?php
class HelloWorldTest extends PHPUnit_Framework_TestCase
{
/**
* @var PDO
*/
private $pdo;
public function setUp()
{
$this->pdo = new PDO($GLOBALS['db_dsn'], $GLOBALS['db_username'], $GLOBALS['db_password']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->query("CREATE TABLE hello (what VARCHAR(50) NOT NULL)");
}
public function tearDown()
{
if (!$this->pdo)
return;
$this->pdo->query("DROP TABLE hello");
}
public function testHelloWorld()
{
$helloWorld = new HelloWorld($this->pdo);
$this->assertEquals('Hello World', $helloWorld->hello());
}
public function testHello()
{
$helloWorld = new HelloWorld($this->pdo);
$this->assertEquals('Hello Bar', $helloWorld->hello('Bar'));
}
public function testWhat()
{
$helloWorld = new HelloWorld($this->pdo);
$this->assertFalse($helloWorld->what());
$helloWorld->hello('Bar');
$this->assertEquals('Bar', $helloWorld->what());
}
}
<?php
function loader($class)
{
$file = $class . '.php';
if (file_exists($file)) {
require $file;
}
}
spl_autoload_register('loader');
{
"require-dev": {
"phpunit/phpunit": "5.0.*"
}
}
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Tests/bootstrap.php" colors="true">
<php>
<var name="db_dsn" value="mysql:dbname=hello_world_test;host=mysql"/>
<var name="db_username" value="root"/>
<var name="db_password" value="mysql"/>
</php>
<testsuites>
<testsuite name="Hello World Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Tests/bootstrap.php" colors="true">
<php>
<var name="db_dsn" value="pgsql:dbname=hello_world_test;host=postgres"/>
<var name="db_username" value="postgres"/>
<var name="db_password" value="postgres"/>
</php>
<testsuites>
<testsuite name="Hello World Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment