Browse Source

Add zend framework 1 functional unit tests

Gerard Roche 10 years ago
parent
commit
0e8d1f18a1

+ 1 - 0
frameworks/PHP/php-zend-framework1/.gitignore

@@ -6,3 +6,4 @@
 .idea
 composer.lock
 cache.properties
+phpunit.xml

+ 34 - 0
frameworks/PHP/php-zend-framework1/phpunit.xml.dist

@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit backupGlobals="false"
+         backupStaticAttributes="false"
+         beStrictAboutOutputDuringTests="false"
+         beStrictAboutTestsThatDoNotTestAnything="true"
+         beStrictAboutTestSize="true"
+         beStrictAboutTodoAnnotatedTests="true"
+         bootstrap="test/bootstrap.php"
+         checkForUnintentionallyCoveredCode="true"
+         colors="true"
+         convertErrorsToExceptions="true"
+         convertNoticesToExceptions="true"
+         convertWarningsToExceptions="true"
+         strict="false"
+         verbose="true"
+         >
+
+    <testsuites>
+        <testsuite name="unit">
+            <directory suffix="Test.php">test</directory>
+        </testsuite>
+    </testsuites>
+
+    <filter>
+        <whitelist processUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">application</directory>
+        </whitelist>
+    </filter>
+
+    <!-- <logging>
+        <log type="coverage-html" target="build/coverage" />
+    </logging> -->
+
+</phpunit>

+ 2 - 0
frameworks/PHP/php-zend-framework1/public/index.php

@@ -10,6 +10,8 @@ defined('APPLICATION_ENV')
 
 set_include_path(realpath(dirname(__FILE__) . '/../vendor/zendframework/zendframework1/library'));
 
+require_once 'Zend/Application.php';
+
 // Create application, bootstrap, and run
 $application = new Zend_Application(
     APPLICATION_ENV,

+ 43 - 0
frameworks/PHP/php-zend-framework1/test/application/controllers/DbControllerTest.php

@@ -0,0 +1,43 @@
+<?php
+
+class DbControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
+{
+    protected function setUp()
+    {
+        $this->bootstrap = new Zend_Application(
+            APPLICATION_ENV,
+            APPLICATION_PATH . '/configs/application.ini'
+        );
+
+        parent::setUp();
+    }
+
+    public function testType2SingleDatabaseQuery()
+    {
+        $this->dispatch('/db');
+
+        $this->assertModule('default');
+        $this->assertController('db');
+        $this->assertAction('index');
+        $this->assertResponseCode(200);
+        $this->assertHeaderRegex('Content-Type', '#^application/json$#');
+
+        $content = $this->getResponse()->getBody();
+
+        $this->assertRegExp('#^{"id"\:"\d+","randomNumber":"\d+"}$#', $content);
+
+        $decodedContent = json_decode($content, true);
+
+        $this->assertTrue(json_last_error() === JSON_ERROR_NONE, 'Json decode failure');
+
+        $this->assertArrayHasKey('id', $decodedContent);
+        $this->assertGreaterThan(0, $decodedContent['id']);
+        $this->assertLessThan(10000, $decodedContent['id']);
+
+        $this->assertArrayHasKey('randomNumber', $decodedContent);
+        $this->assertGreaterThan(0, $decodedContent['randomNumber']);
+        $this->assertLessThan(10000, $decodedContent['randomNumber']);
+
+        $this->assertCount(2, $decodedContent);
+    }
+}

+ 53 - 0
frameworks/PHP/php-zend-framework1/test/application/controllers/DbMultiControllerTest.php

@@ -0,0 +1,53 @@
+<?php
+
+class DbMultiControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
+{
+    protected function setUp()
+    {
+        $this->bootstrap = new Zend_Application(
+            APPLICATION_ENV,
+            APPLICATION_PATH . '/configs/application.ini'
+        );
+
+        parent::setUp();
+    }
+
+    public function testType3MultipleDatabaseQueries()
+    {
+        $this->dispatch('/db-multi?queries=2');
+        $this->assertResponseResultsEquals(2, $this->getResponse()->getBody());
+    }
+
+    /**
+     * Helper assertion
+     *
+     * @param  int $expectedCount
+     * @param  string $content
+     */
+    protected function assertResponseResultsEquals($expectedCount, $content)
+    {
+        $this->assertModule('default');
+        $this->assertController('db-multi');
+        $this->assertAction('index');
+        $this->assertResponseCode(200);
+        $this->assertHeaderRegex('Content-Type', '#^application/json$#');
+
+        $results = json_decode($content, true);
+
+        $this->assertTrue(json_last_error() === JSON_ERROR_NONE, 'Json decode failure');
+
+        $this->assertCount($expectedCount, $results);
+
+        $count = count($results);
+        for ($i = 0; $i < $count; $i++) {
+
+            $this->assertArrayHasKey('id', $results[$i]);
+            $this->assertGreaterThan(0, $results[$i]['id']);
+            $this->assertLessThan(10000, $results[$i]['id']);
+
+            $this->assertArrayHasKey('randomNumber', $results[$i]);
+            $this->assertGreaterThan(0, $results[$i]['randomNumber']);
+            $this->assertLessThan(10000, $results[$i]['randomNumber']);
+        }
+    }
+}

+ 30 - 0
frameworks/PHP/php-zend-framework1/test/application/controllers/JsonControllerTest.php

@@ -0,0 +1,30 @@
+<?php
+
+class JsonControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
+{
+    protected function setUp()
+    {
+        $this->bootstrap = new Zend_Application(
+            APPLICATION_ENV,
+            APPLICATION_PATH . '/configs/application.ini'
+        );
+
+        parent::setUp();
+    }
+
+    public function testType1JsonSerialization()
+    {
+        $this->dispatch('/json');
+
+        $this->assertModule('default');
+        $this->assertController('json');
+        $this->assertAction('index');
+        $this->assertResponseCode(200);
+        $this->assertHeaderRegex('Content-Type', '#^application/json$#');
+
+        $content = $this->getResponse()->getBody();
+
+        $this->assertSame('{"message":"Hello, World!"}', $content);
+        $this->assertEquals(27, iconv_strlen($content, 'UTF-8'));
+    }
+}

+ 22 - 0
frameworks/PHP/php-zend-framework1/test/bootstrap.php

@@ -0,0 +1,22 @@
+<?php
+
+ini_set('error_reporting', -1);
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+ini_set('log_errors', 0);
+ini_set('date.timezone', 'UTC');
+ini_set('max_execution_time', 0);
+
+// Define path to application directory
+defined('APPLICATION_PATH')
+    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
+
+// Define application environment
+defined('APPLICATION_ENV')
+    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
+
+// Ensure library/ is on include_path
+set_include_path(realpath(dirname(__FILE__) . '/../vendor/zendframework/zendframework1/library'));
+
+require_once 'Zend/Loader/Autoloader.php';
+Zend_Loader_Autoloader::getInstance();