未加星标

Get Ready for TDD Katas with PHPUnit + PhpStorm

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Are you aphp developer looking to start with unit tests? Do you want to follow my weekly TDD Kata posts but don’t know how to start? Let’s get you set up step by step, so that you are ready to write your first test!

Read the first weekly Kata post to learn what it is all about.

Prerequisites PhpStorm IDE Local PHP installation (preferrably PHP 7) Create kata project Create a new project of type “PHP Empty Project”
Get Ready for TDD Katas with PHPUnit + PhpStorm
Go to the menu Tools > Composer > Init Composer… and enter the path to PHP and composer. If you don’t have composer installed, PhpStorm can download it for you right there
Get Ready for TDD Katas with PHPUnit + PhpStorm
A new file composer.json opens. Change the project details if you like, and add an “autoload” section: {
"name": "my/katas",
"description": "My TDD Katas",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "me",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Katas\\": ["src", "tests"]
}
},
"require": {
"phpunit/phpunit": "5.7.5"
}
} Go to the menu Tools > Composer > Add dependency… . Type phpunit/phpunit and select the latest non-dev version:
Get Ready for TDD Katas with PHPUnit + PhpStorm
Click “Install”, composer will now install PHPUnit which might take a few minutes. When it’s done, click “Close” Your first test

Let’s start with the Bowling Game kata:

Create a file tests/Bowling/GameTest.php as follows: <?php
namespace Katas\Bowling;
class GameTest extends \PHPUnit_Framework_TestCase
{
public function testTest()
{
$this->fail('Hello!');
}
} The first test I create in a new project is always one that tests if the test framework itself is running properly. Let’s try to execute this test so that we see the “Hello!” fail message. Right click the test in the project view and select “Run ‘GameTest'”
Get Ready for TDD Katas with PHPUnit + PhpStorm
It is the first time we run a PHPUnit test in this project, so we have to configure the environment a bit. Click “Fix” and choose to load PHPUnit from the composer autoloader ( vendor/autoload.php in your project). This will also enable us to use our own source code without explicit include statements or writing our own autoloader.
Get Ready for TDD Katas with PHPUnit + PhpStorm
Then click “Run”. Now you should see this:
Get Ready for TDD Katas with PHPUnit + PhpStorm
Let’s make the test pass <?php
namespace Katas\Bowling;
class GameTest extends \PHPUnit_Framework_TestCase
{
public function testTest()
{
$this->assertTrue(true);
}
}

Now that we have configured how to run it, there’s a “run” button on the top right. Click it:


Get Ready for TDD Katas with PHPUnit + PhpStorm

This is the result:


Get Ready for TDD Katas with PHPUnit + PhpStorm
OK, we’ve seen a failing and a passing test, but did not test anything yet. The next thing we are testing is if we can instantiate our Game class (we don’t need the testTest method anymore, delete it). <?php
namespace Katas\Bowling;
class GameTest extends \PHPUnit_Framework_TestCase
{
public function testGameCanBeInstantiated()
{
$game = new Game();
$this->assertInstanceOf(Game::class, $game);
}
} Running the test shows an error (of course, the class Game does not exist yet):
Get Ready for TDD Katas with PHPUnit + PhpStorm
Create the file src/Bowling/Game.php as follows: <?php
namespace Katas\Bowling;
class Game
{
} Run the test again, it should pass. If not, double check the autoload settings in composer.json . If you make changes to them, you need to update the autoloader using composer dump-autoload on the command line
Get Ready for TDD Katas with PHPUnit + PhpStorm
If the command “composer” is not found, enter the full path to composer.phar instead. Congratulations, you are now ready to write your first real test! <?php
namespace Katas\Bowling;
class GameTest extends \PHPUnit_Framework_TestCase
{
public function testGutterGame()
{
$game = new Game();
for ($i = 0; $i < 10; ++$i) {$game->roll(0);
}
$this->assertEquals(0, $game->score();
}
}

From here on you should be able to follow the steps in the Bowling Game PowerPoint . The Java code can be written very similar in PHP.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPJava
分页:12
转载请注明
本文标题:Get Ready for TDD Katas with PHPUnit + PhpStorm
本站链接:http://www.codesec.net/view/524022.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(60)