Skip to content

Commit

Permalink
Merge pull request #721 from robbieaverill/feature/720-check-class-ex…
Browse files Browse the repository at this point in the history
…ists-on-lookup

Feature/720 check class exists on lookup
  • Loading branch information
ktomk committed Nov 2, 2015
2 parents f782736 + 0bcd03f commit 00da5f8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
2 changes: 2 additions & 0 deletions readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,8 @@ Resolve/Lookup Class Names
Resolves the given type and grouped class name to a class name, useful for debugging rewrites.
If the resolved class doesn't exist, an info message will be displayed.
.. code-block:: sh
$ n98-magerun.phar dev:class:lookup <block|model|helper> <name>
Expand Down
4 changes: 4 additions & 0 deletions src/N98/Magento/Command/Developer/ClassLookupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($this->initMagento()) {
$resolved = $this->_getConfig()->getGroupedClassName($input->getArgument('type'), $input->getArgument('name'));
$output->writeln(ucfirst($input->getArgument('type')) . ' <comment>' . $input->getArgument('name') . "</comment> resolves to <comment>" . $resolved . '</comment>');

if (!class_exists('\\' . $resolved)) {
$output->writeln('<info>Note:</info> Class <comment>' . $resolved . '</comment> does not exist!');
}
}
}
}
68 changes: 64 additions & 4 deletions tests/N98/Magento/Command/Developer/ClassLookupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@

class ClassLookupCommandTest extends TestCase
{
public function testExecute()
/**
* Test that the class lookup command resolves to the expected Magento class, and optionally
* whether it outputs a notice informing that the class doesn't exist
* @dataProvider classLookupProvider
*
* @param string $type Model, helper, block
* @param string $name Magento dev code
* @param string $expected Resolved class name
* @param bool $exists Whether the resolved class should exist
*/
public function testExecute($type, $name, $expected, $exists)
{
$application = $this->getApplication();
$application->add(new ClassLookupCommand());
Expand All @@ -17,11 +27,61 @@ public function testExecute()
$commandTester->execute(
array(
'command' => $command->getName(),
'type' => 'model',
'name' => 'catalog/product',
'type' => $type,
'name' => $name
)
);

$this->assertRegExp('/Mage_Catalog_Model_Product/', $commandTester->getDisplay());
$output = $commandTester->getDisplay();
$this->assertRegExp(sprintf('/%s/', $expected), $output);

$existsAssertion = (!$exists) ? 'assertRegExp' : 'assertNotRegExp';
$this->{$existsAssertion}(sprintf('/%s/', 'does not exist'), $output);
}

/**
* Provide data for the class lookup testExecute()
* @return array
*/
public function classLookupProvider()
{
return array(
array(
'type' => 'model',
'name' => 'catalog/product',
'expected' => 'Mage_Catalog_Model_Product',
'exists' => true
),
array(
'type' => 'model',
'name' => 'catalog/nothing_to_see_here',
'expected' => 'Mage_Catalog_Model_Nothing_To_See_Here',
'exists' => false
),
array(
'type' => 'helper',
'name' => 'checkout/cart',
'expected' => 'Mage_Checkout_Helper_Cart',
'exists' => true
),
array(
'type' => 'helper',
'name' => 'checkout/stolen_creditcards',
'expected' => 'Mage_Checkout_Helper_Stolen_Creditcards',
'exists' => false
),
array(
'type' => 'block',
'name' => 'customer/account_dashboard',
'expected' => 'Mage_Customer_Block_Account_Dashboard',
'exists' => true
),
array(
'type' => 'block',
'name' => 'customer/my_code_snippets',
'expected' => 'Mage_Customer_Block_My_Code_Snippets',
'exists' => false
)
);
}
}

0 comments on commit 00da5f8

Please sign in to comment.