[TASK] Replace calls to assertAttribute* methods of phpunit 58/59758/6
authorAnja Leichsenring <anja.leichsenring@typo3.com>
Tue, 19 Feb 2019 19:42:46 +0000 (20:42 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Sat, 23 Feb 2019 20:30:38 +0000 (21:30 +0100)
Those methods are about to be removed, in preparation the core tests
were reviewed for usage and replaced with suitable functionality.

Resolves: #87746
Releases: master
Change-Id: Ibc79e656b914f235c27d8461bcca358a38c28b6e
Reviewed-on: https://review.typo3.org/c/59758
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
13 files changed:
typo3/sysext/belog/Tests/Unit/Domain/Model/ConstraintTest.php
typo3/sysext/core/Tests/Unit/Log/Fixtures/WriterFixture.php
typo3/sysext/core/Tests/Unit/Log/LoggerTest.php
typo3/sysext/core/Tests/Unit/Log/Processor/AbstractMemoryTest.php
typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php
typo3/sysext/core/Tests/Unit/Mail/MailerTest.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapperTest.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/PersistenceManagerTest.php
typo3/sysext/extbase/Tests/UnitDeprecated/Reflection/ObjectAccessTest.php
typo3/sysext/fluid/Tests/Unit/Core/Rendering/RenderingContextTest.php
typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetContextTest.php
typo3/sysext/fluid/Tests/Unit/View/TemplatePathsTest.php
typo3/sysext/frontend/Tests/Unit/Authentication/FrontendUserAuthenticationTest.php

index 58eed19..dc80431 100644 (file)
@@ -38,8 +38,7 @@ class ConstraintTest extends UnitTestCase
     {
         $date = new \DateTime();
         $this->subject->setManualDateStart($date);
-
-        $this->assertAttributeEquals($date, 'manualDateStart', $this->subject);
+        $this->assertEquals($date, $this->subject->getManualDateStart());
     }
 
     /**
@@ -48,8 +47,7 @@ class ConstraintTest extends UnitTestCase
     public function setManualDateStartForNoArgumentSetsManualDateStart()
     {
         $this->subject->setManualDateStart();
-
-        $this->assertAttributeEquals(null, 'manualDateStart', $this->subject);
+        $this->assertNull($this->subject->getManualDateStart());
     }
 
     /**
@@ -59,8 +57,7 @@ class ConstraintTest extends UnitTestCase
     {
         $date = new \DateTime();
         $this->subject->setManualDateStop($date);
-
-        $this->assertAttributeEquals($date, 'manualDateStop', $this->subject);
+        $this->assertEquals($date, $this->subject->getManualDateStop());
     }
 
     /**
@@ -69,7 +66,6 @@ class ConstraintTest extends UnitTestCase
     public function setManualDateStopForNoArgumentSetsManualDateStop()
     {
         $this->subject->setManualDateStop();
-
-        $this->assertAttributeEquals(null, 'manualDateStop', $this->subject);
+        $this->assertNull($this->subject->getManualDateStop());
     }
 }
index bee35cb..ba3dbb0 100644 (file)
@@ -28,4 +28,9 @@ class WriterFixture extends \TYPO3\CMS\Core\Log\Writer\AbstractWriter
     {
         $this->records[] = $record;
     }
+
+    public function getRecords(): array
+    {
+        return $this->records;
+    }
 }
index 3f660e2..725a72b 100644 (file)
@@ -45,7 +45,7 @@ class LoggerTest extends UnitTestCase
         $logger->addWriter(LogLevel::ERROR, $writer);
         // warning < error, thus must not be logged
         $logger->log(LogLevel::WARNING, 'test message');
-        $this->assertAttributeEmpty('records', $writer);
+        $this->assertEmpty($writer->getRecords());
     }
 
     /**
@@ -127,7 +127,7 @@ class LoggerTest extends UnitTestCase
         $logger->addWriter(LogLevel::NOTICE, $writer);
         // notice == notice, thus must be logged
         $logger->log(LogLevel::NOTICE, 'test message');
-        $this->assertAttributeNotEmpty('records', $writer);
+        $this->assertNotEmpty($writer->getRecords());
     }
 
     /**
@@ -158,7 +158,7 @@ class LoggerTest extends UnitTestCase
         $writer = new Fixtures\WriterFixture();
         $logger->addWriter(LogLevel::DEBUG, $writer);
         call_user_func([$logger, $shorthandMethod], 'test message');
-        $this->assertAttributeNotEmpty('records', $writer);
+        $this->assertNotEmpty($writer->getRecords());
     }
 
     /**
@@ -171,7 +171,7 @@ class LoggerTest extends UnitTestCase
         $logger->addWriter(LogLevel::NOTICE, $writer);
         // warning > notice, thus must be logged
         $logger->log(LogLevel::WARNING, 'test message');
-        $this->assertAttributeNotEmpty('records', $writer);
+        $this->assertNotEmpty($writer->getRecords());
     }
 
     /**
index 9ba0fc6..b9a991c 100644 (file)
@@ -24,32 +24,12 @@ class AbstractMemoryTest extends UnitTestCase
     /**
      * @test
      */
-    public function getRealMemoryUsageGetsRealMemoryUsage()
-    {
-        /** @var $processor \TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor */
-        $processor = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor::class);
-        $this->assertAttributeEquals($processor->getRealMemoryUsage(), 'realMemoryUsage', $processor);
-    }
-
-    /**
-     * @test
-     */
     public function setRealMemoryUsageSetsRealMemoryUsage()
     {
         /** @var $processor \TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor */
         $processor = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor::class);
         $processor->setRealMemoryUsage(false);
