From 15f66e911cfa0f3286899daec4e10b35a49b5711 Mon Sep 17 00:00:00 2001 From: Robert Heine Date: Wed, 22 Jun 2016 20:24:38 +0200 Subject: [PATCH 1/3] phpci --- HelloWorld.php | 1 + README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/HelloWorld.php b/HelloWorld.php index bd42dc80..fb621349 100644 --- a/HelloWorld.php +++ b/HelloWorld.php @@ -17,6 +17,7 @@ public function createDBIFNotExists() { $this->pdo->query($sql); return true; } + public function hello($what = 'World') { diff --git a/README.md b/README.md index f4d26023..7f282c9a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ continuous integration with a PHP project. Here is a sample status icon showing the state of the master branch. + [![Build Status](https://travis-ci.org/Robdebert/travisci-php-example-test.svg?branch=travistest)](https://travis-ci.org/Robdebert/travisci-php-example-test) In order to run this project just fork it on github.com and then [enable](http://about.travis-ci.org/docs/user/getting-started/) From bd337f4b64c1c54b65398148b1a36b0ba0c64cef Mon Sep 17 00:00:00 2001 From: Robert Heine Date: Sat, 25 Jun 2016 20:54:12 +0200 Subject: [PATCH 2/3] Jenkins Test --- index.php | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 00000000..67e239d6 --- /dev/null +++ b/index.php @@ -0,0 +1,2 @@ + Date: Sat, 25 Jun 2016 22:19:43 +0200 Subject: [PATCH 3/3] PHP Unit 5 Tests, erfolgreich --- HelloWorld.php | 31 +++++++++++++-------- Tests/HelloWorldTest.php | 60 ++++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/HelloWorld.php b/HelloWorld.php index fb621349..bce5830a 100644 --- a/HelloWorld.php +++ b/HelloWorld.php @@ -13,24 +13,31 @@ public function __construct(PDO $pdo) } public function createDBIFNotExists() { - $sql = "CREATE TABLE IF NOT EXISTS hello;"; - $this->pdo->query($sql); - return true; + $sql = "CREATE DATABASE IF NOT EXISTS hello;"; + $result = $this->pdo->query($sql); + return (bool) $result; + } + public function createTableIFNotExists() { + $sql = "CREATE TABLE IF NOT EXISTS `world` ( `what` VARCHAR(255) NOT NULL , PRIMARY KEY (`what`)) ENGINE = InnoDB;"; + $result = $this->pdo->prepare($sql)->execute(); + return (bool) $result; } - - public function hello($what = 'World') + public function insert($message) { - $sql = "INSERT INTO hello VALUES (" . $this->pdo->quote($what) . ")"; - $this->pdo->query($sql); - return "Hello $what"; + $sql = "INSERT INTO world VALUES ('{$message}')"; + $sth = $this->pdo->prepare($sql); + $result = $sth->execute(); + return (bool) $result; } - public function what() + public function getAll() { - $sql = "SELECT what FROM hello"; - $stmt = $this->pdo->query($sql); - return $stmt->fetchColumn(); + $sql = "SELECT * FROM world"; + $sth = $this->pdo->prepare($sql); + $sth->execute(); + $result_array = $sth->fetchAll(PDO::FETCH_ASSOC); + return $result_array; } } diff --git a/Tests/HelloWorldTest.php b/Tests/HelloWorldTest.php index eaab7996..a051458c 100644 --- a/Tests/HelloWorldTest.php +++ b/Tests/HelloWorldTest.php @@ -1,47 +1,71 @@ 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)"); + $this->pdo = new PDO("mysql:host=localhost;dbname=hello", "root", "", array( + PDO::ATTR_PERSISTENT => true + )); + + $currentDir = __DIR__; + $baseDir = str_replace("\Tests", "", $currentDir); + $file = $baseDir."\HelloWorld.php"; + + include_once($file); } + public function setup() { + + $this->helloWorld = new HelloWorld($this->pdo); + + } + public function tearDown() { - $this->pdo->query("DROP TABLE hello"); + unset($this->helloWorld); } + - public function testHelloWorld() + public function testDBCreate() { + $result = $this->helloWorld->createDBIFNotExists(); + $this->assertTrue($result, true); + } + + public function testTableCreate() { + $result = $this->helloWorld->createTableIFNotExists(); + $this->assertTrue($result, true); + } + + + + public function testInsert() { - $helloWorld = new HelloWorld($this->pdo); - - $this->assertEquals('Hello World', $helloWorld->hello()); + $result = $this->helloWorld->insert("1. Versuch - ".date("d.m.Y H:i:s")); + $this->assertTrue($result); } + public function testHello() { - $helloWorld = new HelloWorld($this->pdo); - - $this->assertEquals('Hello Bar', $helloWorld->hello('Bar')); + $results = $this->helloWorld->getAll(); + $this->assertTrue(is_array($results), true); // https://phpunit.de/manual/current/en/appendixes.assertions.html } + public function testWhat() { - $helloWorld = new HelloWorld($this->pdo); - - $this->assertFalse($helloWorld->what()); - $helloWorld->hello('Bar'); + $result = $this->helloWorld->insert("2. Versuch - ".date("d.m.Y H:i:s")); + $this->assertTrue($result); - $this->assertEquals('Bar', $helloWorld->what()); + // $this->assertEquals('Bar', $this->helloWorld->getAll()); } }