2011-09-06 20 views
13

का उपयोग कर Symfony2 मॉडल का परीक्षण कैसे करें I Symfony2 प्रोजेक्ट में एक मॉडल का परीक्षण करने का प्रयास कर रहा हूं, लेकिन मुझे नहीं पता कि इकाई प्रबंधक को रिकॉर्ड को सहेजने और पुनः प्राप्त करने के लिए कैसे प्राप्त किया जाए।PHPUnit

क्या कोई मुझे इस के लिए सही दस्तावेज़ों पर इंगित कर सकता है?

उत्तर

21

अपने मॉडल का परीक्षण करने के लिए, आप setUp() विधि का उपयोग कर सकते हैं। link to docs

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 

class MyModelTest extends WebTestCase 
{ 
    /** 
    * @var EntityManager 
    */ 
    private $_em; 

    protected function setUp() 
    { 
     $kernel = static::createKernel(); 
     $kernel->boot(); 
     $this->_em = $kernel->getContainer()->get('doctrine.orm.entity_manager'); 
     $this->_em->beginTransaction(); 
    } 

    /** 
    * Rollback changes. 
    */ 
    public function tearDown() 
    { 
     $this->_em->rollback(); 
    } 

    public function testSomething() 
    { 
     $user = $this->_em->getRepository('MyAppMyBundle:MyModel')->find(1); 
     } 

आशा यह आप

+0

यह मेरे लिए काम किया। धन्यवाद। –

+1

और यहां दस्तावेज़ों के लिए एक लिंक @ symfony.com है - http://symfony.com/doc/current/cookbook/testing/doctrine.html –

7

Symfony2 मॉडल डोमेन ऑब्जेक्ट्स होने की उम्मीद है जो कोड में domain models का प्रतिनिधित्व करते हैं।

डोमेन वस्तुओं संबंधित डोमेन अवधारणा के व्यापार व्यवहार को लागू करने के बजाय एक अधिक विशिष्ट प्रौद्योगिकी ढांचे की आवश्यकताओं द्वारा में परिभाषित किया जा विशुद्ध रूप से परिभाषित किया जाना चाहिए। - Domain-driven design - Wikipedia, the free encyclopedia

डोमेन वस्तुओं (और इसके परीक्षण) Symfony2 एपीआई और सिद्धांत एपीआई को छोड़कर तुम सच में खुद को परीक्षण करने के लिए चाहते हैं, तो इस पर निर्भर नहीं करना चाहिए।

लेखन सिम्फनी 2 यूनिट परीक्षण मानक PHPUnit इकाई परीक्षण लिखने से अलग नहीं है। - Symfony - Testing

आप व्यापार तर्क (प्रक्रियाओं, नियमों, व्यवहार, आदि) PHPUnit (या Behat) के साथ डोमेन वस्तुओं में प्रतिनिधित्व है और आमतौर पर test doubles परीक्षण कर सकते हैं।

+0

डॉक्स हाँ, मैं पढ़ लिया है का परीक्षण करें और उन अवधारणाओं को समझने के लिए। लेकिन अगर मैं परीक्षण करना चाहता हूं, उदाहरण के लिए, स्लोगेबल व्यवहार, रिकॉर्ड केवल सहेजे जाने के बाद ही काम करता है। मैं इसका परीक्षण कैसे कर सकता हूं? कभी भी बंडल का परीक्षण नहीं किया जाता है, मैं सिर्फ यह देखना चाहता हूं कि symfony2 – nerohc

+1

में चीजों का परीक्षण कैसे करें, यह जानना हमारे लिए महत्वपूर्ण है कि उत्पाद की मुख्य चिंताएं क्या हैं। यदि आप स्लग्जेबल व्यवहार विकसित कर रहे हैं, तो यह आपके लिए सबसे महत्वपूर्ण बात है कि स्लग स्ट्रिंग सही ढंग से स्लग्जेबल फ़ील्ड के मानों के आधार पर जेनरेट की जाती है, और इसलिए आप इसका परीक्षण करते हैं। बेशक आप जांच सकते हैं कि स्लग फ़ील्ड का मान डेटाबेस में ठीक से संग्रहीत किया गया है या नहीं, लेकिन यह उत्पाद की मुख्य चिंता नहीं है, और यह सिद्धांत की मुख्य चिंता है (और यह पहले से ही परीक्षण किया गया है)। – iteman

+1

हां, लेकिन स्लग को aftersave बनाया गया है, इसलिए जांचने के लिए कि जेनरेट की गई स्ट्रिंग ठीक है, तो मुझे ऑब्जेक्ट को सहेजने के लिए * की आवश्यकता है, स्वयं को बचाने के लिए नहीं (जिसे निश्चित रूप से डॉक्टर टीम द्वारा परीक्षण किया जाता है), लेकिन उत्पन्न स्ट्रिंग की जांच करने के लिए, जो सिस्टम की चिंता है। – nerohc

2
namespace Ibw\JobeetBundle\Tests\Repository; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
use Symfony\Bundle\FrameworkBundle\Console\Application; 
use Symfony\Component\Console\Output\NullOutput; 
use Symfony\Component\Console\Input\ArrayInput; 
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; 
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; 
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand; 

class CategoryRepositoryTest extends WebTestCase 
{ 
    private $em; 
    private $application; 

    public function setUp() 
    { 
     static::$kernel = static::createKernel(); 
     static::$kernel->boot(); 

     $this->application = new Application(static::$kernel); 

     // drop the database 
     $command = new DropDatabaseDoctrineCommand(); 
     $this->application->add($command); 
     $input = new ArrayInput(array(
      'command' => 'doctrine:database:drop', 
      '--force' => true 
     )); 
     $command->run($input, new NullOutput()); 

     // we have to close the connection after dropping the database so we don't get "No database selected" error 
     $connection = $this->application->getKernel()->getContainer()->get('doctrine')->getConnection(); 
     if ($connection->isConnected()) { 
      $connection->close(); 
     } 

     // create the database 
     $command = new CreateDatabaseDoctrineCommand(); 
     $this->application->add($command); 
     $input = new ArrayInput(array(
      'command' => 'doctrine:database:create', 
     )); 
     $command->run($input, new NullOutput()); 

     // create schema 
     $command = new CreateSchemaDoctrineCommand(); 
     $this->application->add($command); 
     $input = new ArrayInput(array(
      'command' => 'doctrine:schema:create', 
     )); 
     $command->run($input, new NullOutput()); 

     // get the Entity Manager 
     $this->em = static::$kernel->getContainer() 
      ->get('doctrine') 
      ->getManager(); 

     // load fixtures 
     $client = static::createClient(); 
     $loader = new \Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader($client->getContainer()); 
     $loader->loadFromDirectory(static::$kernel->locateResource('@IbwJobeetBundle/DataFixtures/ORM')); 
     $purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($this->em); 
     $executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($this->em, $purger); 
     $executor->execute($loader->getFixtures()); 
    } 

    public function testFunction() 
    { 
      // here you test save any object or test insert any object 
    } 

    protected function tearDown() 
    { 
     parent::tearDown(); 
     $this->em->close(); 
    } 
} 

में मदद करता है इस लिंक में की तरह: Jobeet Unit Test Tutorial समझाने इकाई और इकाई भंडार

+1

जबकि यह सैद्धांतिक रूप से प्रश्न का उत्तर दे सकता है, [यह बेहतर होगा] (http: //meta.stackexchange.com/q/8259) यहां उत्तर के आवश्यक हिस्सों को शामिल करने के लिए, और संदर्भ के लिए लिंक प्रदान करें। –