Development

Changeset 7107

You must first sign up to be able to contribute.

Changeset 7107

Show
Ignore:
Timestamp:
01/20/08 08:32:10 (7 months ago)
Author:
fabien
Message:

added full unit tests to sfEscapedViewParameterHolder and sfViewParameterHolder

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/view/sfEscapedViewParameterHolder.class.php

    r6723 r7107  
    6363   * 
    6464   * @return array An array of view parameters 
     65   * 
     66   * @throws InvalidArgumentException 
    6567   */ 
    6668  public function toArray() 
     
    7173    { 
    7274      case 'on': 
    73         $escapedData = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll()); 
    74         $attributes['sf_data'] = $escapedData; 
     75        $attributes['sf_data'] = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll()); 
    7576        break; 
    7677      case 'bc': 
    77         $escapedData = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll()); 
    7878        $attributes = $this->getAll(); 
    79         $attributes['sf_data'] = $escapedData
     79        $attributes['sf_data'] = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll())
    8080        break; 
    8181      case 'both': 
    82         $escapedData = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll()); 
    83         foreach ($escapedData as $key => $value) 
     82        $attributes['sf_data'] = sfOutputEscaper::escape($this->getEscapingMethod(), $this->getAll()); 
     83        foreach ($attributes['sf_data'] as $key => $value) 
    8484        { 
    8585          $attributes[$key] = $value; 
    8686        } 
    87         $attributes['sf_data'] = $escapedData; 
    8887        break; 
    8988      case 'off': 
    9089        $attributes = $this->getAll(); 
    9190        break; 
     91      default: 
     92        throw new InvalidArgumentException(sprintf('Unknown strategy "%s".', $this->getEscaping())); 
    9293    } 
    9394 
     
    108109 
    109110  /** 
    110    * Sets the escape caracter mode
     111   * Sets the escape character strategy
    111112   * 
    112113   * @param string Escape code 
     
    122123   * If the escaping method is empty, then that is returned. The default value 
    123124   * specified by the sub-class will be used. If the method does not exist (in 
    124    * the sense there is no define associated with the method) and exception is 
     125   * the sense there is no define associated with the method), an exception is 
    125126   * thrown. 
    126127   * 
    127128   * @return string The escaping method as the name of the function to use 
    128129   * 
    129    * @throws <b>sfConfigurationException</b> If the method does not exist 
     130   * @throws InvalidArgumentException If the method does not exist 
    130131   */ 
    131132  public function getEscapingMethod() 
     
    138139    if (!defined($this->escapingMethod)) 
    139140    { 
    140       throw new sfConfigurationException(sprintf('The escaping method "%s" is not available.', $this->escapingMethod)); 
     141      throw new InvalidArgumentException(sprintf('The escaping method "%s" is not available.', $this->escapingMethod)); 
    141142    } 
    142143 
  • branches/1.1/test/unit/view/sfEscapedViewParameterHolderTest.php

    r5018 r7107  
    1212require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1313 
    14 $t = new lime_test(10, new lime_output_color()); 
     14$t = new lime_test(27, new lime_output_color()); 
    1515 
    1616define('ESC_ENTITIES', 'esc_entities'); 
     
    6969$t->is($p->getEscapingMethod(), ESC_RAW, '->initialize() takes an array of options as its third argument'); 
    7070 
     71// ->isEscaped() 
     72$t->diag('->isEscaped()'); 
     73$t->is($p->isEscaped(), true, '->isEscaped() always returns true'); 
     74 
    7175// ->getEscaping() ->setEscaping() 
    7276$t->diag('->getEscaping() ->setEscaping()'); 
     
    8084$t->is($p->getEscapingMethod(), ESC_RAW, '->setEscapingMethod() changes the escaping method'); 
    8185 
     86$p->setEscapingMethod(''); 
     87$t->is($p->getEscapingMethod(), '', '->getEscapingMethod() returns an empty value if the method is empty'); 
     88 
     89try 
     90{ 
     91  $p->setEscapingMethod('nonexistant'); 
     92  $p->getEscapingMethod(); 
     93  $t->fail('->getEscapingMethod() throws an InvalidArgumentException if the escaping method does not exist'); 
     94} 
     95catch (InvalidArgumentException $e) 
     96{ 
     97  $t->pass('->getEscapingMethod() throws an InvalidArgumentException if the escaping method does not exist'); 
     98} 
     99 
    82100// ->toArray() 
    83101$t->diag('->toArray()'); 
     
    86104$t->is($a['foo'], 'bar', '->toArray() returns an array representation of the parameter holder'); 
    87105 
     106// escaping strategies 
     107$p = new sfEscapedViewParameterHolder(); 
     108$p->initialize(new sfEventDispatcher(), array('foo' => 'bar')); 
     109 
     110$t->diag('Escaping strategy to on'); 
     111$p->setEscaping('on'); 
     112$values = $p->toArray(); 
     113$t->is(count($values), 1, '->toArray() knows about the "on" strategy'); 
     114$t->is(count($values['sf_data']), 1, '->toArray() knows about the "on" strategy'); 
     115$t->is($values['sf_data']['foo'], '-ESCAPED-bar-ESCAPED-', '->toArray() knows about the "on" strategy'); 
     116 
     117try 
     118{ 
     119  $p->setEscaping('null'); 
     120  $p->toArray(); 
     121  $t->fail('->toArray() throws an InvalidArgumentException if the escaping strategy does not exist'); 
     122} 
     123catch (InvalidArgumentException $e) 
     124{ 
     125  $t->pass('->toArray() throws an InvalidArgumentException if the escaping strategy does not exist'); 
     126} 
     127 
     128$t->diag('Escaping strategy to bc'); 
     129$p->setEscaping('bc'); 
     130$values = $p->toArray(); 
     131$t->is(count($values), 2, '->toArray() knows about the "bc" strategy'); 
     132$t->is(count($values['sf_data']), 1, '->toArray() knows about the "bc" strategy'); 
     133$t->is($values['foo'], 'bar', '->toArray() knows about the "bc" strategy'); 
     134$t->is($values['sf_data']['foo'], '-ESCAPED-bar-ESCAPED-', '->toArray() knows about the "bc" strategy'); 
     135 
     136$t->diag('Escaping strategy to both'); 
     137$p->setEscaping('both'); 
     138$values = $p->toArray(); 
     139$t->is(count($values), 2, '->toArray() knows about the "both" strategy'); 
     140$t->is(count($values['sf_data']), 1, '->toArray() knows about the "both" strategy'); 
     141$t->is($values['foo'], '-ESCAPED-bar-ESCAPED-', '->toArray() knows about the "both" strategy'); 
     142$t->is($values['sf_data']['foo'], '-ESCAPED-bar-ESCAPED-', '->toArray() knows about the "both" strategy'); 
     143 
     144$t->diag('Escaping strategy to off'); 
     145$p->setEscaping('off'); 
     146$values = $p->toArray(); 
     147$t->is(count($values), 1, '->toArray() knows about the "off" strategy'); 
     148$t->is($values['foo'], 'bar', '->toArray() knows about the "off" strategy'); 
     149 
    88150// ->serialize() / ->unserialize() 
    89151$t->diag('->serialize() / ->unserialize()'); 
  • branches/1.1/test/unit/view/sfViewParameterHolderTest.php

    r5018 r7107  
    1212require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1313 
    14 $t = new lime_test(4, new lime_output_color()); 
     14$t = new lime_test(5, new lime_output_color()); 
    1515 
    1616class myRequest 
     
    4646$t->is($p->get('foo'), 'bar', '->initialize() takes an array of default parameters as its second argument'); 
    4747 
     48// ->isEscaped() 
     49$t->diag('->isEscaped()'); 
     50$t->is($p->isEscaped(), false, '->isEscaped() always returns false'); 
     51 
    4852// ->toArray() 
    4953$t->diag('->toArray()');