本文整理汇总了PHP中Symfony\Component\BrowserKit\Client类的典型用法代码示例。如果您正苦于以下问题:PHP Client类的具体用法?PHP Client怎么用?PHP Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Initializes Goutte driver.
*
* @param Symfony\Component\BrowserKit\Client $client BrowserKit client instance
*/
public function __construct(Client $client = null)
{
// create new kernel, that could be easily rebooted
$class = get_class($client->getKernel());
$kernel = new $class('Behat', 'config.yml', $client->getKernel()->getEnvironment(), $client->getKernel()->isDebug());
$kernel->boot();
parent::__construct($kernel->getContainer()->get('test.client'));
}
示例2: __construct
/**
* Initializes BrowserKit driver.
*
* @param Client $client BrowserKit client instance
* @param string|null $baseUrl Base URL for HttpKernel clients
*/
public function __construct(Client $client, $baseUrl = null)
{
$this->client = $client;
$this->client->followRedirects(true);
if ($baseUrl !== null && $client instanceof HttpKernelClient) {
$client->setServerParameter('SCRIPT_FILENAME', parse_url($baseUrl, PHP_URL_PATH));
}
}
示例3: testIndex
public function testIndex()
{
$this->client = static::createClient();
$this->client->setServerParameters(['CONTENT_TYPE' => 'application/json', 'HTTP_HOST' => 'localhost:8000']);
// create user
$userName = md5(microtime());
$registration = $this->makeRequest('/user/create', ["user" => ["name" => $userName]]);
$this->assertEquals($registration->getMessage(), 'Ok');
$userId = $registration->getData()->id;
// try to create duplicate
$registration = $this->makeRequest('/user/create', ["user" => ["name" => $userName]]);
$this->assertEquals($registration->getMessage(), 'User already exists');
// create opponents
for ($i = 0; $i <= 10; $i++) {
$registration = $this->makeRequest('/user/create', ["user" => ["name" => md5(microtime() + $i)]]);
$this->assertEquals($registration->getMessage(), 'Ok');
}
// login
$login = $this->makeRequest('/user/login', ["token" => $userName]);
$this->assertEquals($login->getMessage(), 'Ok');
$this->assertEquals($login->getData()->id, $userId);
// opponent
$opponents = $this->makeRequest('/user/opponents', ["token" => $userName]);
$this->assertEquals($opponents->getMessage(), 'Ok');
$opponentId = $opponents->getData()[0]->id;
$opponentName = $opponents->getData()[0]->name;
// invite
$response = $this->makeRequest('/combat/invite', ["token" => $userName, "id" => $opponentId]);
$this->assertEquals($response->getMessage(), 'Ok');
$combatId = $response->getData()->combatId;
$attackFirst = $response->getData()->youFirst;
// attack
$response = $this->makeRequest('/combat/attack', ["token" => $attackFirst ? $opponentName : $userName, "combatId" => $combatId, "skillId" => 2]);
$this->assertEquals($response->getMessage(), 'It is not your turn');
// attack
$response = $this->makeRequest('/combat/attack', ["token" => $attackFirst ? $userName : $opponentName, "combatId" => $combatId, "skillId" => 2]);
$this->assertEquals($response->getMessage(), 'Ok');
// attack
$response = $this->makeRequest('/combat/attack', ["token" => $attackFirst ? $opponentName : $userName, "combatId" => $combatId, "skillId" => 2]);
$this->assertEquals($response->getMessage(), 'This skill already used');
// collect
$response = $this->makeRequest('/combat/collect', ["token" => $attackFirst ? $opponentName : $userName, "combatId" => $combatId]);
$this->assertEquals($response->getMessage(), 'Combat is not finished');
// attack
$response = $this->makeRequest('/combat/attack', ["token" => $attackFirst ? $opponentName : $userName, "combatId" => $combatId, "skillId" => 1]);
$this->assertEquals($response->getMessage(), 'Ok');
// attack
$response = $this->makeRequest('/combat/attack', ["token" => $attackFirst ? $userName : $opponentName, "combatId" => $combatId, "skillId" => 3]);
$this->assertEquals($response->getMessage(), 'Ok');
// attack
$response = $this->makeRequest('/combat/attack', ["token" => $attackFirst ? $opponentName : $userName, "combatId" => $combatId, "skillId" => 1]);
$this->assertEquals($response->getMessage(), 'Combat was finished');
// collect
$response = $this->makeRequest('/combat/collect', ["token" => $opponentName, "combatId" => $combatId]);
// collect
$response = $this->makeRequest('/combat/collect', ["token" => $userName, "combatId" => $combatId]);
}
示例4: setUp
public function setUp()
{
parent::setUp();
$this->db('ORM')->purgeDatabase();
$this->client = $this->createAuthenticatedClient();
$this->searchManager = $this->client->getContainer()->get('massive_search.search_manager');
$this->entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
$this->createUser();
$this->indexProducts();
}
示例5: logIn
public function logIn(Client $client)
{
$session = $client->getContainer()->get('session');
$firewall = 'admin';
$token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_USER'));
$session->set('_security_' . $firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
}
示例6: addToCart
private function addToCart($mealName, $quantity, Crawler $crawler, Client $client)
{
$titles = $crawler->filter('h4')->reduce(function ($crawler) use($mealName) {
return false !== strpos($crawler->text(), $mealName);
});
if (count($titles) !== 1) {
throw new \RuntimeException(sprintf('Expected 1 title containing "%s", found %s.', $mealName, count($titles)));
}
$link = $titles->eq(0)->parents()->first()->filter('input[data-meal]');
$mealId = $link->attr('data-meal');
$client->request('POST', '/cart', array('meal' => $mealId, 'mode' => 'add', 'quantity' => $quantity));
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
示例7: authorizeClient
/**
* Create authorized client
*
* @link http://stackoverflow.com/questions/14957807/symfony2-tests-with-fosuserbundle/27223293#27223293
* @return Client
*/
public static function authorizeClient(Client $client)
{
$container = $client->getContainer();
$session = $container->get('session');
/** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
$userManager = $container->get('fos_user.user_manager');
/** @var $loginManager \FOS\UserBundle\Security\LoginManager */
$loginManager = $container->get('fos_user.security.login_manager');
$firewallName = $container->getParameter('fos_user.firewall_name');
/// @todo create fixture for this
$testUser = $container->getParameter('test_user');
$user = $userManager->findUserBy(array('username' => $testUser));
$loginManager->loginUser($firewallName, $user);
// save the login token into the session and put it in a cookie
$token = $container->get('security.context')->getToken();
$session->set('_security_' . $firewallName, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
return $client;
}
示例8: login
protected function login(Client $client, $username = 'admin', $password = 'password')
{
$client->restart();
$crawler = $client->request('GET', '/login');
$this->assertTrue($client->getResponse()->isSuccessful(), 'Response should be successful');
$form = $crawler->selectButton('Login')->form();
$client->submit($form, array('_username' => $username, '_password' => $password));
$this->assertTrue($client->getResponse()->isRedirect(), 'Response should be redirect');
$crawler = $client->followRedirect();
$this->assertGreaterThan(0, $crawler->filter('html:contains("Benvenuto")')->count());
}
示例9: authenticate
/**
* @return AuthenticateResponse
*/
public function authenticate()
{
$this->client->request('POST', self::SING_IN_URI, ['username' => $this->username, 'password' => $this->password], [], ['HTTP_ACCEPT' => 'application/json']);
$loginResponse = json_decode($this->client->getResponse()->getContent());
$authenticateHeaders = ['HTTP_TOKEN' => isset($loginResponse->Token) ? $loginResponse->Token : null, 'HTTP_EXPIREAT' => isset($loginResponse->ExpireAt) ? $loginResponse->ExpireAt : null, 'HTTP_USERNAME' => isset($loginResponse->Username) ? $loginResponse->Username : null];
return new AuthenticateResponse([], $authenticateHeaders);
}
示例10: getDocument
/**
* @param $uri
* @param $format
* @return \PHPExcel
*/
protected function getDocument($uri, $format = 'xlsx')
{
// generate source
static::$client->request('GET', $uri);
$source = static::$client->getResponse()->getContent();
// create source directory if necessary
if (!file_exists(__DIR__ . static::$TEMP_PATH)) {
mkdir(__DIR__ . static::$TEMP_PATH);
}
// save source
file_put_contents(__DIR__ . static::$TEMP_PATH . 'simple' . '.' . $format, $source);
// load source
switch ($format) {
case 'ods':
$reader = new PHPExcel_Reader_OOCalc();
break;
case 'xls':
$reader = new PHPExcel_Reader_Excel5();
break;
case 'xlsx':
$reader = new PHPExcel_Reader_Excel2007();
break;
default:
throw new InvalidArgumentException();
}
return $reader->load(__DIR__ . static::$TEMP_PATH . 'simple' . '.' . $format);
}
示例11: setCookiesFromOptions
protected function setCookiesFromOptions()
{
if (isset($this->config['cookies']) && is_array($this->config['cookies']) && !empty($this->config['cookies'])) {
$domain = parse_url($this->config['url'], PHP_URL_HOST);
$cookieJar = $this->client->getCookieJar();
foreach ($this->config['cookies'] as &$cookie) {
if (!is_array($cookie) || !array_key_exists('Name', $cookie) || !array_key_exists('Value', $cookie)) {
throw new \InvalidArgumentException('Cookies must have at least Name and Value attributes');
}
if (!isset($cookie['Domain'])) {
$cookie['Domain'] = $domain;
}
if (!isset($cookie['Expires'])) {
$cookie['Expires'] = null;
}
if (!isset($cookie['Path'])) {
$cookie['Path'] = '/';
}
if (!isset($cookie['Secure'])) {
$cookie['Secure'] = false;
}
if (!isset($cookie['HttpOnly'])) {
$cookie['HttpOnly'] = false;
}
$cookieJar->set(new \Symfony\Component\BrowserKit\Cookie($cookie['Name'], $cookie['Value'], $cookie['Expires'], $cookie['Path'], $cookie['Domain'], $cookie['Secure'], $cookie['HttpOnly']));
}
}
}
示例12: followRedirect
/**
* override to allow redirect history recording
*
* @see parent::followRedirect
*/
public function followRedirect()
{
if (!empty($this->redirect)) {
$this->redirect_list[] = $this->redirect;
}
//if
return parent::followRedirect();
}
示例13: seeResponseCodeIs
/**
* Checks response code.
*
* @param $num
*/
public function seeResponseCodeIs($num)
{
if (method_exists($this->client->getResponse(), 'getStatusCode')) {
\PHPUnit_Framework_Assert::assertEquals($num, $this->client->getResponse()->getStatusCode());
} else {
\PHPUnit_Framework_Assert::assertEquals($num, $this->client->getResponse()->getStatus());
}
}
示例14: __construct
/**
* Constructor.
*
* @param HttpKernelInterface $kernel An HttpKernel instance
* @param array $server The server parameters (equivalent of $_SERVER)
* @param History $history A History instance to store the browser history
* @param CookieJar $cookieJar A CookieJar instance to store the cookies
*/
public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
{
$this->kernel = $kernel;
parent::__construct($server, $history, $cookieJar);
$this->followRedirects = false;
}
示例15: testSavePerDiemAction
/**
* testing savePerDiem action.
*/
public function testSavePerDiemAction()
{
// Empty request
$crawler = $this->client->request('POST', '/secured/travel/admin/save/perdiem');
$this->assertJson($this->client->getResponse()->getContent(), 'testSavePerDiemAction: The response\'s content is not a JSON object.');
$this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'application/json'), 'testSavePerDiemAction: The content-type is not a json.');
// Filled up request
$crawler = $this->client->request('POST', '/secured/travel/admin/save/perdiem', array('perdiem' => array(0 => array('id' => 1, 'hours' => 14, 'amount' => 24), 1 => array('id' => null, 'hours' => 12, 'amount' => 12))));
$this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'text/html; charset=UTF-8'), 'testSavePerDiemAction: The content-type is not html.');
}