Пример #1
0
 public function testDefaultPropertyComparator()
 {
     $list = new ArrayList();
     $list->add(PhpProperty::create('arr')->setVisibility(PhpProperty::VISIBILITY_PRIVATE));
     $list->add(PhpProperty::create('bar')->setVisibility(PhpProperty::VISIBILITY_PROTECTED));
     $list->add(PhpProperty::create('foo'));
     $list->add(PhpProperty::create('baz'));
     $list->sort(new DefaultPropertyComparator());
     $ordered = $list->map(function ($item) {
         return $item->getName();
     })->toArray();
     $this->assertEquals(['baz', 'foo', 'bar', 'arr'], $ordered);
 }
Пример #2
0
 public function testSortAndReverse()
 {
     $unsorted = [5, 2, 8, 3, 9, 4, 6, 1, 7, 10];
     $list = new ArrayList($unsorted);
     $this->assertEquals([10, 7, 1, 6, 4, 9, 3, 8, 2, 5], $list->reverse()->toArray());
     $this->assertEquals(range(1, 10), $list->sort()->toArray());
     $list = new ArrayList($unsorted);
     $this->assertEquals(range(10, 1), $list->reverseSort()->toArray());
     $list = new ArrayList($unsorted);
     $cmp = function ($a, $b) {
         if ($a == $b) {
             return 0;
         }
         return $a < $b ? -1 : 1;
     };
     $this->assertEquals(range(1, 10), $list->sort($cmp)->toArray());
     $items = ['x', 'c', 'a', 't', 'm'];
     $list = new ArrayList();
     foreach ($items as $item) {
         $list->add(new Item($item));
     }
     $list->sort(new ComparableComparator());
     $this->assertEquals(['a', 'c', 'm', 't', 'x'], $list->map(function ($item) {
         return $item->getContent();
     })->toArray());
 }
 public function testComparison()
 {
     $list = new ArrayList(['author', 'since', 'see', 'param', 'author']);
     $list->sort(new TagNameComparator());
     $this->assertEquals(['see', 'author', 'author', 'since', 'param'], $list->toArray());
 }