Browse Source

Clean up Collection::transpose().

Refs #8957
Mark Story 9 years ago
parent
commit
2f4f4338b7

+ 1 - 3
src/Collection/CollectionInterface.php

@@ -946,7 +946,6 @@ interface CollectionInterface extends Iterator, JsonSerializable
      */
     public function unwrap();
 
-
     /**
      * Transpose rows and columns into columns and rows
      *
@@ -969,10 +968,9 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * //     ['2013', '100', '200', '300'],
      * //     ['2014', '50', '100', '200'],
      * // ]
-     *
      * ```
      *
-     * @return Collection
+     * @return \Cake\Collection\CollectionInterface
      */
     public function transpose();
 }

+ 3 - 2
src/Collection/CollectionTrait.php

@@ -30,6 +30,7 @@ use Cake\Collection\Iterator\UnfoldIterator;
 use Cake\Collection\Iterator\ZipIterator;
 use Countable;
 use LimitIterator;
+use LogicException;
 use RecursiveIteratorIterator;
 use Traversable;
 
@@ -662,7 +663,7 @@ trait CollectionTrait
     /**
      * {@inheritDoc}
      *
-     * @return \Cake\Collection
+     * @return \Cake\Collection\CollectionInterface
      */
     public function transpose()
     {
@@ -671,7 +672,7 @@ trait CollectionTrait
         $result = [];
         foreach ($arrayValue as $column => $row) {
             if (count($row) != $length) {
-                throw new \LogicException('Child arrays do not have even length');
+                throw new LogicException('Child arrays do not have even length');
             }
             $result[] = array_column($arrayValue, $column);
         }

+ 37 - 0
tests/TestCase/Collection/CollectionTest.php

@@ -1602,4 +1602,41 @@ class CollectionTest extends TestCase
         $expected = [[1, 2], [3, [4, 5]], [6, [7, [8, 9], 10]], [11]];
         $this->assertEquals($expected, $chunked);
     }
+
+    public function testTranspose()
+    {
+        $collection = new Collection([
+            ['Products', '2012', '2013', '2014'],
+            ['Product A', '200', '100', '50'],
+            ['Product B', '300', '200', '100'],
+            ['Product C', '400', '300', '200'],
+        ]);
+        $transposed = $collection->transpose();
+        $expected = [
+            ['Products', 'Product A', 'Product B', 'Product C'],
+            ['2012', '200', '300', '400'],
+            ['2013', '100', '200', '300'],
+            ['2014', '50', '100', '200'],
+        ];
+
+        $this->assertEquals($expected, $transposed->toList());
+    }
+
+    /**
+     * Tests that provided arrays do not have even length
+     *
+     * @expectedException \LogicException
+     * @return void
+     */
+    public function testTransposeUnEvenLengthShouldThrowException()
+    {
+        $collection = new Collection([
+            ['Products', '2012', '2013', '2014'],
+            ['Product A', '200', '100', '50'],
+            ['Product B', '300'],
+            ['Product C', '400', '300'],
+        ]);
+
+        $collection->transpose();
+    }
 }

+ 0 - 60
tests/TestCase/Collection/Iterator/TransposeIteratorTest.php

@@ -1,60 +0,0 @@
-<?php
-/**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.3.0
- * @license       http://www.opensource.org/licenses/mit-license.php MIT License
- */
-
-namespace Cake\Test\TestCase\Collection\Iterator;
-
-use Cake\Collection\Collection;
-use Cake\TestSuite\TestCase;
-
-class TransposeIteratorTest extends TestCase
-{
-
-    public function testTranspose()
-    {
-        $collection = new Collection([
-            ['Products', '2012', '2013', '2014'],
-            ['Product A', '200', '100', '50'],
-            ['Product B', '300', '200', '100'],
-            ['Product C', '400', '300', '200'],
-        ]);
-        $transposed = $collection->transpose();
-        $expected = [
-            ['Products', 'Product A', 'Product B', 'Product C'],
-            ['2012', '200', '300', '400'],
-            ['2013', '100', '200', '300'],
-            ['2014', '50', '100', '200'],
-        ];
-
-        $this->assertEquals($expected, $transposed->toList());
-    }
-
-    /**
-     * Tests that provided arrays do not have even length
-     *
-     * @expectedException \LogicException
-     * @return void
-     */
-    public function testTransposeUnEvenLengthShouldThrowException()
-    {
-        $collection = new Collection([
-            ['Products', '2012', '2013', '2014'],
-            ['Product A', '200', '100', '50'],
-            ['Product B', '300'],
-            ['Product C', '400', '300'],
-        ]);
-
-        $collection->transpose();
-    }
-}