小编Jan*_*nvi的帖子

如何在PHPUnit测试用例中使用服务器变量?

我正在使用PHPUnit测试用例测试模块。所有的东西都工作正常,但是当我使用$_SERVER['REMOTE_ADDR']它时会出现致命错误并停止执行。

CategoryControllerTest.php

<?php
namespace ProductBundle\Controller\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
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('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());
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

有1个错误: …

php phpunit symfony

5
推荐指数
1
解决办法
3043
查看次数

为什么我在 Users.history:list API 中得到空数组

我正在尝试使用Users:History带有historyId的API来获取gmail帐户中完成的更改历史记录。但不知何故,它会在完成几秒钟或几分钟的更改后返回数据。

代码

function listHistory($service, $userId, $startHistoryId) {
  $opt_param = array('startHistoryId' => $startHistoryId);
  $pageToken = NULL;
  $histories = array();

  do {
    try {
      if ($pageToken) {
        $opt_param['pageToken'] = $pageToken;
      }
      $historyResponse = $service->users_history->listUsersHistory($userId, $opt_param);
      if ($historyResponse->getHistory()) {
        $histories = array_merge($histories, $historyResponse->getHistory());
        $pageToken = $historyResponse->getNextPageToken();
      }
    } catch (Exception $e) {
      print 'An error occurred: ' . $e->getMessage();
    }
  } while ($pageToken);


  return $histories;
}
Run Code Online (Sandbox Code Playgroud)

参考: https: //developers.google.com/gmail/api/v1/reference/users/history/list

php gmail-api

2
推荐指数
1
解决办法
958
查看次数

标签 统计

php ×2

gmail-api ×1

phpunit ×1

symfony ×1