How to use server variables in PHPUnit Test cases?(如何在 PHPUnit 测试用例中使用服务器变量?)
问题描述
我正在使用 PHPUnit 测试用例测试模块.一切正常,但是当我使用 $_SERVER['REMOTE_ADDR']
时,它会出现致命错误并停止执行.
CategoryControllerTest.php
boot();$this->container = static::$kernel->getContainer();$this->em = static::$kernel->getContainer()->get('doctrine')->getManager();}公共功能 testCategory() {$ip_address = $_SERVER['REMOTE_ADDR'];$client = 静态::createClient(array(), array('HTTP_HOST' => static::$kernel->getContainer()->getParameter('test_http_host')));$crawler = $client->request('POST', '/category/new');$client->enableProfiler();$this->assertEquals('ProductBundleControllerCategoryController::addAction', $client->getRequest()->attributes->get('_controller'));$form = $crawler->selectButton('new_category')->form();$form['category[name]'] = "电子";$form['category[id]'] = "美国版";$form['category[ip]'] = $ip_address;$client->submit($form);$this->assertTrue($client->getResponse()->isRedirect('/category/new'));//检查重定向是否正确$client->followRedirect();$this->assertEquals(1, $crawler->filter('html:contains("类别创建成功.")')->count());}}
错误
有 1 个错误:
<块引用>1) ProductBundleTestsControllerCategoryControllerTest::testCategory未定义索引:REMOTE_ADDR
我尝试将它添加到 setUp()
函数中,但效果不佳.
创建返回IP地址的服务,并在测试用例中模拟该服务.
在这里,将控制器和服务创建为 UserIpAddress.get()
将返回用户的 ip 地址.
service.yml
UserIpAddress:类:AppBundleControllerUserIpAddressController论据:容器:@service_container"
UserIpAddressController.php
类 UserIpAddressController{公共函数获取(){返回 $_SERVER['REMOTE_ADDR'];}}
<块引用>
创建UserIpAddress"服务的模拟.它将覆盖现有服务.使用UserIpAddress"服务获取项目中的 IP 地址.
CategoryControllerTest.php
$UserIpAddress = $this->getMockBuilder('UserIpAddress')->disableOriginalConstructor()->getMock();$UserIpAddress->expects($this->once())->方法('get')->willReturn('192.161.1.1');//设置你想使用的IP地址
现在,使用 $UserIpAddress->get();
I am testing modules using PHPUnit Test cases. All the things working fine but when i use $_SERVER['REMOTE_ADDR']
it gives fatal error and stops execution.
CategoryControllerTest.php
<?php
namespace ProductBundleControllerTestsController;
use SymfonyBundleFrameworkBundleTestWebTestCase;
class CategoryControllerTest extends WebTestCase {
protected function setUp() {
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->container = static::$kernel->getContainer();
$this->em = static::$kernel->getContainer()->get('doctrine')->getManager();
}
public function testCategory() {
$ip_address = $_SERVER['REMOTE_ADDR'];
$client = static::createClient(
array(), array('HTTP_HOST' => static::$kernel->getContainer()->getParameter('test_http_host')
));
$crawler = $client->request('POST', '/category/new');
$client->enableProfiler();
$this->assertEquals('ProductBundleControllerCategoryController::addAction', $client->getRequest()->attributes->get('_controller'));
$form = $crawler->selectButton('new_category')->form();
$form['category[name]'] = "Electronics";
$form['category[id]'] = "For US";
$form['category[ip]'] = $ip_address;
$client->submit($form);
$this->assertTrue($client->getResponse()->isRedirect('/category/new')); // check if redirecting properly
$client->followRedirect();
$this->assertEquals(1, $crawler->filter('html:contains("Category Created Successfully.")')->count());
}
}
Error
There was 1 error:
1) ProductBundleTestsControllerCategoryControllerTest::testCategory Undefined index: REMOTE_ADDR
I have tried to add it in setUp()
function but it's not working as well.
Create service which returns ip address and mock the service in test case.
Here, create controller and service as UserIpAddress. get()
will return ip address of user.
service.yml
UserIpAddress:
class: AppBundleControllerUserIpAddressController
arguments:
container: "@service_container"
UserIpAddressController.php
class UserIpAddressController
{
public function get()
{
return $_SERVER['REMOTE_ADDR'];
}
}
Create mock of "UserIpAddress" service. It will override existing service. Use 'UserIpAddress' service to get ip address in your project.
CategoryControllerTest.php
$UserIpAddress = $this->getMockBuilder('UserIpAddress')
->disableOriginalConstructor()
->getMock();
$UserIpAddress->expects($this->once())
->method('get')
->willReturn('192.161.1.1'); // Set ip address whatever you want to use
Now, get ip address using $UserIpAddress->get();
这篇关于如何在 PHPUnit 测试用例中使用服务器变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHPUnit 测试用例中使用服务器变量?
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01