Skip to content

Commit

Permalink
Added few unit tests, fixed CS
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Apr 20, 2012
1 parent ef891c9 commit dd13a0f
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 20 deletions.
35 changes: 22 additions & 13 deletions Command/AbstractPropelCommand.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ abstract class AbstractPropelCommand extends ContainerAwareCommand
* *
* @return string * @return string
*/ */
static public function getPackagePrefix(Bundle $bundle, $baseDirectory = '') protected function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
{ {
$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath())); $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$length = count(explode('\\', $bundle->getNamespace())) * (-1); $length = count(explode('\\', $bundle->getNamespace())) * (-1);
Expand Down Expand Up @@ -192,7 +192,7 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
$finalSchemas = $this->getFinalSchemas($kernel, $this->bundle); $finalSchemas = $this->getFinalSchemas($kernel, $this->bundle);
foreach ($finalSchemas as $schema) { foreach ($finalSchemas as $schema) {
list($bundle, $finalSchema) = $schema; list($bundle, $finalSchema) = $schema;
$packagePrefix = self::getPackagePrefix($bundle, $base); $packagePrefix = $this->getPackagePrefix($bundle, $base);


$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName(); $tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
$this->tempSchemas[$tempSchema] = array( $this->tempSchemas[$tempSchema] = array(
Expand Down Expand Up @@ -281,6 +281,10 @@ protected function getSchemasFromBundle(BundleInterface $bundle)
return $finalSchemas; return $finalSchemas;
} }


/**
* @param \SplFileInfo $file
* @return string
*/
protected function getRelativeFileName(\SplFileInfo $file) protected function getRelativeFileName(\SplFileInfo $file)
{ {
return substr(str_replace(realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../'), '', $file), 1); return substr(str_replace(realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../'), '', $file), 1);
Expand Down Expand Up @@ -403,6 +407,14 @@ protected function getCacheDir()
return $this->cacheDir; 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'. * Get connection by checking the input option named 'connection'.
* Returns the default connection if no option specified or an exception * Returns the default connection if no option specified or an exception
Expand Down Expand Up @@ -521,18 +533,18 @@ protected function writeTaskError($output, $taskName, $more = true)
* @param OutputInterface $output The output. * @param OutputInterface $output The output.
* @param string $filename The filename. * @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 OutputInterface $output The output.
* @param string $directory The directory. * @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);
} }


/** /**
Expand All @@ -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() protected function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{
return $this->getContainer()->get('file_locator');
}

private function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{ {
$schemaPath = str_replace( $schemaPath = str_replace(
$bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR, $bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR,
Expand Down
2 changes: 0 additions & 2 deletions Command/Phing.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@


namespace Propel\PropelBundle\Command; namespace Propel\PropelBundle\Command;


require_once 'phing/Phing.php';

/** /**
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
Expand Down
12 changes: 7 additions & 5 deletions Controller/PanelController.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public function explainAction($token, $connection, $query)
return new Response('<div class="error">This query cannot be explained.</div>'); return new Response('<div class="error">This query cannot be explained.</div>');
} }


return $this->container->get('templating')->renderResponse('PropelBundle:Panel:explain.html.twig', array( return $this->container->get('templating')->renderResponse(
'data' => $results, 'PropelBundle:Panel:explain.html.twig',
'query' => $query, array(
)); 'data' => $results,

'query' => $query,
)
);
} }
} }
223 changes: 223 additions & 0 deletions Tests/Command/AbstractPropelCommandTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@


use Propel\PropelBundle\Tests\TestCase; use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Command\AbstractPropelCommand; 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> * @author William Durand <william.durand1@gmail.com>
Expand All @@ -35,12 +38,232 @@ public function testParseDbNameWithoutDbName()
{ {
$this->assertNull($this->command->parseDbName('foo')); $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 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) public function parseDbName($dsn)
{ {
return parent::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);
}
} }
Empty file.
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
This is a schema.xml

0 comments on commit dd13a0f

Please sign in to comment.