[NFC] Update mocking to use getMockBuilder instead instead of createMock
authorSeamus Lee <seamuslee001@gmail.com>
Sun, 14 Jun 2020 02:22:23 +0000 (12:22 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Sun, 14 Jun 2020 02:22:23 +0000 (12:22 +1000)
tests/phpunit/CRM/Core/CommunityMessagesTest.php
tests/phpunit/CRM/Core/JobManagerTest.php
tests/phpunit/CRM/Extension/ManagerTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContactTest.php

index a2c8d800eba03c8b5b54d98b932d3c61a528cb86..e4a4e8b643186af8e77347bd44c2897ba6696a4b 100644 (file)
@@ -369,8 +369,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
    * @return CRM_Utils_HttpClient|PHPUnit\Framework\MockObject\MockObject
    */
   protected function expectNoHttpRequest() {
-    $mockFunction = $this->mockMethod;
-    $client = $this->$mockFunction('CRM_Utils_HttpClient');
+    $client = $this->getMockBuilder('CRM_Utils_HttpClient')->getMock();
     $client->expects($this->never())
       ->method('get');
     return $client;
@@ -384,8 +383,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
    * @return CRM_Utils_HttpClient|PHPUnit\Framework\MockObject\MockObject
    */
   protected function expectOneHttpRequest($response) {
-    $mockFunction = $this->mockMethod;
-    $client = $this->$mockFunction('CRM_Utils_HttpClient');
+    $client = $this->getMockBuilder('CRM_Utils_HttpClient')->getMock();
     $client->expects($this->once())
       ->method('get')
       ->will($this->returnValue($response));
index 3dbee40464ac6baef7bfa2b78c8ed1f91fc8764f..f2d8077b5dd8e4e4dc735536201fc87e2ecc8839 100644 (file)
@@ -20,7 +20,6 @@ class CRM_Core_JobManagerTest extends CiviUnitTestCase {
   }
 
   public function testHookCron() {
-    $mockFunction = $this->mockMethod;
     $hook = $this->getMockBuilder(stdClass::class)
       ->setMethods(['civicrm_cron'])
       ->getMock();
index f7f58e7040cd04c403ee0733dcaaa343d5f11919..52396f41ba57626a28d2c160c8adccf604723fb8 100644 (file)
@@ -33,8 +33,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * @expectedException CRM_Extension_Exception
    */
   public function testInstallInvalidType() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $testingTypeManager->expects($this->never())
       ->method('onPreInstall');
     $manager = $this->_createManager([
@@ -51,8 +50,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * "the first row" or "all rows".
    */
   public function testInstall_Disable_Uninstall() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -98,8 +96,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * @throws \CRM_Extension_Exception
    */
   public function test_InstallAuto_DisableDownstream_UninstallDownstream() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -140,8 +137,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * @throws \CRM_Extension_Exception
    */
   public function testInstallAuto_Twice() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -170,8 +166,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
   }
 
   public function test_InstallAuto_DisableUpstream() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -213,8 +208,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Subseuently disable and uninstall.
    */
   public function testInstall_DirtyRemove_Disable_Uninstall() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -252,8 +246,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Install an extension with a valid type name.
    */
   public function testInstall_Disable_Enable() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -295,8 +288,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Performing 'install' on a 'disabled' extension performs an 'enable'
    */
   public function testInstall_Disable_Install() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -335,8 +327,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Install an extension with a valid type name.
    */
   public function testEnableBare() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -363,8 +354,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Get the status of an unknown extension.
    */
   public function testStatusUnknownKey() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $testingTypeManager->expects($this->never())
       ->method('onPreInstall');
     $manager = $this->_createManager([
@@ -377,8 +367,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Replace code for an extension that doesn't exist in the container
    */
   public function testReplace_Unknown() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -406,8 +395,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * Replace code for an extension that doesn't exist in the container
    */
   public function testReplace_Uninstalled() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -440,8 +428,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * file="oddball", and the upgrade has file="newextension".
    */
   public function testReplace_Installed() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
@@ -478,8 +465,7 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
    * file="oddball", and the upgrade has file="newextension".
    */
   public function testReplace_InstalledMissing() {
-    $mockFunction = $this->mockMethod;
-    $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface');
+    $testingTypeManager = $this->getMockBuilder('CRM_Extension_Manager_Interface')->getMock();
     $manager = $this->_createManager([
       self::TESTING_TYPE => $testingTypeManager,
     ]);
index 83fff3e4185cae226db4a55ae446b90a74260078..899c6d5a648e9c88fc070af52d5478b03abb8964 100644 (file)
@@ -165,13 +165,6 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    */
   public $setupIDs = [];
 
-  /**
-   * PHPUnit Mock Method to use.
-   *
-   * @var string
-   */
-  public $mockMethod = 'getMock';
-
   /**
    *  Constructor.
    *
@@ -201,12 +194,6 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
       // FIXME: loosen coupling
       _civix_phpunit_setUp();
     }
-    if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '5', '>=')) {
-      $this->mockMethod = 'createMock';
-    }
-    elseif (class_exists('PHPUnit\Runner\Version') && version_compare(PHPUnit\Runner\Version::id(), '6', '>=')) {
-      $this->mockMethod = 'createMock';
-    }
   }
 
   /**
index af306ee34aa11f4398e7f205e21389b6ed3c5e58..939ceaa6945339b6d6910118fa0a941e57b30a0c 100644 (file)
@@ -2370,8 +2370,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertTrue(is_numeric($ufMatch->id));
 
     // setup - mock the calls to CRM_Utils_System_*::getUfId
-    $mockFunction = $this->mockMethod;
-    $userSystem = $this->$mockFunction('CRM_Utils_System_UnitTests', ['getUfId']);
+    $userSystem = $this->getMockBuilder('CRM_Utils_System_UnitTests')->setMethods(['getUfId'])->getMock();
     $userSystem->expects($this->once())
       ->method('getUfId')
       ->with($this->equalTo('exampleUser'))
@@ -2457,8 +2456,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
    */
   public function testContactGetByUnknownUsername() {
     // setup - mock the calls to CRM_Utils_System_*::getUfId
-    $mockFunction = $this->mockMethod;
-    $userSystem = $this->$mockFunction('CRM_Utils_System_UnitTests', ['getUfId']);
+    $userSystem = $this->getMockBuilder('CRM_Utils_System_UnitTests')->setMethods(['getUfId'])->getMock();
     $userSystem->expects($this->once())
       ->method('getUfId')
       ->with($this->equalTo('exampleUser'))