Browse Source

Merge pull request #15973 from cakephp/4.next-tests-stan

Add phpstan for tests.
othercorey 4 years ago
parent
commit
82876f9b7f
31 changed files with 225 additions and 36 deletions
  1. 1 0
      templates/Error/missing_cell_template.php
  2. 7 2
      tests/TestCase/Auth/ControllerAuthorizeTest.php
  3. 2 0
      tests/TestCase/Auth/DigestAuthenticateTest.php
  4. 13 3
      tests/TestCase/Auth/Storage/SessionStorageTest.php
  5. 5 0
      tests/TestCase/Console/ShellDispatcherTest.php
  6. 5 0
      tests/TestCase/Controller/Component/AuthComponentTest.php
  7. 7 2
      tests/TestCase/Controller/Component/FlashComponentTest.php
  8. 1 1
      tests/TestCase/Controller/Component/SecurityComponentTest.php
  9. 5 0
      tests/TestCase/Controller/ControllerFactoryTest.php
  10. 5 0
      tests/TestCase/Controller/Exception/AuthSecurityExceptionTest.php
  11. 7 2
      tests/TestCase/Error/ExceptionRendererTest.php
  12. 5 0
      tests/TestCase/Http/MiddlewareQueueTest.php
  13. 20 0
      tests/TestCase/Http/RunnerTest.php
  14. 5 0
      tests/TestCase/I18n/TimeTest.php
  15. 5 0
      tests/TestCase/Mailer/Transport/DebugTransportTest.php
  16. 1 0
      tests/TestCase/Routing/RouterTest.php
  17. 0 9
      tests/TestCase/View/Helper/FormHelperTest.php
  18. 7 2
      tests/TestCase/View/Helper/NumberHelperTest.php
  19. 7 2
      tests/TestCase/View/Helper/TextHelperTest.php
  20. 5 5
      tests/TestCase/View/ViewTest.php
  21. 10 0
      tests/TestCase/View/Widget/BasicWidgetTest.php
  22. 10 0
      tests/TestCase/View/Widget/ButtonWidgetTest.php
  23. 10 0
      tests/TestCase/View/Widget/CheckboxWidgetTest.php
  24. 15 0
      tests/TestCase/View/Widget/DateTimeWidgetTest.php
  25. 10 0
      tests/TestCase/View/Widget/FileWidgetTest.php
  26. 10 0
      tests/TestCase/View/Widget/LabelWidgetTest.php
  27. 10 0
      tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php
  28. 10 0
      tests/TestCase/View/Widget/RadioWidgetTest.php
  29. 10 0
      tests/TestCase/View/Widget/SelectBoxWidgetTest.php
  30. 10 0
      tests/TestCase/View/Widget/TextareaWidgetTest.php
  31. 7 8
      tests/phpstan.neon

+ 1 - 0
templates/Error/missing_cell_template.php

@@ -13,6 +13,7 @@
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  * @var string $name
  * @var string $file
+ * @var array<string> $paths
  */
 $this->layout = 'dev_error';
 

+ 7 - 2
tests/TestCase/Auth/ControllerAuthorizeTest.php

@@ -30,6 +30,11 @@ use Cake\TestSuite\TestCase;
 class ControllerAuthorizeTest extends TestCase
 {
     /**
+     * @var \Cake\Controller\Controller|\PHPUnit\Framework\MockObject\MockObject
+     */
+    protected $controller;
+
+    /**
      * @var \Cake\Auth\ControllerAuthorize
      */
     protected $auth;
@@ -44,9 +49,9 @@ class ControllerAuthorizeTest extends TestCase
             ->addMethods(['isAuthorized'])
             ->disableOriginalConstructor()
             ->getMock();
-        $this->components = new ComponentRegistry($this->controller);
+        $components = new ComponentRegistry($this->controller);
 
-        $this->auth = new ControllerAuthorize($this->components);
+        $this->auth = new ControllerAuthorize($components);
     }
 
     public function testControllerErrorOnMissingMethod(): void

+ 2 - 0
tests/TestCase/Auth/DigestAuthenticateTest.php

@@ -133,6 +133,7 @@ class DigestAuthenticateTest extends TestCase
             'environment' => ['REQUEST_METHOD' => 'GET'],
         ]);
 