-        $this->assertAttributeEquals(false, 'realMemoryUsage', $processor);
-    }
-
-    /**
-     * @test
-     */
-    public function getFormatSizeGetsFormatSize()
-    {
-        /** @var $processor \TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor */
-        $processor = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor::class);
-        $this->assertAttributeEquals($processor->getFormatSize(), 'formatSize', $processor);
+        $this->assertFalse($processor->getRealMemoryUsage());
     }
 
     /**
@@ -60,6 +40,6 @@ class AbstractMemoryTest extends UnitTestCase
         /** @var $processor \TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor */
         $processor = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor::class);
         $processor->setFormatSize(false);
-        $this->assertAttributeEquals(false, 'formatSize', $processor);
+        $this->assertFalse($processor->getFormatSize());
     }
 }
index 304a102..39dc715 100644 (file)
@@ -91,7 +91,7 @@ class FileWriterTest extends UnitTestCase
         vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
         $writer = GeneralUtility::makeInstance(FileWriter::class);
         $writer->setLogFile($this->getDefaultFileName());
-        $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
+        $this->assertEquals($this->getDefaultFileName(), $writer->getLogFile());
     }
 
     /**
@@ -102,7 +102,7 @@ class FileWriterTest extends UnitTestCase
         $writer = GeneralUtility::makeInstance(FileWriter::class);
         $tempFile = rtrim(sys_get_temp_dir(), '/\\') . '/typo3.log';
         $writer->setLogFile($tempFile);
-        $this->assertAttributeEquals($tempFile, 'logFile', $writer);
+        $this->assertEquals($tempFile, $writer->getLogFile());
     }
 
     /**
index 8257b20..f9dd39e 100644 (file)
@@ -15,10 +15,13 @@ namespace TYPO3\CMS\Core\Tests\Unit\Mail;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
 use TYPO3\CMS\Core\Controller\ErrorPageController;
 use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Mail\Mailer;
+use TYPO3\CMS\Core\Mail\TransportFactory;
 use TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeTransportFixture;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -54,9 +57,14 @@ class MailerTest extends UnitTestCase
     {
         $settings = ['transport' => 'mbox', 'transport_mbox_file' => '/path/to/file'];
         $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail'];
+
+        $transportFactory = $this->prophesize(TransportFactory::class);
+        $transportFactory->get(Argument::any())->willReturn($this->prophesize(\Swift_Transport::class));
+        GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
         $this->subject->injectMailSettings($settings);
         $this->subject->__construct();
-        $this->assertAttributeSame($settings, 'mailSettings', $this->subject);
+
+        $transportFactory->get($settings)->shouldHaveBeenCalled();
     }
 
     /**
@@ -66,7 +74,13 @@ class MailerTest extends UnitTestCase
     {
         $settings = ($GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail']);
         $this->subject->__construct();
-        $this->assertAttributeSame($settings, 'mailSettings', $this->subject);
+        $transportFactory = $this->prophesize(TransportFactory::class);
+        $transportFactory->get(Argument::any())->willReturn($this->prophesize(\Swift_Transport::class));
+        GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
+        $this->subject->injectMailSettings($settings);
+        $this->subject->__construct();
+
+        $transportFactory->get($settings)->shouldHaveBeenCalled();
     }
 
     /**
index 1079952..aafd577 100644 (file)
@@ -146,10 +146,10 @@ class DataMapperTest extends UnitTestCase
         );
         $dataMapper->_call('thawProperties', $object, $row);
 
-        $this->assertAttributeEquals('firstValue', 'firstProperty', $object);
-        $this->assertAttributeEquals(1234, 'secondProperty', $object);
-        $this->assertAttributeEquals(1.234, 'thirdProperty', $object);
-        $this->assertAttributeEquals(false, 'fourthProperty', $object);
+        $this->assertEquals('firstValue', $object->firstProperty);
+        $this->assertEquals(1234, $object->secondProperty);
+        $this->assertEquals(1.234, $object->thirdProperty);
+        $this->assertEquals(false, $object->fourthProperty);
     }
 
     /**
index e3f3b3d..3914bad 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
  */
 
 use PHPUnit\Framework\MockObject\MockObject;
