[TASK] Add unit test for fatal error on null object method call 22/42922/2
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 29 Aug 2015 12:27:20 +0000 (14:27 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Sat, 29 Aug 2015 14:34:40 +0000 (16:34 +0200)
With #45685 a bugfix was merged. Now the corresponding
unit test is added as well.

Change-Id: Id62c4c7a29d2e28847381c18e963cb834e35fa01
Resolves: #69405
Relates: #45685
Releases: master, 6.2
Reviewed-on: http://review.typo3.org/42922
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/form/Tests/Unit/Domain/Model/Attribute/AttributesAttributeTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/form/Tests/Unit/Domain/Model/Attribute/AttributesAttributeTest.php b/typo3/sysext/form/Tests/Unit/Domain/Model/Attribute/AttributesAttributeTest.php
new file mode 100644 (file)
index 0000000..7235616
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+namespace typo3\sysext\form\Tests\Unit\Domain;
+
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Form\Domain\Model\Attribute\AbstractAttribute;
+use TYPO3\CMS\Form\Domain\Model\Attribute\AttributesAttribute;
+use TYPO3\CMS\Form\Localization;
+use TYPO3\CMS\Form\Request;
+use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Test case for class \TYPO3\CMS\Form\Domain\Model\Attribute\AttributesAttribute
+ */
+class AttributesAttributeTest extends UnitTestCase {
+
+       /**
+        * @var AttributesAttribute
+        */
+       protected $subject = NULL;
+
+       /**
+        * @var array A backup of registered singleton instances
+        */
+       protected $singletonInstances = array();
+
+       /**
+        * @var string
+        */
+       protected $elementId;
+
+       /**
+        *
+        */
+       public function setUp(){
+               $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
+               GeneralUtility::addInstance(ContentObjectRenderer::class, $contentObjectRendererProphecy->reveal());
+               $localisationProphecy = $this->prophesize(Localization::class);
+               GeneralUtility::addInstance(Localization::class, $localisationProphecy->reveal());
+               $requestProphecy = $this->prophesize(Request::class);
+               $this->singletonInstances = GeneralUtility::getSingletonInstances();
+               GeneralUtility::setSingletonInstance(Request::class, $requestProphecy->reveal());
+               $this->elementId = uniqid('elementId_', TRUE);
+               $this->subject = new AttributesAttribute($this->elementId);
+       }
+
+       /**
+        * Tear down the tests
+        */
+       protected function tearDown() {
+               GeneralUtility::resetSingletonInstances($this->singletonInstances);
+               parent::tearDown();
+       }
+
+       /**
+        * @test
+        */
+       public function hasAttributeReturnsFalseForMissingAttribute() {
+               $this->assertFalse($this->subject->hasAttribute('anAttribute'));
+       }
+
+       /**
+        * @test
+        */
+       public function getValueReturnsEmptyIfAttributeIsNotSet() {
+               $this->assertEmpty($this->subject->getValue('anAttribute'));
+       }
+
+       /**
+        * @test
+        */
+       public function getValueReturnsValueIfAttributeIsSet() {
+               $attributeProphecy = $this->prophesize(AbstractAttribute::class);
+               $attributeProphecy->getValue()->shouldBeCalled()->willReturn('aValue');
+
+               $this->subject->setAttribute('anAttribute', $attributeProphecy->reveal());
+               $this->assertSame('aValue', $this->subject->getValue('anAttribute'));
+       }
+}