"Fossies" - the Fresh Open Source Software Archive

Member "Symfony/vendor/symfony/symfony/src/Symfony/Component/Form/Tests/ChoiceList/AbstractChoiceListTest.php" (30 Mar 2020, 5539 Bytes) of package /linux/www/Symfony_Standard_Vendors_3.4.39.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 /*
    4  * This file is part of the Symfony package.
    5  *
    6  * (c) Fabien Potencier <fabien@symfony.com>
    7  *
    8  * For the full copyright and license information, please view the LICENSE
    9  * file that was distributed with this source code.
   10  */
   11 
   12 namespace Symfony\Component\Form\Tests\ChoiceList;
   13 
   14 use PHPUnit\Framework\TestCase;
   15 
   16 /**
   17  * @author Bernhard Schussek <bschussek@gmail.com>
   18  */
   19 abstract class AbstractChoiceListTest extends TestCase
   20 {
   21     /**
   22      * @var \Symfony\Component\Form\ChoiceList\ChoiceListInterface
   23      */
   24     protected $list;
   25 
   26     /**
   27      * @var array
   28      */
   29     protected $choices;
   30 
   31     /**
   32      * @var array
   33      */
   34     protected $values;
   35 
   36     /**
   37      * @var array
   38      */
   39     protected $structuredValues;
   40 
   41     /**
   42      * @var array
   43      */
   44     protected $keys;
   45 
   46     /**
   47      * @var mixed
   48      */
   49     protected $choice1;
   50 
   51     /**
   52      * @var mixed
   53      */
   54     protected $choice2;
   55 
   56     /**
   57      * @var mixed
   58      */
   59     protected $choice3;
   60 
   61     /**
   62      * @var mixed
   63      */
   64     protected $choice4;
   65 
   66     /**
   67      * @var string
   68      */
   69     protected $value1;
   70 
   71     /**
   72      * @var string
   73      */
   74     protected $value2;
   75 
   76     /**
   77      * @var string
   78      */
   79     protected $value3;
   80 
   81     /**
   82      * @var string
   83      */
   84     protected $value4;
   85 
   86     /**
   87      * @var string
   88      */
   89     protected $key1;
   90 
   91     /**
   92      * @var string
   93      */
   94     protected $key2;
   95 
   96     /**
   97      * @var string
   98      */
   99     protected $key3;
  100 
  101     /**
  102      * @var string
  103      */
  104     protected $key4;
  105 
  106     protected function setUp()
  107     {
  108         parent::setUp();
  109 
  110         $this->list = $this->createChoiceList();
  111 
  112         $choices = $this->getChoices();
  113 
  114         $this->values = $this->getValues();
  115         $this->structuredValues = array_combine(array_keys($choices), $this->values);
  116         $this->choices = array_combine($this->values, $choices);
  117         $this->keys = array_combine($this->values, array_keys($choices));
  118 
  119         // allow access to the individual entries without relying on their indices
  120         reset($this->choices);
  121         reset($this->values);
  122         reset($this->keys);
  123 
  124         for ($i = 1; $i <= 4; ++$i) {
  125             $this->{'choice'.$i} = current($this->choices);
  126             $this->{'value'.$i} = current($this->values);
  127             $this->{'key'.$i} = current($this->keys);
  128 
  129             next($this->choices);
  130             next($this->values);
  131             next($this->keys);
  132         }
  133     }
  134 
  135     public function testGetChoices()
  136     {
  137         $this->assertSame($this->choices, $this->list->getChoices());
  138     }
  139 
  140     public function testGetValues()
  141     {
  142         $this->assertSame($this->values, $this->list->getValues());
  143     }
  144 
  145     public function testGetStructuredValues()
  146     {
  147         $this->assertSame($this->values, $this->list->getStructuredValues());
  148     }
  149 
  150     public function testGetOriginalKeys()
  151     {
  152         $this->assertSame($this->keys, $this->list->getOriginalKeys());
  153     }
  154 
  155     public function testGetChoicesForValues()
  156     {
  157         $values = [$this->value1, $this->value2];
  158         $this->assertSame([$this->choice1, $this->choice2], $this->list->getChoicesForValues($values));
  159     }
  160 
  161     public function testGetChoicesForValuesPreservesKeys()
  162     {
  163         $values = [5 => $this->value1, 8 => $this->value2];
  164         $this->assertSame([5 => $this->choice1, 8 => $this->choice2], $this->list->getChoicesForValues($values));
  165     }
  166 
  167     public function testGetChoicesForValuesPreservesOrder()
  168     {
  169         $values = [$this->value2, $this->value1];
  170         $this->assertSame([$this->choice2, $this->choice1], $this->list->getChoicesForValues($values));
  171     }
  172 
  173     public function testGetChoicesForValuesIgnoresNonExistingValues()
  174     {
  175         $values = [$this->value1, $this->value2, 'foobar'];
  176         $this->assertSame([$this->choice1, $this->choice2], $this->list->getChoicesForValues($values));
  177     }
  178 
  179     // https://github.com/symfony/symfony/issues/3446
  180     public function testGetChoicesForValuesEmpty()
  181     {
  182         $this->assertSame([], $this->list->getChoicesForValues([]));
  183     }
  184 
  185     public function testGetValuesForChoices()
  186     {
  187         $choices = [$this->choice1, $this->choice2];
  188         $this->assertSame([$this->value1, $this->value2], $this->list->getValuesForChoices($choices));
  189     }
  190 
  191     public function testGetValuesForChoicesPreservesKeys()
  192     {
  193         $choices = [5 => $this->choice1, 8 => $this->choice2];
  194         $this->assertSame([5 => $this->value1, 8 => $this->value2], $this->list->getValuesForChoices($choices));
  195     }
  196 
  197     public function testGetValuesForChoicesPreservesOrder()
  198     {
  199         $choices = [$this->choice2, $this->choice1];
  200         $this->assertSame([$this->value2, $this->value1], $this->list->getValuesForChoices($choices));
  201     }
  202 
  203     public function testGetValuesForChoicesIgnoresNonExistingChoices()
  204     {
  205         $choices = [$this->choice1, $this->choice2, 'foobar'];
  206         $this->assertSame([$this->value1, $this->value2], $this->list->getValuesForChoices($choices));
  207     }
  208 
  209     public function testGetValuesForChoicesEmpty()
  210     {
  211         $this->assertSame([], $this->list->getValuesForChoices([]));
  212     }
  213 
  214     public function testGetChoicesForValuesWithNull()
  215     {
  216         $values = $this->list->getValuesForChoices([null]);
  217 
  218         $this->assertNotEmpty($this->list->getChoicesForValues($values));
  219     }
  220 
  221     /**
  222      * @return \Symfony\Component\Form\ChoiceList\ChoiceListInterface
  223      */
  224     abstract protected function createChoiceList();
  225 
  226     abstract protected function getChoices();
  227 
  228     abstract protected function getValues();
  229 }