+use Prophecy\Argument;
 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
 use TYPO3\CMS\Extbase\Object\Container\Container;
 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
@@ -192,14 +193,24 @@ class PersistenceManagerTest extends UnitTestCase
     public function addActuallyAddsAnObjectToTheInternalObjectsArray(): void
     {
         $someObject = new \stdClass();
+        $backend = $this->prophesize(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
         $persistenceManager = new PersistenceManager(
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface::class),
-            $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class),
+            $backend->reveal(),
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class)
         );
         $persistenceManager->add($someObject);
 
-        $this->assertAttributeContains($someObject, 'addedObjects', $persistenceManager);
+        $expectedAddedObjects = new ObjectStorage();
+        $expectedAddedObjects->attach($someObject);
+
+        // this is the actual assertion
+        $backend->setAggregateRootObjects($expectedAddedObjects)->shouldBeCalled();
+
+        $backend->setChangedEntities(Argument::any())->shouldBeCalled();
+        $backend->setDeletedEntities(Argument::any())->shouldBeCalled();
+        $backend->commit()->shouldBeCalled();
+        $persistenceManager->persistAll();
     }
 
     /**
@@ -211,9 +222,10 @@ class PersistenceManagerTest extends UnitTestCase
         $object2 = new \stdClass();
         $object3 = new \stdClass();
 
+        $backend = $this->prophesize(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
         $persistenceManager = new PersistenceManager(
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface::class),
-            $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class),
+            $backend->reveal(),
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class)
         );
         $persistenceManager->add($object1);
@@ -222,9 +234,20 @@ class PersistenceManagerTest extends UnitTestCase
 
         $persistenceManager->remove($object2);
 
-        $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
-        $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
-        $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
+        $expectedAddedObjects = new ObjectStorage();
+        $expectedAddedObjects->attach($object1);
+        $expectedAddedObjects->attach($object2);
+        $expectedAddedObjects->attach($object3);
+        $expectedAddedObjects->detach($object2);
+
+        // this is the actual assertion
+        $backend->setAggregateRootObjects($expectedAddedObjects)->shouldBeCalled();
+
+        $backend->setChangedEntities(Argument::any())->shouldBeCalled();
+        $backend->setDeletedEntities(Argument::any())->shouldBeCalled();
+        $backend->commit()->shouldBeCalled();
+
+        $persistenceManager->persistAll();
     }
 
     /**
@@ -236,9 +259,10 @@ class PersistenceManagerTest extends UnitTestCase
         $object2 = new \ArrayObject(['val' => '2']);
         $object3 = new \ArrayObject(['val' => '3']);
 
+        $backend = $this->prophesize(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
         $persistenceManager = new PersistenceManager(
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface::class),
-            $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class),
+            $backend->reveal(),
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class)
         );
         $persistenceManager->add($object1);
@@ -250,9 +274,20 @@ class PersistenceManagerTest extends UnitTestCase
 
         $persistenceManager->remove($object2);
 
-        $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
-        $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
-        $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
+        // replay the actual sequence of actions, that makes the objectStorages comparable
+        $expectedAddedObjects = new ObjectStorage();
+        $expectedAddedObjects->attach($object1);
+        $expectedAddedObjects->attach($object2);
+        $expectedAddedObjects->attach($object3);
+        $expectedAddedObjects->detach($object2);
+
+        // this is the actual assertion
+        $backend->setAggregateRootObjects($expectedAddedObjects)->shouldBeCalled();
+
+        $backend->setChangedEntities(Argument::any())->shouldBeCalled();
+        $backend->setDeletedEntities(Argument::any())->shouldBeCalled();
+        $backend->commit()->shouldBeCalled();
+        $persistenceManager->persistAll();
     }
 
     /**
@@ -264,14 +299,24 @@ class PersistenceManagerTest extends UnitTestCase
     public function removeRetainsObjectForObjectsNotInCurrentSession(): void
     {
         $object = new \ArrayObject(['val' => '1']);
+        $backend = $this->prophesize(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
         $persistenceManager = new PersistenceManager(
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface::class),
-            $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class),
+            $backend->reveal(),
             $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class)
         );
         $persistenceManager->remove($object);
 
-        $this->assertAttributeContains($object, 'removedObjects', $persistenceManager);
+        $expectedDeletedObjects = new ObjectStorage();
+        $expectedDeletedObjects->attach($object);
+        $backend->setAggregateRootObjects(Argument::any())->shouldBeCalled();
+        $backend->setChangedEntities(Argument::any())->shouldBeCalled();
+
+        // this is the actual assertion
+        $backend->setDeletedEntities($expectedDeletedObjects)->shouldBeCalled();
+
+        $backend->commit()->shouldBeCalled();
+        $persistenceManager->persistAll();
     }
 
     /**
index 948257d..3bf550a 100644 (file)
@@ -74,22 +74,4 @@ class ObjectAccessTest extends UnitTestCase
         $this->expectExceptionCode(1302855001);
         ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty', true);
     }
-
-    /**
-     * @test
-     */
-    public function setPropertySetsValueIfPropertyIsNotAccessibleWhenForceDirectAccessIsTrue()
-    {
-        $this->assertTrue(ObjectAccess::setProperty($this->dummyObject, 'unexposedProperty', 'was set anyway', true));
-        $this->assertAttributeEquals('was set anyway', 'unexposedProperty', $this->dummyObject);
-    }
-
-    /**
-     * @test
-     */
-    public function setPropertySetsValueIfPropertyDoesNotExistWhenForceDirectAccessIsTrue()
-    {
-        $this->assertTrue(ObjectAccess::setProperty($this->dummyObject, 'unknownProperty', 'was set anyway', true));
-        $this->assertAttributeEquals('was set anyway', 'unknownProperty', $this->dummyObject);
-    }
 }
