Tests/Services/DocumentServiceTest.php
author ymh <ymh.work@gmail.com>
Wed, 14 Dec 2011 23:28:57 +0100
changeset 63 774ba82dca59
parent 57 186c4121c7b3
child 65 ba6b8e38d90e
permissions -rw-r--r--
add tests and fixtures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Tests\Services;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
require_once(__DIR__ . "/../../../../../../../app/AppKernel.php");
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class DocumentServiceTest extends \PHPUnit_Framework_TestCase
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
{
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    protected $_container;
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    19
    protected $_doctrine;
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    20
    protected $_application;
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    21
    protected $_kernel;
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    public function __construct()
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    {
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    25
        $this->_kernel = new \AppKernel("test", true);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    26
        $this->_kernel->boot();
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    27
        $this->_container = $this->_kernel->getContainer();
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        parent::__construct();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    protected function get($service)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        return $this->_container->get($service);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    36
    protected function getDoctrine()
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    37
    {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    38
        if(is_null($this->_doctrine))
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    39
        {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    40
            $this->_doctrine = $this->get('doctrine');
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    41
        }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    42
        return $this->_doctrine;
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    43
    }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    44
    
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    45
    
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    46
    public function testGetTagLabels()
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    47
    {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    48
        $doc_service = $this->get("wiki_tag.document");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    49
        $doc1 = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 1");
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    51
        $tags = $doc_service->getTagLabels($doc1->getId());
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    52
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    53
        $this->assertEquals(4,count($tags));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    54
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    55
        $this->assertEquals(array("Tag1","Tag2","Tag3","Tag4"),$tags);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    56
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    57
    }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    58
    
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    public function testCopyTags()
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    62
        $doc_service = $this->get("wiki_tag.document");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    63
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    64
        $doc1 = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 1");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    65
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    66
        $this->assertEquals(4,count($doc_service->getTagLabels($doc1->getId())));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    67
                
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    68
        $doc2 = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 4");
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    70
        $this->assertEquals(0,count($doc_service->getTagLabels($doc2->getId())));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    71
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    72
        $doc_service->copyTags($doc1->getId(), $doc2->getId());
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    73
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    74
        $this->assertEquals(4,count($doc_service->getTagLabels($doc2->getId())));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    75
        $this->assertEquals(array("Tag1","Tag2","Tag3","Tag4"),$doc_service->getTagLabels($doc2->getId()));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    76
        
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    79
    public function setUp()
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    80
    {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    81
        $this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->_kernel);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    82
        $this->_application->setAutoExit(false);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    83
        $this->runConsole("doctrine:schema:drop", array("--force" => true));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    84
        $this->runConsole("wikitag:schema:create");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    85
        $this->runConsole("cache:warmup");
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    86
        $this->runConsole("doctrine:fixtures:load", array("--fixtures" => __DIR__ . "/../../../../../../../src/Company/BaseBundle/DataFixtures"));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    87
    }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    88
    
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    89
    protected function runConsole($command, Array $options = array())
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    90
    {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    91
        
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    92
        $options["-e"] = "test";
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    93
        $options["-q"] = null;
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    94
        $options = array_merge($options, array('command' => $command));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    95
        return $this->_application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    96
    }
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
}
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99