1. Go to this page and download the library: Download cosma/testing-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cosma / testing-bundle example snippets
# app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
// ...
$bundles[] = new h4cc\AliceFixturesBundle\h4ccAliceFixturesBundle();
$bundles[] = new Cosma\Bundle\TestingBundle\TestingBundle();
}
}
use Cosma\Bundle\TestingBundle\TestCase\SimpleTestCase;
class SomeVerySimpleUnitTest extends SimpleTestCase
{
public function testSomething()
{
$mockedUserFullNamespace = $this->getMockedEntityWithId('Acme\AppBundle\Entity\User', 1);
$mockedUserBundleNamespace = $this->getMockedEntityWithId('AppBundle:User', 2);
$userFullNamespace = $this->getEntityWithId('Acme\AppBundle\Entity\User', 3);
$userBundleNamespace = $this->getEntityWithId('AppBundle:User', 4);
$thisTestClassPath = $this->getTestClassPath();
}
}
use Cosma\Bundle\TestingBundle\TestCase\WebTestCase;
class SomeWebFunctionalTest extends WebTestCase
{
public function setUp()
{
/**
* Required call that boots the Symfony kernel
*/
parent::setUp();
}
public function testSomething()
{
$kernel = $this->getKernel();
$container = $this->getContainer();
// Client for functional tests. Emulates a browser
$client = $this->getClient();
}
}
use Cosma\Bundle\TestingBundle\TestCase\DBTestCase;
class SomeFunctionalWebDBTest extends WebTestCase
{
public function setUp()
{
/**
* Required call that boots the Symfony kernel
*/
parent::setUp();
/**
* drops database tables before every test.
* has two strategies set by parameter cosma_testing.doctrine.cleaning_strategy:
* 1. truncate (default, faster)
* 2. drop (actual drop, slower)
*/
$this->dropDatabase();
/**
* 1. Truncates the tables user and group(default behaviour)
* 2. Loads two fixtures files located in src/AppBundle/Fixture/Table/User.yml and src/AnotherBundle/Fixture/Table/Group.yml
*
*/
$this->loadFixtures(
[
'AppBundle:Table:User',
'AnotherBundle:Table:Group'
]
);
/**
* Loads a fixtures file located in src/SomeBundle/Fixture/SomeDirectory/Book.yml
* Doesn't truncate the table
*/
$this->loadFixtures(
[
'SomeBundle:SomeDirectory:Book'
],
false
);
}
public function testSomething()
{
/**
* Fixtures can be load inside a test, too.
*/
$this->loadFixtures(['SomeBundle:SomeDirectory:Author']);
$entityManager = $this->getEntityManager();
$entityRepository = $this->getEntityRepository('AppBundle:User');
$fixtureManager = $this->getFixtureManager();
}
}
use Cosma\Bundle\TestingBundle\TestCase\SolrTestCase;
class SomeSolrTest extends SolrTestCase
{
public function setUp()
{
/**
* Required call that boots the Symfony kernel and truncate default test Solr core
*/
parent::setUp();
}
public function testIndex()
{
$solariumClient = $this->getSolariumClient();
/**
* get an update query instance
*/
$update = $solariumClient->createUpdate();
/**
* first fixture document
*/
$documentOne = $update->createDocument();
$documentOne->id = 123;
$documentOne->name = 'testdoc-1';
$documentOne->price = 364;
/**
* second fixture document
*/
$documentTwo = $update->createDocument();
$documentTwo->id = 124;
$documentTwo->name = 'testdoc-2';
$documentTwo->price = 340;
/**
* add the documents and a commit command to the update query
*/
$update->addDocuments([$documentOne, $documentTwo]);
$update->addCommit();
/**
* execute query
*/
$solariumClient->update($update);
}
}
class SomeElasticTest extends ElasticTestCase
{
public function setUp()
{
/**
* Required call that boots the Symfony kernel and recreates default test elastic index
*/
parent::setUp();
}
public function testSomethingElastic()
{
// get default Elastica client
$elasticClient = $this->getElasticClient();
// get default index - test
$elasticIndex = $this->getElasticIndex();
// create another index
$anotherElasticIndex = $elasticClient->getIndex('another_index');
$anotherElasticIndex->create([], true);
//Create a type
/** @type \Elastica\Type $type **/
$type = $this->getElasticIndex()->getType('type');
// index documents
$type->addDocument(
new \Elastica\Document(1, ['username' => 'someUser'])
);
$type->addDocument(
new \Elastica\Document(2, ['username' => 'anotherUser'])
);
$type->addDocument(
new \Elastica\Document(3, ['username' => 'someotherUser'])
);
$elasticIndex->refresh();
//query for documents
$query = array(
'query' => array(
'query_string' => array(
'query' => '*User',
)
)
);
$path = $elasticIndex->getName() . '/' . $type->getName() . '/_search';
$response = $elasticClient->request($path, Request::GET, $query);
$responseArray = $response->getData();
$this->assertEquals(3, $responseArray['hits']['total']);
}
}
use Cosma\Bundle\TestingBundle\TestCase\SeleniumTestCase;
class SomeSeleniumTest extends SeleniumTestCase
{
public function setUp()
{
/**
* Required call that boots the Symfony kernel and initialize selenium remote web driver
*/
parent::setUp();
}
/**
* read title from google site
*/
public function testGoogleTitle()
{
$remoteWebDriver = $this->getRemoteWebDriver();
$domain = $this->getTestDomain();
// open http url http://testdomain/somepage.html
$webDriver = $this->open('/somepage.html');
$this->assertContains('Some Title', $webDriver->getTitle());
// open https url https://testdomain/securePage.html
$webDriver = $this->openSecure('/securePage.html');
$this->assertContains('Some Title', $webDriver->getTitle());
}
}
use Cosma\Bundle\TestingBundle\TestCase\RedisTestCase;
class SomeSeleniumTest extends RedisTestCase
{
public function setUp()
{
/**
* Required call that boots the Symfony kernel and initialize selenium remote web driver
*/
parent::setUp();
}
/**
* read title from google site
*/
public function testGoogleTitle()
{
$redisClient = $this->getRedisClient();
$redisClient->set('key' , 'value');
}
}
namespace Acme\AppBundle\TestCase;
use Cosma\Bundle\TestingBundle\TestCase\WebTestCase;
// add the rest of traits
use Cosma\Bundle\TestingBundle\TestCase\Traits\DBTrait;
use Cosma\Bundle\TestingBundle\TestCase\Traits\ElasticTrait;
use Cosma\Bundle\TestingBundle\TestCase\Traits\SeleniumTrait;
use Cosma\Bundle\TestingBundle\TestCase\Traits\RedisTrait;
abstract class ComposedTestCase extends WebTestCase
{
/**
* This Test Case combines: DB, Elastic and Selenium Test Cases
*/
use DBTrait;
use ElasticTrait;
use SeleniumTrait;
use RedisTrait;
public function setUp()
{
parent::setUp();
$this->getFixtureManager(); // from DBTrait
$this->recreateIndex(); // from ElasticTrait
$this->getRemoteWebDriver(); // from SeleniumTrait
$this->resetRedisDatabase(); // from RedisTrait
}
}
use Cosma\Bundle\TestingBundle\TestCase\SimpleTestCase;
/**
* Will retry 10 times all the Class tests that are failing
*
* @retry 10
*/
class SomeVerySimpleUnitTest extends SimpleTestCase
{
/**
* Will retry 10 times this test if is failing because of the class annotation from above
*/
public function testFirst()
{
// ...
}
/**
* Will retry 4 times this test if is failing because of the method annotation from below
*
* @retry 4
*/
public function testSecond()
{
// ...
}
}
use Cosma\Bundle\TestingBundle\TestCase\SimpleTestCase;
class SomeUnitTest extends SimpleTestCase
{
public function testGetsAverageTemperatureFromThreeServiceReadings()
{
$service = \Mockery::mock('service');
$service->shouldReceive('readTemp')->times(3)->andReturn(10, 12, 14);
$temperature = new Temperature($service);
$this->assertEquals(12, $temperature->average());
}
}
bash
$ php composer.phar
bash
# Argument :: file - sole cosma_testing:generate:test /path/to/file/containing/classes_or_traits.php
bash
# Argument :: dump directory - ified will save all entities : default *
# Option :: --associations / -a - saves the associations between entities, too
$ php app/console cosma_testing:fixtures:export [-a|--associations] dumpDirectory [entity]
$ php app/console cosma_testing:fixtures:export -a "path/to/dump/directory" BundleName:Entity
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.