index 3da1120..69c69e1 100644 (file)
@@ -109,7 +109,7 @@ class RenderingContextTest extends UnitTestCase
         $controllerContext->expects($this->atLeastOnce())->method('getRequest')->willReturn($request);
         $subject->setControllerContext($controllerContext);
         $subject->setControllerAction($input);
-        $this->assertAttributeSame($expected, 'controllerAction', $subject);
+        $this->assertEquals($expected, $subject->getControllerAction());
     }
 
     /**
index aace045..a1cfd7f 100644 (file)
@@ -58,8 +58,9 @@ class WidgetContextTest extends UnitTestCase
     public function setterMethodSetsPropertyValue($name, $value)
     {
         $method = 'set' . ucfirst($name);
+        $getter = 'get' . ucfirst($name);
         call_user_func_array([$this->widgetContext, $method], [$value]);
-        $this->assertAttributeEquals($value, $name, $this->widgetContext);
+        $this->assertEquals($value, $this->widgetContext->$getter());
     }
 
     /**
index 489d19e..98aec5c 100644 (file)
@@ -95,10 +95,11 @@ class TemplatePathsTest extends UnitTestCase
     public function pathSetterMethodSortsPathsByKeyDescending($method, array $paths, array $expected)
     {
         $setter = 'set' . ucfirst($method);
+        $getter = 'get' . ucfirst($method);
         $subject = $this->getMockBuilder(TemplatePaths::class)->setMethods(['sanitizePath'])->getMock();
         $subject->expects($this->any())->method('sanitizePath')->willReturnArgument(0);
         $subject->$setter($paths);
-        $this->assertAttributeSame($expected, $method, $subject);
+        $this->assertEquals($expected, $subject->$getter());
     }
 
     /**
index a04eb4d..d049694 100644 (file)
@@ -172,7 +172,7 @@ class FrontendUserAuthenticationTest extends UnitTestCase
         $subject->start();
         $subject->setSessionData('foo', 'bar');
         $subject->removeSessionData();
-        $this->assertAttributeEmpty('sessionData', $subject);
+        $this->assertNull($subject->getSessionData('someKey'));
     }
 
     /**
@@ -235,7 +235,7 @@ class FrontendUserAuthenticationTest extends UnitTestCase
         $this->assertEmpty($subject->getSessionData($uniqueSessionId));
         $this->assertEmpty($subject->user);
         $subject->setSessionData('foo', 'bar');
-        $this->assertAttributeNotEmpty('sessionData', $subject);
+        $this->assertNotNull('sessionData', $subject->getSessionData('foo'));
 
         // Suppress "headers already sent" errors - phpunit does that internally already
         $prev = error_reporting(0);
@@ -315,7 +315,7 @@ class FrontendUserAuthenticationTest extends UnitTestCase
         $subject->gc_probability = -1;
         $subject->start();
 
-        $this->assertAttributeNotEmpty('user', $subject);
+        $this->assertNotNull($subject->user);
         $this->assertEquals('existingUserName', $subject->user['username']);
     }
 
@@ -533,12 +533,12 @@ class FrontendUserAuthenticationTest extends UnitTestCase
         $this->assertEmpty($this->subject->_get('sessionData'));
         $this->assertEmpty($this->subject->user);
         $this->subject->setSessionData('foo', 'bar');
-        $this->assertAttributeNotEmpty('sessionData', $this->subject);
+        $this->assertNotNull($this->subject->getSessionData('foo'));
         $this->subject->storeSessionData();
 
         // Should delete session after setting to null
         $this->subject->setSessionData('foo', null);
-        $this->assertAttributeEmpty('sessionData', $this->subject);
+        $this->assertNull($this->subject->getSessionData('foo'));
         $sessionBackend->expects($this->once())->method('remove')->with('newSessionId');
         $sessionBackend->expects($this->never())->method('update');