$ph->add($parameters);
$t->is($ph->getAll(), $parameters, '->getAll() returns all parameters');
// ->has()
$t->diag('->has()');
$ph = new sfParameterHolder();
$ph->set('foo', 'bar');
$t->is($ph->has('foo'), true, '->has() returns true if the key exists');
$t->is($ph->has('bar'), false, '->has() returns false if the key does not exist');
$ph->set('bar', null);
$t->is($ph->has('bar'), true, '->has() returns true if the key exist, even if the value is null');
// ->remove()
$t->diag('->remove()');
$ph = new sfParameterHolder();
$ph->set('foo', 'bar');
$ph->set('myfoo', 'bar');
$ph->remove('foo');
$t->is($ph->has('foo'), false, '->remove() removes the key from parameters');
$ph->remove('myfoo');
$t->is($ph->has('myfoo'), false, '->remove() removes the key from parameters');
$t->is($ph->remove('nonexistant', 'foobar'), 'foobar', '->remove() takes a default value as its second argument');
$t->is($ph->getAll(), null, '->remove() removes the key from parameters');
// ->set()
$t->diag('->set()');
$foo = 'bar';
$ph = new sfParameterHolder();
$ph->set('foo', $foo);
$t->is($ph->get('foo'), $foo, '->set() sets the value for a key');
$foo = 'foo';
$t->is($ph->get('foo'), 'bar', '->set() sets the value for a key, not a reference');
// ->setByRef()
$t->diag('->setByRef()');
Beispiel #2
0
 /**
  * Simulates a click on a link or button.
  *
  * @param string $name       The link or button text
  * @param array  $arguments
  *
  * @return sfBrowser
  */
 public function click($name, $arguments = array())
 {
     $dom = $this->getResponseDom();
     if (!$dom) {
         throw new sfException('Cannot click because there is no current page in the browser.');
     }
     $xpath = new DomXpath($dom);
     // text link
     if ($link = $xpath->query(sprintf('//a[.="%s"]', $name))->item(0)) {
         return $this->get($link->getAttribute('href'));
     }
     // image link
     if ($link = $xpath->query(sprintf('//a/img[@alt="%s"]/ancestor::a', $name))->item(0)) {
         return $this->get($link->getAttribute('href'));
     }
     // form
     if (!($form = $xpath->query(sprintf('//input[((@type="submit" or @type="button") and @value="%s") or (@type="image" and @alt="%s")]/ancestor::form', $name, $name))->item(0))) {
         if (!($form = $xpath->query(sprintf('//button[.="%s" or @id="%s" or @name="%s"]/ancestor::form', $name, $name, $name))->item(0))) {
             throw new sfException(sprintf('Cannot find the "%s" link or button.', $name));
         }
     }
     // form attributes
     $url = $form->getAttribute('action');
     $method = $form->getAttribute('method') ? strtolower($form->getAttribute('method')) : 'get';
     // merge form default values and arguments
     $defaults = array();
     $arguments = sfToolkit::arrayDeepMerge($this->fields, $arguments);
     foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $form) as $element) {
         $elementName = $element->getAttribute('name');
         $nodeName = $element->nodeName;
         $value = null;
         if ($nodeName == 'input' && ($element->getAttribute('type') == 'checkbox' || $element->getAttribute('type') == 'radio')) {
             if ($element->getAttribute('checked')) {
                 $value = $element->getAttribute('value');
             }
         } else {
             if ($nodeName == 'input' && $element->getAttribute('type') == 'file') {
                 $ph = new sfParameterHolder();
                 $ph->add($arguments);
                 $filename = $ph->get($elementName, '');
                 if (is_readable($filename)) {
                     $fileError = UPLOAD_ERR_OK;
                     $fileSize = filesize($filename);
                 } else {
                     $fileError = UPLOAD_ERR_NO_FILE;
                     $fileSize = 0;
                 }
                 $ph->remove($elementName);
                 $arguments = $ph->getAll();
                 $this->parseArgumentAsArray($elementName, array('name' => basename($filename), 'type' => '', 'tmp_name' => $filename, 'error' => $fileError, 'size' => $fileSize), $this->files);
             } else {
                 if ($nodeName == 'input' && ($element->getAttribute('type') != 'submit' && $element->getAttribute('type') != 'button' || $element->getAttribute('value') == $name) && ($element->getAttribute('type') != 'image' || $element->getAttribute('alt') == $name)) {
                     $value = $element->getAttribute('value');
                 } else {
                     if ($nodeName == 'textarea') {
                         $value = '';
                         foreach ($element->childNodes as $el) {
                             $value .= $dom->saveXML($el);
                         }
                     } else {
                         if ($nodeName == 'select') {
                             if ($multiple = $element->hasAttribute('multiple')) {
                                 $elementName = str_replace('[]', '', $elementName);
                                 $value = array();
                             } else {
                                 $value = null;
                             }
                             $found = false;
                             foreach ($xpath->query('descendant::option', $element) as $option) {
                                 if ($option->getAttribute('selected')) {
                                     $found = true;
                                     if ($multiple) {
                                         $value[] = $option->getAttribute('value');
                                     } else {
                                         $value = $option->getAttribute('value');
                                     }
                                 }
                             }
                             // if no option is selected and if it is a simple select box, take the first option as the value
                             if (!$found && !$multiple) {
                                 $value = $xpath->query('descendant::option', $element)->item(0)->getAttribute('value');
                             }
                         }
                     }
                 }
             }
         }
         if (null !== $value) {
             $this->parseArgumentAsArray($elementName, $value, $defaults);
         }
     }
     // create request parameters
     $arguments = sfToolkit::arrayDeepMerge($defaults, $arguments);
     if ('post' == $method) {
         return $this->post($url, $arguments);
     } else {
         $query_string = http_build_query($arguments, null, '&');
         $sep = false === strpos($url, '?') ? '?' : '&';
         return $this->get($url . ($query_string ? $sep . $query_string : ''));
     }
 }