+        $e = null;
         try {
             $this->auth->unauthenticated($request, new Response());
         } catch (UnauthorizedException $e) {
@@ -166,6 +167,7 @@ class DigestAuthenticateTest extends TestCase
         $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
         $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
 
+        $e = null;
         try {
             $this->auth->unauthenticated($request, new Response());
         } catch (UnauthorizedException $e) {

+ 13 - 3
tests/TestCase/Auth/Storage/SessionStorageTest.php

@@ -28,11 +28,21 @@ use Cake\TestSuite\TestCase;
 class SessionStorageTest extends TestCase
 {
     /**
+     * @var \Cake\Http\Session|\PHPUnit\Framework\MockObject\MockObject
+     */
+    protected $session;
+
+    /**
      * @var \Cake\Auth\Storage\SessionStorage
      */
     protected $storage;
 
     /**
+     * @var array<string, mixed>
+     */
+    protected $user;
+
+    /**
      * setup
      */
     public function setUp(): void
@@ -40,9 +50,9 @@ class SessionStorageTest extends TestCase
         parent::setUp();
 
         $this->session = $this->getMockBuilder(Session::class)->getMock();
-        $this->request = new ServerRequest(['session' => $this->session]);
-        $this->response = new Response();
-        $this->storage = new SessionStorage($this->request, $this->response, ['key' => 'Auth.AuthUser']);
+        $request = new ServerRequest(['session' => $this->session]);
+        $response = new Response();
+        $this->storage = new SessionStorage($request, $response, ['key' => 'Auth.AuthUser']);
         $this->user = ['id' => 1];
     }
 

+ 5 - 0
tests/TestCase/Console/ShellDispatcherTest.php

@@ -26,6 +26,11 @@ use Cake\TestSuite\TestCase;
 class ShellDispatcherTest extends TestCase
 {
     /**
+     * @var \Cake\Console\ShellDispatcher|\PHPUnit\Framework\MockObject\MockObject
+     */
+    protected $dispatcher;
+
+    /**
      * setUp method
      */
     public function setUp(): void

+ 5 - 0
tests/TestCase/Controller/Component/AuthComponentTest.php

@@ -52,6 +52,11 @@ class AuthComponentTest extends TestCase
     protected $Auth;
 
     /**
+     * @var \Cake\Http\ServerRequest
+     */
+    protected $request;
+
+    /**
      * fixtures property
      *
      * @var array<string>

+ 7 - 2
tests/TestCase/Controller/Component/FlashComponentTest.php

@@ -40,6 +40,11 @@ class FlashComponentTest extends TestCase
     protected $Session;
 
     /**
+     * @var \Cake\Controller\Controller
+     */
+    protected $Controller;
+
+    /**
      * setUp method
      */
     public function setUp(): void
@@ -47,8 +52,8 @@ class FlashComponentTest extends TestCase
         parent::setUp();
         static::setAppNamespace();
         $this->Controller = new Controller(new ServerRequest(['session' => new Session()]));
-        $this->ComponentRegistry = new ComponentRegistry($this->Controller);
-        $this->Flash = new FlashComponent($this->ComponentRegistry);
+        $ComponentRegistry = new ComponentRegistry($this->Controller);
+        $this->Flash = new FlashComponent($ComponentRegistry);
         $this->Session = new Session();
     }
 

+ 1 - 1
tests/TestCase/Controller/Component/SecurityComponentTest.php

@@ -1288,7 +1288,7 @@ class SecurityComponentTest extends TestCase
     public function testBlackholeThrowsBadRequest(): void
     {
         $this->Security->setConfig('blackHoleCallback', '');
-        $message = '';
+        $message = $reason = null;
 
         Configure::write('debug', false);
         try {

+ 5 - 0
tests/TestCase/Controller/ControllerFactoryTest.php

@@ -38,6 +38,11 @@ class ControllerFactoryTest extends TestCase
     protected $factory;
 
     /**
+     * @var \Cake\Core\Container
+     */
+    protected $container;
+
+    /**
      * Setup
      */
     public function setUp(): void

+ 5 - 0
tests/TestCase/Controller/Exception/AuthSecurityExceptionTest.php

@@ -25,6 +25,11 @@ use Cake\TestSuite\TestCase;
 class AuthSecurityExceptionTest extends TestCase
 {
     /**
+     * @var \Cake\Controller\Exception\AuthSecurityException
+     */
+    protected $authSecurityException;
+
+    /**
      * setUp method
      */
     public function setUp(): void

+ 7 - 2
tests/TestCase/Error/ExceptionRendererTest.php

@@ -55,7 +55,12 @@ class ExceptionRendererTest extends TestCase
     /**
      * @var bool
      */
-    protected $_restoreError = false;
+    protected $restoreError = false;
+
+    /**
+     * @var bool
+     */
+    protected $called;
 
     /**
      * setup create a request object to get out of router later.
@@ -78,7 +83,7 @@ class ExceptionRendererTest extends TestCase
     {
         parent::tearDown();
         $this->clearPlugins();
-        if ($this->_restoreError) {
+        if ($this->restoreError) {
             restore_error_handler();
         }
     }

+ 5 - 0
tests/TestCase/Http/MiddlewareQueueTest.php

@@ -29,6 +29,11 @@ use TestApp\Middleware\SampleMiddleware;
 class MiddlewareQueueTest extends TestCase
 {
     /**
+     * @var string
+     */
+    protected $previousNamespace;
+
+    /**
      * setUp
      */
     public function setUp(): void

+ 20 - 0
tests/TestCase/Http/RunnerTest.php

@@ -29,6 +29,26 @@ use RuntimeException;
 class RunnerTest extends TestCase
 {
     /**
+     * @var \Cake\Http\MiddlewareQueue
+     */
+    protected $queue;
+
+    /**
+     * @var \Closure
+     */
+    protected $ok;
+
+    /**
+     * @var \Closure
+     */
+    protected $pass;
+
+    /**
+     * @var \Closure
+     */
+    protected $fail;
+
+    /**
      * setup
      */
     public function setUp(): void

+ 5 - 0
tests/TestCase/I18n/TimeTest.php

@@ -32,6 +32,11 @@ use IntlDateFormatter;
 class TimeTest extends TestCase
 {
     /**
+     * @var \Cake\Chronos\ChronosInterface|null
+     */
+    protected $now;
+
+    /**
      * setUp method
      */
     public function setUp(): void

+ 5 - 0
tests/TestCase/Mailer/Transport/DebugTransportTest.php

@@ -28,6 +28,11 @@ use Cake\TestSuite\TestCase;
 class DebugTransportTest extends TestCase
 {
     /**
+     * @var \Cake\Mailer\Transport\DebugTransport
+     */
+    protected $DebugTransport;
+
+    /**
      * Setup
      */
     public function setUp(): void

+ 1 - 0
tests/TestCase/Routing/RouterTest.php

@@ -1200,6 +1200,7 @@ class RouterTest extends TestCase
          * @var string $UUID
          * @var string $Year
          * @var string $Month
+         * @var string $Day
          * @var string $Action
          */
         extract(Router::getNamedExpressions());

+ 0 - 9
tests/TestCase/View/Helper/FormHelperTest.php

@@ -99,15 +99,6 @@ class FormHelperTest extends TestCase
         $this->url = '/articles/add';
         $this->Form = new FormHelper($this->View);
 
-        $this->dateRegex = [
-            'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
-            'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
-            'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
-            'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
-            'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
-            'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
-        ];
-
         $this->article = [
             'schema' => [
                 'id' => ['type' => 'integer'],

+ 7 - 2
tests/TestCase/View/Helper/NumberHelperTest.php

@@ -40,6 +40,11 @@ class NumberHelperTest extends TestCase
     protected $View;
 
     /**
+     * @var string
+     */
+    protected $appNamespace;
+
+    /**
      * setUp method
      */
     public function setUp(): void
@@ -47,7 +52,7 @@ class NumberHelperTest extends TestCase
         parent::setUp();
         $this->View = new View();
 
-        $this->_appNamespace = Configure::read('App.namespace');
+        $this->appNamespace = Configure::read('App.namespace');
         static::setAppNamespace();
     }
 
@@ -58,7 +63,7 @@ class NumberHelperTest extends TestCase
     {
         parent::tearDown();
         $this->clearPlugins();
-        static::setAppNamespace($this->_appNamespace);
+        static::setAppNamespace($this->appNamespace);
         unset($this->View);
     }
 

+ 7 - 2
tests/TestCase/View/Helper/TextHelperTest.php

@@ -41,6 +41,11 @@ class TextHelperTest extends TestCase
     protected $View;
 
     /**
+     * @var string
+     */
+    protected $appNamespace;
+
+    /**
      * setUp method
      */
     public function setUp(): void
@@ -49,7 +54,7 @@ class TextHelperTest extends TestCase
         $this->View = new View();
         $this->Text = new TextHelper($this->View);
 
-        $this->_appNamespace = Configure::read('App.namespace');
+        $this->appNamespace = Configure::read('App.namespace');
         static::setAppNamespace();
     }
 
@@ -59,7 +64,7 @@ class TextHelperTest extends TestCase
     public function tearDown(): void
     {
         unset($this->Text, $this->View);
-        static::setAppNamespace($this->_appNamespace);
+        static::setAppNamespace($this->appNamespace);
         parent::tearDown();
     }
 

+ 5 - 5
tests/TestCase/View/ViewTest.php

@@ -71,6 +71,11 @@ class ViewTest extends TestCase
     protected $ThemePostsController;
 
     /**
+     * @var \Cake\Controller\Controller
+     */
+    protected $ThemeController;
+
+    /**
      * setUp method
      */
     public function setUp(): void
@@ -78,7 +83,6 @@ class ViewTest extends TestCase
         parent::setUp();
 
         $request = new ServerRequest();
-        $this->Controller = new Controller($request);
         $this->PostsController = new ViewPostsController($request);
         $this->PostsController->index();
         $this->View = $this->PostsController->createView();
@@ -88,8 +92,6 @@ class ViewTest extends TestCase
         $this->ThemeController = new Controller($themeRequest);
         $this->ThemePostsController = new ThemePostsController($themeRequest);
         $this->ThemePostsController->index();
-        $this->ThemeView = $this->ThemePostsController->createView();
-        $this->ThemeView->setTemplatePath('Posts');
 
         $this->loadPlugins(['TestPlugin', 'PluginJs', 'TestTheme', 'Company/TestPluginThree']);
         Configure::write('debug', true);
@@ -104,8 +106,6 @@ class ViewTest extends TestCase
         $this->clearPlugins();
         unset($this->View);
         unset($this->PostsController);
-        unset($this->Controller);
-        unset($this->ThemeView);
         unset($this->ThemePostsController);
         unset($this->ThemeController);
     }

+ 10 - 0
tests/TestCase/View/Widget/BasicWidgetTest.php

@@ -26,6 +26,16 @@ use Cake\View\Widget\BasicWidget;
  */
 class BasicWidgetTest extends TestCase
 {
+    /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
     public function setUp(): void
     {
         parent::setUp();

+ 10 - 0
tests/TestCase/View/Widget/ButtonWidgetTest.php

@@ -26,6 +26,16 @@ use Cake\View\Widget\ButtonWidget;
  */
 class ButtonWidgetTest extends TestCase
 {
+    /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
     public function setUp(): void
     {
         parent::setUp();

+ 10 - 0
tests/TestCase/View/Widget/CheckboxWidgetTest.php

@@ -27,6 +27,16 @@ use Cake\View\Widget\CheckboxWidget;
 class CheckboxWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup method.
      */
     public function setUp(): void

+ 15 - 0
tests/TestCase/View/Widget/DateTimeWidgetTest.php

@@ -27,6 +27,21 @@ use DateTime;
  */
 class DateTimeWidgetTest extends TestCase
 {
+    /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
+     * @var \Cake\View\Widget\DateTimeWidget
+     */
+    protected $DateTime;
+
     public function setUp(): void
     {
         parent::setUp();

+ 10 - 0
tests/TestCase/View/Widget/FileWidgetTest.php

@@ -28,6 +28,16 @@ use Cake\View\Widget\FileWidget;
 class FileWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup
      */
     public function setUp(): void

+ 10 - 0
tests/TestCase/View/Widget/LabelWidgetTest.php

@@ -27,6 +27,16 @@ use Cake\View\Widget\LabelWidget;
 class LabelWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup method.
      */
     public function setUp(): void

+ 10 - 0
tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php

@@ -29,6 +29,16 @@ use Cake\View\Widget\NestingLabelWidget;
 class MultiCheckboxWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup method.
      */
     public function setUp(): void

+ 10 - 0
tests/TestCase/View/Widget/RadioWidgetTest.php

@@ -29,6 +29,16 @@ use Cake\View\Widget\RadioWidget;
 class RadioWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup method.
      */
     public function setUp(): void

+ 10 - 0
tests/TestCase/View/Widget/SelectBoxWidgetTest.php

@@ -29,6 +29,16 @@ use Cake\View\Widget\SelectBoxWidget;
 class SelectBoxWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup method.
      */
     public function setUp(): void

+ 10 - 0
tests/TestCase/View/Widget/TextareaWidgetTest.php

@@ -27,6 +27,16 @@ use Cake\View\Widget\TextareaWidget;
 class TextareaWidgetTest extends TestCase
 {
     /**
+     * @var \Cake\View\Form\NullContext
+     */
+    protected $context;
+
+    /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $templates;
+
+    /**
      * setup
      */
     public function setUp(): void

+ 7 - 8
tests/phpstan.neon

@@ -4,14 +4,13 @@ parameters:
 	bootstrapFiles:
 		- bootstrap.php
 	paths:
-		- TestCase/Collection/
-		- TestCase/Core/
-		- TestCase/Database/
-		- TestCase/Datasource/
-		- TestCase/Event/
-		- TestCase/ORM/
-		- TestCase/Utility/
-		- TestCase/Validation/
+		- TestCase/
+
+	excludePaths:
+		- TestCase/Error/DebuggerTest.php
+		- TestCase/Error/ErrorHandlerTest.php
+		- TestCase/Filesystem/FolderTest.php
+		- TestCase/Routing/RouterTest.php
 
 	earlyTerminatingMethodCalls:
 		Cake\Console\Shell: