Permalink
Browse files

Added few unit tests, fixed CS

  • Loading branch information...
willdurand committed Apr 20, 2012
1 parent ef891c9 commit dd13a0f40be2153225238f1e24c2862267e521c7
@@ -63,7 +63,7 @@
*
* @return string
*/
- static public function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
+ protected function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
{
$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$length = count(explode('\\', $bundle->getNamespace())) * (-1);
@@ -192,7 +192,7 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
$finalSchemas = $this->getFinalSchemas($kernel, $this->bundle);
foreach ($finalSchemas as $schema) {
list($bundle, $finalSchema) = $schema;
- $packagePrefix = self::getPackagePrefix($bundle, $base);
+ $packagePrefix = $this->getPackagePrefix($bundle, $base);
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
$this->tempSchemas[$tempSchema] = array(
@@ -281,6 +281,10 @@ protected function getSchemasFromBundle(BundleInterface $bundle)
return $finalSchemas;
}
+ /**
+ * @param \SplFileInfo $file
+ * @return string
+ */
protected function getRelativeFileName(\SplFileInfo $file)
{
return substr(str_replace(realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../'), '', $file), 1);
@@ -403,6 +407,14 @@ protected function getCacheDir()
return $this->cacheDir;
}
+ /**
+ * @return \Symfony\Component\Config\FileLocatorInterface
+ */
+ protected function getFileLocator()
+ {
+ return $this->getContainer()->get('file_locator');
+ }
+
/**
* Get connection by checking the input option named 'connection'.
* Returns the default connection if no option specified or an exception
@@ -521,18 +533,18 @@ protected function writeTaskError($output, $taskName, $more = true)
* @param OutputInterface $output The output.
* @param string $filename The filename.
*/
- protected function writeNewFile($output, $filename)
+ protected function writeNewFile(OutputInterface $output, $filename)
{
- return $output->writeln('>> <info>File+</info> ' . $filename);
+ $output->writeln('>> <info>File+</info> ' . $filename);
}
/**
* @param OutputInterface $output The output.
* @param string $directory The directory.
*/
- protected function writeNewDirectory($output, $directory)
+ protected function writeNewDirectory(OutputInterface $output, $directory)
{
- return $output->writeln('>> <info>Dir+</info> ' . $directory);
+ $output->writeln('>> <info>Dir+</info> ' . $directory);
}
/**
@@ -548,14 +560,11 @@ protected function askConfirmation(OutputInterface $output, $question, $default
}
/**
- * @return \Symfony\Component\Config\FileLocatorInterface
+ * @param \SplFileInfo $schema
+ * @param BundleInterface $bundle
+ * @return string
*/
- protected function getFileLocator()
- {
- return $this->getContainer()->get('file_locator');
- }
-
- private function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
+ protected function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{
$schemaPath = str_replace(
$bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR,
View
@@ -10,8 +10,6 @@
namespace Propel\PropelBundle\Command;
-require_once 'phing/Phing.php';
-
/**
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
@@ -75,10 +75,12 @@ public function explainAction($token, $connection, $query)
return new Response('<div class="error">This query cannot be explained.</div>');
}
- return $this->container->get('templating')->renderResponse('PropelBundle:Panel:explain.html.twig', array(
- 'data' => $results,
- 'query' => $query,
- ));
-
+ return $this->container->get('templating')->renderResponse(
+ 'PropelBundle:Panel:explain.html.twig',
+ array(
+ 'data' => $results,
+ 'query' => $query,
+ )
+ );
}
}
@@ -12,6 +12,9 @@
use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Command\AbstractPropelCommand;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\HttpKernel\Bundle\BundleInterface;
+use Symfony\Component\HttpKernel\KernelInterface;
/**
* @author William Durand <william.durand1@gmail.com>
@@ -35,12 +38,232 @@ public function testParseDbNameWithoutDbName()
{
$this->assertNull($this->command->parseDbName('foo'));
}
+
+ public function testTransformToLogicalName()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $bundle
+ ->expects($this->once())
+ ->method('getName')
+ ->will($this->returnValue('MySuperBundle'));
+ $bundle
+ ->expects($this->once())
+ ->method('getPath')
+ ->will($this->returnValue('/Users/foo/project/src/My/SuperBundle'));
+
+ $schema = $this
+ ->getMockBuilder('\SplFileInfo')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $schema
+ ->expects($this->once())
+ ->method('getRealPath')
+ ->will($this->returnValue('/Users/foo/project/src/My/SuperBundle/Resources/config/my-schema.xml'));
+
+ $expected = '@MySuperBundle/Resources/config/my-schema.xml';
+
+ $this->assertEquals($expected, $this->command->transformToLogicalName($schema, $bundle));
+ }
+
+ public function testTransformToLogicalNameWithSubDir()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $bundle
+ ->expects($this->once())
+ ->method('getName')
+ ->will($this->returnValue('MySuperBundle'));
+ $bundle
+ ->expects($this->once())
+ ->method('getPath')
+ ->will($this->returnValue('/Users/foo/project/src/My/SuperBundle'));
+
+ $schema = $this
+ ->getMockBuilder('\SplFileInfo')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $schema
+ ->expects($this->once())
+ ->method('getRealPath')
+ ->will($this->returnValue('/Users/foo/project/src/My/SuperBundle/Resources/config/propel/my-schema.xml'));
+
+ $expected = '@MySuperBundle/Resources/config/propel/my-schema.xml';
+
+ $this->assertEquals($expected, $this->command->transformToLogicalName($schema, $bundle));
+ }
+
+ public function testGetSchemasFromBundle()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $bundle
+ ->expects($this->once())
+ ->method('getName')
+ ->will($this->returnValue('MySuperBundle'));
+ $bundle
+ ->expects($this->exactly(2))
+ ->method('getPath')
+ ->will($this->returnValue(__DIR__ . '/../Fixtures/src/My/SuperBundle'));
+
+ $aSchema = realpath(__DIR__ . '/../Fixtures/src/My/SuperBundle/Resources/config/a-schema.xml');
+
+ // hack to by pass the file locator
+ $this->command->setLocateResponse($aSchema);
+
+ $schemas = $this->command->getSchemasFromBundle($bundle);
+
+ $this->assertNotNull($schemas);
+ $this->assertTrue(is_array($schemas));
+ $this->assertCount(1, $schemas);
+ $this->assertArrayHasKey($aSchema, $schemas);
+ $this->assertSame($bundle, $schemas[$aSchema][0]);
+ $this->assertEquals(new \SplFileInfo($aSchema), $schemas[$aSchema][1]);
+ }
+
+ public function testGetSchemasFromBundleWithNoSchema()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $bundle
+ ->expects($this->once())
+ ->method('getPath')
+ ->will($this->returnValue(__DIR__ . '/../Fixtures/src/My/SecondBundle'));
+
+ $schemas = $this->command->getSchemasFromBundle($bundle);
+
+ $this->assertNotNull($schemas);
+ $this->assertTrue(is_array($schemas));
+ $this->assertCount(0, $schemas);
+ }
+
+ public function testGetFinalSchemasWithNoSchemaInBundles()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
+
+ $bundle
+ ->expects($this->once())
+ ->method('getPath')
+ ->will($this->returnValue(__DIR__ . '/../Fixtures/src/My/SecondBundle'));
+
+ $kernel
+ ->expects($this->once())
+ ->method('getBundles')
+ ->will($this->returnValue(array($bundle)));
+
+ $schemas = $this->command->getFinalSchemas($kernel);
+
+ $this->assertNotNull($schemas);
+ $this->assertTrue(is_array($schemas));
+ $this->assertCount(0, $schemas);
+ }
+
+ public function testGetFinalSchemas()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
+
+ $bundle
+ ->expects($this->once())
+ ->method('getName')
+ ->will($this->returnValue('MySuperBundle'));
+ $bundle
+ ->expects($this->exactly(2))
+ ->method('getPath')
+ ->will($this->returnValue(__DIR__ . '/../Fixtures/src/My/SuperBundle'));
+
+ $aSchema = realpath(__DIR__ . '/../Fixtures/src/My/SuperBundle/Resources/config/a-schema.xml');
+
+ // hack to by pass the file locator
+ $this->command->setLocateResponse($aSchema);
+
+ $kernel
+ ->expects($this->once())
+ ->method('getBundles')
+ ->will($this->returnValue(array($bundle)));
+
+ $schemas = $this->command->getFinalSchemas($kernel);
+
+ $this->assertNotNull($schemas);
+ $this->assertTrue(is_array($schemas));
+ $this->assertCount(1, $schemas);
+ $this->assertArrayHasKey($aSchema, $schemas);
+ $this->assertSame($bundle, $schemas[$aSchema][0]);
+ $this->assertEquals(new \SplFileInfo($aSchema), $schemas[$aSchema][1]);
+ }
+
+ public function testGetFinalSchemasWithGivenBundle()
+ {
+ $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+ $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
+
+ $bundle
+ ->expects($this->once())
+ ->method('getName')
+ ->will($this->returnValue('MySuperBundle'));
+ $bundle
+ ->expects($this->exactly(2))
+ ->method('getPath')
+ ->will($this->returnValue(__DIR__ . '/../Fixtures/src/My/SuperBundle'));
+
+ $aSchema = realpath(__DIR__ . '/../Fixtures/src/My/SuperBundle/Resources/config/a-schema.xml');
+
+ // hack to by pass the file locator
+ $this->command->setLocateResponse($aSchema);
+
+ $kernel
+ ->expects($this->never())
+ ->method('getBundles');
+
+ $schemas = $this->command->getFinalSchemas($kernel, $bundle);
+
+ $this->assertNotNull($schemas);
+ $this->assertTrue(is_array($schemas));
+ $this->assertCount(1, $schemas);
+ $this->assertArrayHasKey($aSchema, $schemas);
+ $this->assertSame($bundle, $schemas[$aSchema][0]);
+ $this->assertEquals(new \SplFileInfo($aSchema), $schemas[$aSchema][1]);
+ }
}
class TestableAbstractPropelCommand extends AbstractPropelCommand
{
+ private $locate;
+
+ public function setLocateResponse($locate)
+ {
+ $this->locate = $locate;
+ }
+
+ public function getContainer()
+ {
+ return $this;
+ }
+
+ public function get($service)
+ {
+ return $this;
+ }
+
+ public function locate($file)
+ {
+ return $this->locate;
+ }
+
public function parseDbName($dsn)
{
return parent::parseDbName($dsn);
}
+
+ public function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
+ {
+ return parent::transformToLogicalName($schema, $bundle);
+ }
+
+ public function getSchemasFromBundle(BundleInterface $bundle)
+ {
+ return parent::getSchemasFromBundle($bundle);
+ }
+
+ public function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null)
+ {
+ return parent::getFinalSchemas($kernel, $bundle);
+ }
}
@@ -0,0 +1 @@
+This is a schema.xml

0 comments on commit dd13a0f

Please sign in to comment.