Browse Source

phpunit 10: convert PHPUnitExtension to new event system

Kevin Pfeifer 3 years ago
parent
commit
7fd20e58b0

+ 1 - 1
phpunit.xml.dist

@@ -29,7 +29,7 @@
     </testsuites>
 
     <extensions>
-        <extension class="Cake\TestSuite\Fixture\PHPUnitExtension"/>
+        <bootstrap class="Cake\TestSuite\Fixture\PHPUnitExtension"/>
     </extensions>
 
     <!-- Prevent coverage reports from looking in tests, vendors, config folders -->

+ 13 - 20
src/TestSuite/Fixture/PHPUnitExtension.php

@@ -16,34 +16,27 @@ declare(strict_types=1);
  */
 namespace Cake\TestSuite\Fixture;
 
-use Cake\Log\Log;
-use Cake\TestSuite\ConnectionHelper;
-use PHPUnit\Runner\BeforeFirstTestHook;
+use Cake\TestSuite\Fixture\PHPUnitSubscriber\BeforeFirstTestMethodCalledSubscriber;
+use PHPUnit\Runner\Extension\Extension;
+use PHPUnit\Runner\Extension\Facade;
+use PHPUnit\Runner\Extension\ParameterCollection;
+use PHPUnit\TextUI\Configuration\Configuration;
 
 /**
  * PHPUnit extension to integrate CakePHP's data-only fixtures.
  */
-class PHPUnitExtension implements BeforeFirstTestHook
+class PHPUnitExtension implements Extension
 {
     /**
-     * Initializes before any tests are run.
-     *
+     * @param \PHPUnit\TextUI\Configuration\Configuration $configuration
+     * @param \PHPUnit\Runner\Extension\Facade $facade
+     * @param \PHPUnit\Runner\Extension\ParameterCollection $parameters
      * @return void
      */
-    public function executeBeforeFirstTest(): void
+    public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
     {
-        $helper = new ConnectionHelper();
-        $helper->addTestAliases();
-
-        $enableLogging = in_array('--debug', $_SERVER['argv'] ?? [], true);
-        if ($enableLogging) {
-            $helper->enableQueryLogging();
-            Log::drop('queries');
-            Log::setConfig('queries', [
-                'className' => 'Console',
-                'stream' => 'php://stderr',
-                'scopes' => ['queriesLog'],
-            ]);
-        }
+        $facade->registerSubscriber(
+            new BeforeFirstTestMethodCalledSubscriber()
+        );
     }
 }

+ 48 - 0
src/TestSuite/Fixture/PHPUnitSubscriber/BeforeFirstTestMethodCalledSubscriber.php

@@ -0,0 +1,48 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         5.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Fixture\PHPUnitSubscriber;
+
+use Cake\Log\Log;
+use Cake\TestSuite\ConnectionHelper;
+use PHPUnit\Event\Test\BeforeFirstTestMethodCalled;
+use PHPUnit\Event\Test\BeforeFirstTestMethodCalledSubscriber as BeforeFirstTestMethodCalledSubscriberInterface;
+
+class BeforeFirstTestMethodCalledSubscriber implements BeforeFirstTestMethodCalledSubscriberInterface
+{
+    /**
+     * Initializes before any tests are run.
+     *
+     * @param \PHPUnit\Event\Test\BeforeFirstTestMethodCalled $event The event
+     * @return void
+     */
+    public function notify(BeforeFirstTestMethodCalled $event): void
+    {
+        $helper = new ConnectionHelper();
+        $helper->addTestAliases();
+
+        $enableLogging = in_array('--debug', $_SERVER['argv'] ?? [], true);
+        if ($enableLogging) {
+            $helper->enableQueryLogging();
+            Log::drop('queries');
+            Log::setConfig('queries', [
+                'className' => 'Console',
+                'stream' => 'php://stderr',
+                'scopes' => ['queriesLog'],
+            ]);
+        }
+    }
+}