From c3b842908a341c7cf750753c0c5455ad4ccf4a87 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Wed, 21 Dec 2016 10:08:34 +0100
Subject: [PATCH] phafi-toggle-profiling works again

---
 scripts/emacs/phafi-mode.el                   |  17 +-
 tests/phpunit-testlistener-xhprof/LICENSE     |  33 +++
 .../phpunit-testlistener-xhprof/composer.json |  40 ++++
 .../src/XHProfTestListener.php                | 226 ++++++++++++++++++
 .../src/exceptions/Exception.php              |  15 ++
 .../exceptions/InvalidArgumentException.php   |  15 ++
 .../phpunit-testlistener-xhprof/src/load.php  |   5 +
 tests/phpunit_with_profiling.xml              |  70 ++++++
 8 files changed, 413 insertions(+), 8 deletions(-)
 create mode 100644 tests/phpunit-testlistener-xhprof/LICENSE
 create mode 100644 tests/phpunit-testlistener-xhprof/composer.json
 create mode 100755 tests/phpunit-testlistener-xhprof/src/XHProfTestListener.php
 create mode 100644 tests/phpunit-testlistener-xhprof/src/exceptions/Exception.php
 create mode 100644 tests/phpunit-testlistener-xhprof/src/exceptions/InvalidArgumentException.php
 create mode 100644 tests/phpunit-testlistener-xhprof/src/load.php
 create mode 100644 tests/phpunit_with_profiling.xml

