I've been trying to test a model in a Symfony2 project, but I don't know how to get the entity manager to save and retrive records.
Can anyone point me to the right docs for this?
How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.
PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.
With unit testing we test each component of the code individually. All components can be tested at least once. A major advantage of this approach is that it becomes easier to detect bugs early on. The small scope means it is easier to track down the cause of bugs when they occur.
PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.
In order to test your models, you can use setUp() method. 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);
}
Hope this helps you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With