From c3248ebf531aece37d9f6c0315e8955c92cbd1c2 Mon Sep 17 00:00:00 2001
From: Tom Johnell <tjohnell@gmail.com>
Date: Wed, 22 Jul 2015 14:29:32 -0600
Subject: [PATCH] Added tests for getConfigDir

---
 tests/lib/SimpleSAML/Utils/ConfigTest.php | 49 +++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 tests/lib/SimpleSAML/Utils/ConfigTest.php

diff --git a/tests/lib/SimpleSAML/Utils/ConfigTest.php b/tests/lib/SimpleSAML/Utils/ConfigTest.php
new file mode 100644
index 000000000..556e5d0b0
--- /dev/null
+++ b/tests/lib/SimpleSAML/Utils/ConfigTest.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * Tests for SimpleSAML\Utils\Config
+ */
+class Utils_ConfigTest extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * Test default config dir with not environment variable
+     */
+    public function testDefaultConfigDir()
+    {
+        // clear env var
+        putenv('SIMPLESAMLPHP_CONFIG_DIR');
+        $configDir = \SimpleSAML\Utils\Config::getConfigDir();
+
+        $this->assertEquals($configDir, dirname(dirname(dirname(dirname(__DIR__)))) . '/config');
+    }
+
+    /**
+     * Test valid dir specified by env var overrides default config dir
+     */
+    public function testEnvVariableConfigDir()
+    {
+        putenv('SIMPLESAMLPHP_CONFIG_DIR=' . __DIR__);
+        $configDir = \SimpleSAML\Utils\Config::getConfigDir();
+
+        $this->assertEquals($configDir, __DIR__);
+    }
+
+    /**
+     * Test invalid dir specified by env var results in a thrown exception
+     */
+    public function testInvalidEnvVariableConfigDirThrowsException()
+    {
+        // I used a random hash to ensure this test directory is always invalid
+        $invalidDir = __DIR__ . '/e9826ad19cbc4f5bf20c0913ffcd2ce6';
+        putenv('SIMPLESAMLPHP_CONFIG_DIR=' . $invalidDir);
+
+        $this->setExpectedException(
+            'InvalidArgumentException',
+            'Config directory specified by environment variable SIMPLESAMLPHP_CONFIG_DIR is not a directory.  ' .
+            'Given: "' . $invalidDir . '"'
+        );
+
+        $configDir = \SimpleSAML\Utils\Config::getConfigDir();
+    }
+}
-- 
GitLab