src/Company/BaseBundle/DataFixtures/ORM/LoadDocumentData.php
author ymh <ymh.work@gmail.com>
Mon, 23 Jan 2012 00:47:51 +0100
changeset 64 97ddfdc8bdde
parent 61 9f427e7c88f9
permissions -rw-r--r--
improve search test and doc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
namespace Company\BaseBundle\DataFixures\ORM;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use Symfony\Component\DependencyInjection\ContainerInterface;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Doctrine\Common\DataFixtures\FixtureInterface;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Company\BaseBundle\Entity\Document;
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    16
use Company\BaseBundle\Entity\Category;
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
class LoadDocumentData implements FixtureInterface, ContainerAwareInterface
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
{
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    private $container;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    public function setContainer(ContainerInterface $container = null)
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        $this->container = $container;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    public function load($manager) {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    29
        # create new categories
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    30
        $cat_def_list = array('cat1' => null, 'cat2' => null, 'cat3'=> null);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    31
    
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    32
        foreach(array_keys($cat_def_list) as $cat_name) {
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    33
            $newcat = new Category();
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    34
            $newcat->setName($cat_name);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    35
            $manager->persist($newcat);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    36
            $cat_def_list[$cat_name] = $newcat;
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    37
        }
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    38
        
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        # create new document
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $doc_def_list = array(
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    41
            array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array_values($cat_def_list)),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    42
            array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    43
            array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'])),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    44
            array('title'=>'Title 4', 'description'=>'Description 4', 'tags' => array(), 'categories' => array()),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    45
            array('title'=>'Title 5', 'description'=>'Description 5', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    46
            array('title'=>'Title 10', 'description'=>'Description 10', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array()),
64
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    47
            array('title'=>'Title 11', 'description'=>'Description 11', 'tags' => array('tag4'), 'categories' => array($cat_def_list['cat2'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    48
            array('title'=>'Title 20', 'description'=>'Description 20', 'tags' => array('newtag1'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    49
            array('title'=>'Title 21', 'description'=>'Description 21', 'tags' => array('newtag2'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    50
            array('title'=>'Title 22', 'description'=>'Description 22', 'tags' => array('another'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    51
            array('title'=>'Title 23', 'description'=>'Description 23', 'tags' => array('hello world'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    52
            array('title'=>'Lorem ipsum dolor sit amet', 'description'=>'Morbi adipiscing luctus ullamcorper. Nam suscipit, turpis vel faucibus fringilla, sem leo fermentum nunc, et mattis leo urna sed tellus. Suspendisse consectetur turpis cursus ipsum ullamcorper gravida. Nullam arcu nisi, condimentum id condimentum non, lobortis nec lorem. Donec commodo, ligula sit amet posuere fermentum, urna elit faucibus nunc, et faucibus lorem erat quis urna. Vestibulum a quam eros. Suspendisse non felis a metus faucibus porta. Morbi adipiscing augue vel justo euismod non pulvinar sem posuere. Duis sit amet ipsum et quam cursus commodo eu a purus. Pellentesque gravida tempus libero, eu consectetur nisl posuere id.', 'tags' => array('foobar'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    53
            array('title'=>'Lorem ipsum dolor sit amet', 'description'=>'Lorem ipsum ullamcorper', 'tags' => array('barfoo'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    54
            array('title'=>'caveat', 'description'=>'emptor', 'tags' => array('hello world'), 'categories' => array($cat_def_list['cat3'])),
97ddfdc8bdde improve search test and doc
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
    55
            array('title'=>'emptor', 'description'=>'caveat', 'tags' => array('bonjour monde'), 'categories' => array($cat_def_list['cat3'])),
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        );
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    58
        
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    59
        $newdocs = array();
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    60
        
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        foreach ($doc_def_list as $doc_def) {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            $newdoc = new Document();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            $newdoc->setTitle($doc_def['title']);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            $newdoc->setDescription($doc_def['description']);
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    66
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    67
            foreach($doc_def['categories'] as $cat) {
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    68
                $newdoc->getCategories()->add($cat);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    69
            }
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    70
            
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            $manager->persist($newdoc);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    73
            $newdocs[] = array($newdoc, $doc_def['tags']);
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        $manager->flush();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    79
        foreach ($newdocs as $newdoc_array) {
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    80
            $newdoc = $newdoc_array[0];
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    81
            $tags = $newdoc_array[1];
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    82
            $this->container->get('wiki_tag.document')->addTags($newdoc->getId(), $tags);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    83
            $manager->flush();
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    84
        }
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    85
        
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    86
        $manager->flush();
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
 
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
}