我正在使用PHPUnit测试用例测试模块。所有的东西都工作正常,但是当我使用$_SERVER['REMOTE_ADDR']
它时会出现致命错误并停止执行。
CategoryControllerTest.php
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('ProductBundle\Controller\CategoryController::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()); } }
错误
有1个错误:
1)ProductBundle \ Tests \ Controller \ CategoryControllerTest :: testCategory未定义的索引:REMOTE_ADDR
我试图将其添加到setUp()
功能中,但效果不佳。