From ad1184b203b31e61c8b93e4dff3ad7f2653e9bc9 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Fri, 22 Mar 2019 10:29:40 +0100
Subject: [PATCH] Use Appveyor for CI on Windows (#1083)

* Create .appveyor.yml

* Fix tests for Windows platform

* Fix builtin webserver for Windows

* Properly kill process on Windows
---
 .appveyor.yml                              | 29 ++++++++++++++++++
 tests/BuiltInServer.php                    | 34 +++++++++++++++++-----
 tests/lib/SimpleSAML/ConfigurationTest.php | 10 +++----
 3 files changed, 60 insertions(+), 13 deletions(-)
 create mode 100644 .appveyor.yml

diff --git a/.appveyor.yml b/.appveyor.yml
new file mode 100644
index 000000000..f85c7496d
--- /dev/null
+++ b/.appveyor.yml
@@ -0,0 +1,29 @@
+build: false
+shallow_clone: false
+version: '1.17.1.{build}'
+platform: 'x64'
+clone_folder: C:\projects\simplesamlphp
+
+environment:
+  matrix:
+    - PHP_VERSION: "5.6"
+    - PHP_VERSION: "7.0"
+    - PHP_VERSION: "7.1"
+    - PHP_VERSION: "7.2"
+    - PHP_VERSION: "7.3"
+
+install:
+  - ps: Invoke-WebRequest "https://raw.githubusercontent.com/ChadSikorra/ps-install-php/master/Install-PHP.ps1" -OutFile "Install-PHP.ps1"
+  - ps: .\Install-PHP.ps1 -Version $Env:PHP_VERSION -Highest -Arch x64 -Extensions mbstring,intl,openssl,curl,pdo_mysql,pdo_sqlite
+  - refreshenv
+  - cd C:\projects\simplesamlphp
+  - php -r "readfile('https://getcomposer.org/installer');" | php
+
+before_test:
+  - cd C:\projects\simplesamlphp
+  - php composer.phar install --no-interaction --no-progress --optimize-autoloader --prefer-source --no-ansi
+
+test_script:
+  - cd C:\projects\simplesamlphp
+  - vendor\bin\phpunit.bat
+  
diff --git a/tests/BuiltInServer.php b/tests/BuiltInServer.php
index 0695e8c7e..c12fc6dc2 100644
--- a/tests/BuiltInServer.php
+++ b/tests/BuiltInServer.php
@@ -9,6 +9,8 @@
 
 namespace SimpleSAML\Test;
 
+use SimpleSAML\Utils\System;
+
 class BuiltInServer
 {
 
@@ -60,6 +62,10 @@ class BuiltInServer
         } else {
             $this->docroot = dirname(dirname(__FILE__)).'/www/';
         }
+
+        // Rationalize docroot
+        $this->docroot = str_replace('\\', '/', $this->docroot);
+        $this->docroot = rtrim($this->docroot, '/');
     }
 
 
@@ -80,17 +86,26 @@ class BuiltInServer
         $port = mt_rand(1025, 65535);
         $this->address = 'localhost:'.$port;
 
-        $command = sprintf(
-            'php -S %s -t %s %s >> /dev/null 2>&1 & echo $!',
-            $this->address,
-            $this->docroot,
-            $this->router
-        );
+        if (System::getOS() === System::WINDOWS) {
+            $command = sprintf(
+                'powershell $proc = start-process php -ArgumentList (\'-S %s\', \'-t %s\', \'%s\') -Passthru; Write-output $proc.Id;',
+                $this->address,
+                $this->docroot,
+                $this->router
+            );
+        } else {
+            $command = sprintf(
+                'php -S %s -t %s %s >> /dev/null 2>&1 & echo $!',
+                $this->address,
+                $this->docroot,
+                $this->router
+            );
+        }
 
         // execute the command and store the process ID
         $output = [];
         exec($command, $output);
-        $this->pid = (int) $output[0];
+        $this->pid = intval($output[0]);
 
         // wait until it's listening for connections to avoid race conditions
         $start = microtime(true);
@@ -117,8 +132,11 @@ class BuiltInServer
     {
         if ($this->pid === 0) {
             return;
+        } else if (System::getOS() === System::WINDOWS) {
+            exec('taskkill /PID '.$this->pid);
+        } else {
+            exec('kill '.$this->pid);
         }
-        exec('kill '.$this->pid);
         $this->pid = 0;
     }
 
diff --git a/tests/lib/SimpleSAML/ConfigurationTest.php b/tests/lib/SimpleSAML/ConfigurationTest.php
index 474495e84..18a21fe05 100644
--- a/tests/lib/SimpleSAML/ConfigurationTest.php
+++ b/tests/lib/SimpleSAML/ConfigurationTest.php
@@ -210,17 +210,17 @@ class ConfigurationTest extends \SimpleSAML\Test\Utils\ClearStateTestCase
     public function testGetBaseDir()
     {
         $c = Configuration::loadFromArray([]);
-        $this->assertEquals($c->getBaseDir(), dirname(dirname(dirname(dirname(__FILE__)))).'/');
+        $this->assertEquals($c->getBaseDir(), dirname(dirname(dirname(dirname(__FILE__)))).DIRECTORY_SEPARATOR);
 
         $c = Configuration::loadFromArray([
-            'basedir' => '/basedir',
+            'basedir' => DIRECTORY_SEPARATOR.'basedir',
         ]);
-        $this->assertEquals($c->getBaseDir(), '/basedir/');
+        $this->assertEquals($c->getBaseDir(), DIRECTORY_SEPARATOR.'basedir'.DIRECTORY_SEPARATOR);
 
         $c = Configuration::loadFromArray([
-            'basedir' => '/basedir/',
+            'basedir' => DIRECTORY_SEPARATOR.'basedir'.DIRECTORY_SEPARATOR,
         ]);
-        $this->assertEquals($c->getBaseDir(), '/basedir/');
+        $this->assertEquals($c->getBaseDir(), DIRECTORY_SEPARATOR.'basedir'.DIRECTORY_SEPARATOR);
     }
 
     /**
-- 
GitLab