diff --git a/.gitignore b/.gitignore index 93219f1..43c01d7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ composer.lock vendor index.php typo3 -Tests +/Tests diff --git a/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php b/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php new file mode 100644 index 0000000..77b64fb --- /dev/null +++ b/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php @@ -0,0 +1,56 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use Codappix\TestingTalk\Controller\FrontendUserController; +use PHPUnit\Framework\TestCase; +use TYPO3\CMS\Extbase\Domain\Model\FrontendUser; +use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; +use TYPO3\CMS\Extbase\Reflection\ObjectAccess; + +class FrontendUserControllerTest extends TestCase +{ + /** + * @var FrontendUserController + */ + protected $subject; + + public function setUp() + { + $this->subject = new FrontendUserController(); + } + + /** + * @test + */ + public function providedFrontendUserIsAssignedToView() + { + $frontendUserMock = $this->getMockBuilder(FrontendUser::class)->getMock(); + $viewMock = $this->getMockBuilder(ViewInterface::class)->getMock(); + ObjectAccess::setProperty($this->subject, 'view', $viewMock, true); + + $viewMock->expects($this->once()) + ->method('assign') + ->with('frontendUser', $frontendUserMock); + + $this->subject->showAction($frontendUserMock); + } +} diff --git a/Resources/Private/CodeExamples/Tests/Unit/Domain/Model/AddressTest.php b/Resources/Private/CodeExamples/Tests/Unit/Domain/Model/AddressTest.php new file mode 100644 index 0000000..ef15f9f --- /dev/null +++ b/Resources/Private/CodeExamples/Tests/Unit/Domain/Model/AddressTest.php @@ -0,0 +1,55 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use Codappix\TestingTalk\Domain\Model\Address; +use PHPUnit\Framework\TestCase; + +class AddressTest extends TestCase +{ + /** + * @test + */ + public function streetIsTrimmed() + { + $subject = new Address(' Street ', ''); + + $this->assertSame( + 'Street', + $subject->getStreet(), + 'Street was not trimmed.' + ); + } + + /** + * @test + */ + public function houseNumberIsTrimmed() + { + $subject = new Address('', ' 12 a '); + + $this->assertSame( + '12 a', + $subject->getHouseNumber(), + 'House number was not trimmed.' + ); + } +} diff --git a/readme.rst b/readme.rst index 06b815a..4453fcc 100644 --- a/readme.rst +++ b/readme.rst @@ -34,7 +34,7 @@ Links: Create first test ----------------- -We want to test the controller first:: +We want to test the model first:: mkdir -p Tests/Unit/Domain/Model cp Resources/Private/CodeExamples/Tests/Unit/Domain/Model/AddressTest.php \ @@ -43,3 +43,16 @@ We want to test the controller first:: Execute first test:: ./vendor/bin/phpunit Tests/Unit/ + +Create test for controller +-------------------------- + +We want to test the controller now:: + + mkdir -p Tests/Unit/Controller + cp Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php \ + Tests/Unit/Controller + +Execute all tests:: + + ./vendor/bin/phpunit Tests/Unit/