diff --git a/scripts/emacs/phafi-mode.el b/scripts/emacs/phafi-mode.el
index 4a34da8c36c..5649758caa0 100644
--- a/scripts/emacs/phafi-mode.el
+++ b/scripts/emacs/phafi-mode.el
@@ -205,9 +205,9 @@
 
     (setq phafi-phpunit-command
 	  (concat	debug-mode 
-			(if phafi-phpunit-profiling (phafi-phpunit-profiling-command) "phpunit")
-			" -c " 
-			phafi-phpunit-config 
+			"phpunit -c " 
+			(phafi-selected-phpunit-config)
+			" "
 			command-filter 
 			" " 
 			filename 
@@ -226,11 +226,6 @@
   )
 
 
-(defun phafi-phpunit-profiling-command()
-  (concat (phafi-mode-dir) "profile-phpunit")
-  )
-
-
 (defun phafi-run-phpunit(debug-mode)
   "Run all phpunit tests"
   (interactive "P" )
@@ -354,6 +349,12 @@
   )
 
 
+(defun phafi-selected-phpunit-config()
+  (if phafi-phpunit-profiling
+      (concat (phafi-root-dir) "tests/phpunit_with_profiling.xml")
+    phafi-phpunit-config)
+  )
+
 (defun phafi-sql-patch(patch)
   "Force execution of db migration"
 	(interactive (list (read-number "Enter patch number: ")))
diff --git a/tests/phpunit-testlistener-xhprof/LICENSE b/tests/phpunit-testlistener-xhprof/LICENSE
new file mode 100644
index 00000000000..97048642f8e
--- /dev/null
+++ b/tests/phpunit-testlistener-xhprof/LICENSE
@@ -0,0 +1,33 @@
+PHPUnit_TestListener_XHProf
+
+Copyright (c) 2010-2015, Sebastian Bergmann <sebastian@phpunit.de>.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ * Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in
+   the documentation and/or other materials provided with the
+   distribution.
+
+ * Neither the name of Sebastian Bergmann nor the names of his
+   contributors may be used to endorse or promote products derived
+   from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
diff --git a/tests/phpunit-testlistener-xhprof/composer.json b/tests/phpunit-testlistener-xhprof/composer.json
new file mode 100644
index 00000000000..f6b45cb1bce
--- /dev/null
+++ b/tests/phpunit-testlistener-xhprof/composer.json
@@ -0,0 +1,40 @@
+{
+    "name": "phpunit/test-listener-xhprof",
+    "description": "A TestListener for PHPUnit that uses XHProf for automated profiling of the tested code",
+    "type": "library",
+    "keywords": [
+        "phpunit",
+        "xhprof"
+    ],
+    "homepage": "https://github.com/phpunit/phpunit-testlistener-xhprof",
+    "license": "BSD-3-Clause",
+    "authors": [
+        {
+            "name": "Sebastian Bergmann",
+            "email": "sb@sebastian-bergmann.de",
+            "role": "lead"
+        },
+        {
+            "name": "Benjamin Eberlei",
+            "email": "kontakt@beberlei.de",
+            "role": "lead"
+        }
+    ],
+    "support": {
+        "issues": "https://github.com/phpunit/phpunit-testlistener-xhprof/issues"
+    },
+    "require": {
+        "php": ">=5.3.3",
+        "phpunit/phpunit": "~4.0"
+    },
+    "autoload": {
+        "classmap": [
+            "src/"
+        ]
+    },
+    "extra": {
+        "branch-alias": {
+            "dev-master": "1.0.x-dev"
+        }
+    }
+}
diff --git a/tests/phpunit-testlistener-xhprof/src/XHProfTestListener.php b/tests/phpunit-testlistener-xhprof/src/XHProfTestListener.php
new file mode 100755
index 00000000000..c71fb45918d
--- /dev/null
+++ b/tests/phpunit-testlistener-xhprof/src/XHProfTestListener.php
@@ -0,0 +1,226 @@
+<?php
+/*
+ * This file is part of PHPUnit.
+ *
+ * (c) Sebastian Bergmann <sebastian@phpunit.de>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PHPUnit\XHProfTestListener;
+
+/**
+ * A TestListener that integrates with XHProf.
+ *
+ * Here is an example XML configuration for activating this listener:
+ *
+ * <code>
+ * <listeners>
+ *  <listener class="PHPUnit\XHProfTestListener\XHProfTestListener">
+ *   <arguments>
+ *    <array>
+ *     <element key="xhprofLibFile">
+ *      <string>/var/www/xhprof_lib/utils/xhprof_lib.php</string>
+ *     </element>
+ *     <element key="xhprofRunsFile">
+ *      <string>/var/www/xhprof_lib/utils/xhprof_runs.php</string>
+ *     </element>
+ *     <element key="xhprofWeb">
+ *      <string>http://localhost/xhprof_html/index.php</string>
+ *     </element>
+ *     <element key="appNamespace">
+ *      <string>Doctrine2</string>
+ *     </element>
+ *     <element key="xhprofFlags">
+ *      <string>XHPROF_FLAGS_CPU,XHPROF_FLAGS_MEMORY</string>
+ *     </element>
+ *     <element key="xhprofIgnore">
+ *      <string>call_user_func,call_user_func_array</string>
+ *     </element>
+ *    </array>
+ *   </arguments>
+ *  </listener>
+ * </listeners>
+ * </code>
+ *
+ * @author     Benjamin Eberlei <kontakt@beberlei.de>
+ * @author     Sebastian Bergmann <sebastian@phpunit.de>
+ * @copyright  2011-2015 Sebastian Bergmann <sebastian@phpunit.de>
+ * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
+ * @link       http://www.phpunit.de/
+ * @since      Class available since Release 1.0.0
+ */
+class XHProfTestListener implements \PHPUnit_Framework_TestListener
+{
+    /**
+     * @var array
+     */
+    protected $runs = array();
+
+    /**
+     * @var array
+     */
+    protected $options = array();
+
+    /**
+     * @var integer
+     */
+    protected $suites = 0;
+
+    /**
+     * Constructor.
+     *
+     * @param array $options
+     */
+    public function __construct(array $options = array())
+    {
+        if (!isset($options['appNamespace'])) {
+            throw new InvalidArgumentException(
+              'The "appNamespace" option is not set.'
+            );
+        }
+
+        if (!isset($options['xhprofLibFile']) ||
+            !file_exists($options['xhprofLibFile'])) {
+            throw new InvalidArgumentException(
+              'The "xhprofLibFile" option is not set or the configured file does not exist'
+            );
+        }
+
+        if (!isset($options['xhprofRunsFile']) ||
+            !file_exists($options['xhprofRunsFile'])) {
+            throw new InvalidArgumentException(
+              'The "xhprofRunsFile" option is not set or the configured file does not exist'
+            );
+        }
+
+        require_once $options['xhprofLibFile'];
+        require_once $options['xhprofRunsFile'];
+
+        $this->options = $options;
+    }
+
+    /**
+     * An error occurred.
+     *
+     * @param \PHPUnit_Framework_Test $test
+     * @param \Exception              $e
+     * @param float                  $time
+     */
+    public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+    {
+    }
+
+    /**
+     * A failure occurred.
+     *
+     * @param \PHPUnit_Framework_Test                 $test
+     * @param \PHPUnit_Framework_AssertionFailedError $e
+     * @param float                                  $time
+     */
+    public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
+    {
+    }
+
+    /**
+     * Incomplete test.
+     *
+     * @param \PHPUnit_Framework_Test $test
+     * @param \Exception              $e
+     * @param float                  $time
+     */
+    public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+    {
+    }
+
+    /**
+     * Skipped test.
+     *
+     * @param \PHPUnit_Framework_Test $test
+     * @param \Exception              $e
+     * @param float                  $time
+     */
+    public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+    {
+    }
+
+    /**
+     * Risky test.
+     *
+     * @param \PHPUnit_Framework_Test $test
+     * @param \Exception              $e
+     * @param float                  $time
+     */
+    public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+    {
+    }
+
+    /**
+     * A test started.
+     *
+     * @param \PHPUnit_Framework_Test $test
+     */
+    public function startTest(\PHPUnit_Framework_Test $test)
+    {
+        if (!isset($this->options['xhprofFlags'])) {
+            $flags = XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY;
+        } else {
+            $flags = 0;
+
+            foreach (explode(',', $this->options['xhprofFlags']) as $flag) {
+                $flags += constant($flag);
+            }
+        }
+
+        xhprof_enable($flags, array(
+            'ignored_functions' => explode(',', $this->options['xhprofIgnore'])
+        ));
+    }
+
+    /**
+     * A test ended.
+     *
+     * @param \PHPUnit_Framework_Test $test
+     * @param float                  $time
+     */
+    public function endTest(\PHPUnit_Framework_Test $test, $time)
+    {
+        $data         = xhprof_disable();
+        $runs         = new \XHProfRuns_Default;
+        $run          = $runs->save_run($data, $this->options['appNamespace']);
+        $test_name    = get_class($test) . '::' . $test->getName();
+        $this->runs[$test_name] = $this->options['xhprofWeb'] . '?run=' . $run .
+                                  '&source=' . $this->options['appNamespace'];
+    }
+
+    /**
+     * A test suite started.
+     *
+     * @param \PHPUnit_Framework_TestSuite $suite
+     */
+    public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
+    {
+        $this->suites++;
+    }
+
+    /**
+     * A test suite ended.
+     *
+     * @param \PHPUnit_Framework_TestSuite $suite
+     */
+    public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
+    {
+        $this->suites--;
+
+        if ($this->suites == 0) {
+            print "\n\nXHProf runs: " . count($this->runs) . "\n";
+
+            foreach ($this->runs as $test => $run) {
+                print ' * ' . $test . "\n   " . $run . "\n\n";
+            }
+
+            print "\n";
+        }
+    }
+}
diff --git a/tests/phpunit-testlistener-xhprof/src/exceptions/Exception.php b/tests/phpunit-testlistener-xhprof/src/exceptions/Exception.php
new file mode 100644
index 00000000000..db41f6117fc
--- /dev/null
+++ b/tests/phpunit-testlistener-xhprof/src/exceptions/Exception.php
@@ -0,0 +1,15 @@
+<?php
+/*
+ * This file is part of PHPUnit.
+ *
+ * (c) Sebastian Bergmann <sebastian@phpunit.de>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PHPUnit\XHProfTestListener;
+
+interface Exception
+{
+}
diff --git a/tests/phpunit-testlistener-xhprof/src/exceptions/InvalidArgumentException.php b/tests/phpunit-testlistener-xhprof/src/exceptions/InvalidArgumentException.php
new file mode 100644
index 00000000000..f2c57d64df8
--- /dev/null
+++ b/tests/phpunit-testlistener-xhprof/src/exceptions/InvalidArgumentException.php
@@ -0,0 +1,15 @@
+<?php
+/*
+ * This file is part of PHPUnit.
+ *
+ * (c) Sebastian Bergmann <sebastian@phpunit.de>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PHPUnit\XHProfTestListener;
+
+class InvalidArgumentException extends \InvalidArgumentException implements Exception
+{
+}
diff --git a/tests/phpunit-testlistener-xhprof/src/load.php b/tests/phpunit-testlistener-xhprof/src/load.php
new file mode 100644
index 00000000000..5fded844ea0
--- /dev/null
+++ b/tests/phpunit-testlistener-xhprof/src/load.php
@@ -0,0 +1,5 @@
+<?php
+require_once('XHProfTestListener.php');
+require_once('exceptions/Exception.php');
+require_once('exceptions/InvalidArgumentException.php');
+?>
\ No newline at end of file
diff --git a/tests/phpunit_with_profiling.xml b/tests/phpunit_with_profiling.xml
new file mode 100644
index 00000000000..a279b67a2f1
--- /dev/null
+++ b/tests/phpunit_with_profiling.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit
+    bootstrap="./bootstrap.php"
+    colors="false"
+    backupGlobals="false"
+    stopOnFailure="false"
+    stopOnError="false"
+    >
+  <testsuites>    
+    <testsuite name="DBTestSuite">
+      <directory>./db/</directory>
+    </testsuite>
+    <testsuite name="ScenariosTestSuite">
+      <directory>./scenarios/</directory>
+    </testsuite>
+    <testsuite name="ApplicationTestSuite">
+      <directory>./scenarios/</directory>
+      <directory>./application/</directory>
+      <directory>./library/</directory>
+      <directory>./js/</directory>
+      <directory>../library/storm/tests/Storm/</directory>
+    </testsuite>
+    <testsuite name="DigitalResourcesTestSuite">
+      <directory>../library/digital_resources/</directory>
+    </testsuite>
+  </testsuites>
+  <filter>
+    <whitelist>
+      <directory suffix=".php">../application</directory>
+      <directory suffix=".php">../library/Class</directory>
+      <directory suffix=".php">../library/Trait</directory>
+      <directory suffix=".php">../library/ZendAfi</directory>
+      <directory suffix=".php">../library/fonctions</directory>
+      <directory suffix=".php">./js/</directory>
+      <exclude>
+	<file>../index.php</file>
+	<directory>../library/Class/Pdf</directory>
+	<directory>../library/Thumbs</directory>
+	<directory>../library/storm/tests/Storm</directory>
+      </exclude>
+    </whitelist>
+  </filter>
+  <listeners>
+    <listener file = "./TestSpeedTrap.php" class="TestSpeedTrap"/>
+    <listener class="PHPUnit\XHProfTestListener\XHProfTestListener" file="phpunit-testlistener-xhprof/src/load.php">
+      <arguments>
+	<array>
+	  <element key="xhprofLibFile">
+	    <string>xhprof/xhprof_lib/utils/xhprof_lib.php</string>
+	  </element>
+	  <element key="xhprofRunsFile">
+	    <string>xhprof/xhprof_lib/utils/xhprof_runs.php</string>
+	  </element>
+	  <element key="xhprofWeb">
+	    <string>xhprof/xhprof_html/index.php</string>
+	  </element>
+	  <element key="xhprofFlags">
+	    <string>XHPROF_FLAGS_CPU,XHPROF_FLAGS_MEMORY</string>
+	  </element>
+	  <element key="xhprofIgnore">
+	    <string>call_user_func,call_user_func_array</string>
+	  </element>
+	  <element key="appNamespace">
+	    <string>Bokeh</string>
+	  </element>
+	</array>
+      </arguments>
+    </listener>
+  </listeners>
+</phpunit>
-- 
GitLab