lithium\tests\cases\util\CollectionTest::testCollectionFormatConversion PHP Метод

testCollectionFormatConversion() публичный Метод

Tests that various types of handlers can be registered with Collection::formats(), and that collection instances are converted correctly.
    public function testCollectionFormatConversion()
    {
        Collection::formats('lithium\\net\\http\\Media');
        $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib'));
        $collection = new Collection(compact('data'));
        $expected = json_encode($data);
        $result = $collection->to('json');
        $this->assertEqual($expected, $result);
        $this->assertNull($collection->to('badness'));
        Collection::formats(false);
        $this->assertNull($collection->to('json'));
        Collection::formats('json', function ($collection, $options) {
            return json_encode($collection->to('array'));
        });
        $result = $collection->to('json');
        $this->assertEqual($expected, $result);
        $result = $collection->to(function ($collection) {
            $value = array_map(function ($i) {
                return is_array($i) ? join(',', $i) : $i;
            }, $collection->to('array'));
            return join(',', $value);
        });
        $expected = 'hello,goodbye,bar,dib';
        $this->assertEqual($expected, $result);
    }