// ->add()
$t->diag('->add()');
$foo = 'bar';
$parameters = array('foo' => $foo, 'bar' => 'bar');
$myparameters = array('myfoo' => 'bar', 'mybar' => 'bar');
$ph = new sfParameterHolder();
$ph->add($parameters);
$t->is($ph->getAll(), $parameters, '->add() adds an array of parameters');
$foo = 'mybar';
$t->is($ph->getAll(), $parameters, '->add() adds an array of parameters, not a reference');
// ->addByRef()
$t->diag('->addByRef()');
$foo = 'bar';
$parameters = array('foo' => &$foo, 'bar' => 'bar');
$myparameters = array('myfoo' => 'bar', 'mybar' => 'bar');
$ph = new sfParameterHolder();
$ph->addByRef($parameters);
$t->is($parameters, $ph->getAll(), '->add() adds an array of parameters');
$foo = 'mybar';
$t->is($parameters, $ph->getAll(), '->add() adds a reference of an array of parameters');
// ->serialize() ->unserialize()
$t->diag('->serialize() ->unserialize()');
$t->ok($ph == unserialize(serialize($ph)), 'sfParameterHolder implements the Serializable interface');
// Array path as a key
$t->diag('Array path as a key');
$ph = new sfParameterHolder();
$ph->add(array('foo' => array('bar' => 'foo')));
$t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key');
$t->is($ph->get('foo[bar]'), 'foo', '->has() can takes a multi-array key');
$t->is($ph->remove('foo[bar]'), 'foo', '->remove() can takes a multi-array key');
$t->is($ph->getAll(), array('foo' => array()), '->remove() can takes a multi-array key');
$t->is($ph->get('foo[bars][]'), $ph->has('foo[bars]'), '->has() returns true for an array even if you omit the []');
// ->hasNamespace()
$t->diag('->hasNamespace()');
$ph = new sfParameterHolder();
$ph->set('foo', 'bar');
$ph->set('myfoo', 'bar', 'symfony/mynamespace');
$t->is($ph->hasNamespace($ph->getDefaultNamespace()), true, '->hasNamespace() returns true for the default namespace');
$t->is($ph->hasNamespace('symfony/mynamespace'), true, '->hasNamespace() returns true if the namespace exists');
$t->is($ph->hasNamespace('symfony/nonexistant'), false, '->hasNamespace() returns false if the namespace does not exist');
// ->remove()
$t->diag('->remove()');
$ph = new sfParameterHolder();
$ph->set('foo', 'bar');
$ph->set('myfoo', 'bar');
$ph->set('myfoo', 'bar', 'symfony/mynamespace');
$ph->remove('foo');
$t->is($ph->has('foo'), false, '->remove() removes the key from parameters');
$ph->remove('myfoo');
$t->is($ph->has('myfoo'), false, '->remove() removes the key from parameters');
$t->is($ph->has('myfoo', 'symfony/mynamespace'), true, '->remove() removes the key from parameters for a given namespace');
$ph->remove('myfoo', 'symfony/mynamespace');
$t->is($ph->has('myfoo', 'symfony/mynamespace'), false, '->remove() takes a namespace as its second argument');
$t->is($ph->getAll(), null, '->remove() removes the key from parameters');
// ->removeNamespace()
$t->diag('->removeNamespace()');
$ph = new sfParameterHolder();
$ph->set('foo', 'bar');
$ph->set('myfoo', 'bar');
$ph->set('myfoo', 'bar', 'symfony/mynamespace');
$ph->removeNamespace($ph->getDefaultNamespace());
$t->is($ph->has('foo'), false, '->removeNamespace() removes all keys and values from a namespace');