Пример #1
0
 public function testCategorizeByKeyArrayItems()
 {
     $this->assertCount(0, ObjectUtils::categorizeByKey($this->_provideCollection(0, '_provideArray'), 'id'));
     $this->assertCount(5, ObjectUtils::categorizeByKey($this->_provideCollection(5, '_provideArray'), 'id'));
     $this->assertCount(5, ObjectUtils::categorizeByKey($this->_provideCollection(123, '_provideArray'), 'categoryId'));
     $this->assertCount(0, ObjectUtils::categorizeByKey($this->_provideCollection(123), '_provideArray', 'undefined'));
     $namedCollection = ObjectUtils::keyExplode($this->_provideCollection(123, '_provideArray'), 'name');
     $byCategoryId = ObjectUtils::categorizeByKey($namedCollection, 'categoryId');
     $this->assertCount(5, $byCategoryId);
     $this->assertArrayHasKey(3, $byCategoryId);
     $this->assertArrayNotHasKey('item.5', $byCategoryId[1]);
     // it should be here (if preserved keys), but let's test all just in case
     $this->assertArrayNotHasKey('item.5', $byCategoryId[2]);
     $this->assertArrayNotHasKey('item.5', $byCategoryId[3]);
     $this->assertArrayNotHasKey('item.5', $byCategoryId[4]);
     $this->assertArrayNotHasKey('item.5', $byCategoryId[5]);
     $byCategoryIdNamed = ObjectUtils::categorizeByKey($namedCollection, 'categoryId', true);
     $this->assertCount(5, $byCategoryIdNamed);
     $this->assertArrayHasKey(3, $byCategoryIdNamed);
     $this->assertArrayHasKey('item.5', $byCategoryIdNamed[1]);
 }