Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify PHP unit expectations #11675

Merged
merged 2 commits into from Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests-legacy/Integration/Core/Module/HookRepositoryTest.php
Expand Up @@ -93,8 +93,8 @@ public function test_only_display_hooks_are_retrieved()
$actual['displayTestHookName']
);

$this->assertFalse(
array_key_exists('notADisplayTestHookName', $actual)
$this->assertArrayNotHasKey(
'notADisplayTestHookName', $actual
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests-legacy/Integration/ProductURLsTest.php
Expand Up @@ -115,6 +115,6 @@ public function test_url_ignores_variant_if_not_specified__without_url_rewriting
parse_str($this->getURL(1, null)['query'], $query);

$this->assertEquals(1, $query['id_product']);
$this->assertTrue(empty($query['id_product_attribute']));
$this->assertEmpty($query['id_product_attribute']);
}
}
Expand Up @@ -70,7 +70,7 @@ public function setUp()

public function testCreateCatalogueWithoutProviderFails()
{
$this->setExpectedException('PrestaShopBundle\Translation\Factory\ProviderNotFoundException');
$this->expectException('PrestaShopBundle\Translation\Factory\ProviderNotFoundException');
$expected = $this->factory
->createCatalogue($this->providerMock->getIdentifier())
;
Expand All @@ -89,7 +89,7 @@ public function testCreateCatalogueWithProvider()

public function testCreateTranslationsArrayWithoutProviderFails()
{
$this->setExpectedException('PrestaShopBundle\Translation\Factory\ProviderNotFoundException');
$this->expectException('PrestaShopBundle\Translation\Factory\ProviderNotFoundException');
$expected = $this->factory
->createTranslationsArray($this->providerMock->getIdentifier())
;
Expand Down
Expand Up @@ -152,7 +152,8 @@ public function testFilesExceptionMissingSource()
$filepath = $this->defaultDir.'/moduleConfCrashFileSource.yml';
$name = 'bankwire';

$this->setExpectedException('Exception', 'Missing source file');
$this->expectException('Exception');
$this->expectExceptionMessage('Missing source file');
$this->getModuleSelfConfigurator()->module($name)->file($filepath)->configure();
}

Expand All @@ -161,7 +162,8 @@ public function testFilesExceptionMissingDestination()
$filepath = $this->defaultDir.'/moduleConfCrashFileDestination.yml';
$name = 'bankwire';

$this->setExpectedException('Exception', 'Missing destination file');
$this->expectException('Exception');
$this->expectExceptionMessage('Missing destination file');
$this->getModuleSelfConfigurator()->module($name)->file($filepath)->configure();
}

Expand Down Expand Up @@ -223,7 +225,8 @@ public function testSqlExceptionMissingFile()
$filepath = $this->defaultDir.'/moduleConfCrashSql.yml';
$name = 'bankwire';

$this->setExpectedException('Exception', 'Missing file path');
$this->expectException('Exception');
$this->expectExceptionMessage('Missing file path');
$this->getModuleSelfConfigurator()->module($name)->file($filepath)->configure();
}

Expand Down
Expand Up @@ -161,8 +161,8 @@ public function testModuleHasPico()
$testedModule = $this->modules['module-verified-from-addons-api'];
$presentedModule = $this->modulePresenter->present($testedModule);

$this->assertTrue(array_key_exists('picos', $presentedModule['attributes']));
$this->assertFalse(empty($presentedModule['attributes']['picos']));
$this->assertArrayHasKey('picos', $presentedModule['attributes']);
$this->assertNotEmpty($presentedModule['attributes']['picos']);
}

/**
Expand All @@ -177,8 +177,8 @@ public function testModuleHasNotPico()
$testedModule = $this->modules['module-under-dev'];
$presentedModule = $this->modulePresenter->present($testedModule);

$this->assertTrue(array_key_exists('picos', $presentedModule['attributes']));
$this->assertTrue(empty($presentedModule['attributes']['picos']));
$this->assertArrayHasKey('picos', $presentedModule['attributes']);
$this->assertEmpty($presentedModule['attributes']['picos']);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests-legacy/Unit/Classes/PrestaShopAutoloadTest.php
Expand Up @@ -44,7 +44,7 @@ protected function setUp()

public function testGenerateIndex()
{
$this->assertTrue(file_exists($this->file_index));
$this->assertFileExists($this->file_index);
$data = include($this->file_index);
$this->assertEquals($data['OrderControllerCore']['path'], 'controllers/front/OrderController.php');
}
Expand All @@ -71,7 +71,7 @@ public function testGenerateIndexWithoutOverride()
class Connection extends ConnectionCore {
}');
PrestaShopAutoload::getInstance()->generateIndex();
$this->assertTrue(file_exists($this->file_index));
$this->assertFileExists($this->file_index);
$data = include($this->file_index);
$this->assertEquals($data['OrderControllerCore']['path'], 'controllers/front/OrderController.php');
$this->assertEquals($data['Connection']['override'], false);
Expand Down
2 changes: 1 addition & 1 deletion tests-legacy/Unit/Classes/Smarty/TemplateFinderTest.php
Expand Up @@ -72,7 +72,7 @@ public function testWrongTemplateFallback()

public function testNoFoundTemplateThrowException()
{
$this->setExpectedException('\PrestaShopException');
$this->expectException('\PrestaShopException');
$template = $this->templateFinder->getTemplate('catalog/listing/my-custom-list', 'custom', null, 'fr-FR');
}
}
Expand Up @@ -71,7 +71,7 @@ public function test_getTaxCalculator_ShouldUseFirstComputationMethodFromTaxes()

// Then
$this->assertEquals(TaxCalculator::COMBINE_METHOD, $tax_calculator->computation_method);
$this->assertTrue(is_array($tax_calculator->taxes));
$this->assertInternalType('array', $tax_calculator->taxes);

foreach ($tax_calculator->taxes as $key => $tax) {
$this->assertTrue($tax instanceof Tax);
Expand Down
21 changes: 14 additions & 7 deletions tests-legacy/Unit/Core/Addon/Module/ModuleManagerTest.php
Expand Up @@ -76,49 +76,56 @@ public function testInstallSuccessful()
public function testUninstallSuccessful()
{
$this->assertTrue($this->moduleManager->uninstall(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->assertFalse($this->moduleManager->uninstall(self::UNINSTALLED_MODULE));
}

public function testUpgradeSuccessful()
{
$this->assertTrue($this->moduleManager->upgrade(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->moduleManager->upgrade(self::UNINSTALLED_MODULE);
}

public function testDisableSuccessful()
{
$this->assertTrue($this->moduleManager->disable(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->assertFalse($this->moduleManager->disable(self::UNINSTALLED_MODULE));
}

public function testEnableSuccessful()
{
$this->assertTrue($this->moduleManager->enable(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->assertFalse($this->moduleManager->enable(self::UNINSTALLED_MODULE));
}

public function testDisableOnMobileSuccessful()
{
$this->assertTrue($this->moduleManager->disable_mobile(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->assertFalse($this->moduleManager->disable_mobile(self::UNINSTALLED_MODULE));
}

public function testEnableOnMobileSuccessful()
{
$this->assertTrue($this->moduleManager->enable_mobile(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->assertFalse($this->moduleManager->enable_mobile(self::UNINSTALLED_MODULE));
}

public function testResetSuccessful()
{
$this->assertTrue($this->moduleManager->reset(self::INSTALLED_MODULE));
$this->setExpectedException('Exception', 'The module %module% must be installed first');
$this->expectException('Exception');
$this->expectExceptionMessage('The module %module% must be installed first');
$this->assertFalse($this->moduleManager->reset(self::UNINSTALLED_MODULE));
}

Expand Down
12 changes: 6 additions & 6 deletions tests-legacy/Unit/Core/Addon/Module/ModuleRepositoryTest.php
Expand Up @@ -181,7 +181,7 @@ public function test_get_only_installed_modules()
foreach ($all_modules as $name => $module) {
// Each installed module must be found in the installed modules list
if ($module->database->get('installed') == 1) {
$this->assertTrue(array_key_exists($name, $installed_modules), sprintf('Module %s not found in the filtered list !', $name));
$this->assertArrayHasKey($name, $installed_modules, sprintf('Module %s not found in the filtered list !', $name));
}
}
}
Expand All @@ -204,7 +204,7 @@ public function test_get_only_NOT_installed_modules()
foreach ($all_modules as $name => $module) {
// Each installed module must be found in the installed modules list
if ($module->attributes->get('productType') == 'module' && $module->database->get('installed') == 0) {
$this->assertTrue(array_key_exists($name, $not_installed_modules), sprintf('Module %s not found in the filtered list !', $name));
$this->assertArrayHasKey($name, $not_installed_modules, sprintf('Module %s not found in the filtered list !', $name));
}
}
}
Expand All @@ -229,7 +229,7 @@ public function test_get_only_enabled_modules()
// Each installed module must be found in the installed modules list
if ($module->database->get('installed') == 1
&& $module->database->get('active') == 1) {
$this->assertTrue(array_key_exists($name, $installed_and_active_modules), sprintf('Module %s not found in the filtered list !', $name));
$this->assertArrayHasKey($name, $installed_and_active_modules, sprintf('Module %s not found in the filtered list !', $name));
}
}
}
Expand All @@ -251,7 +251,7 @@ public function test_get_not_enabled_modules()
foreach ($all_modules as $name => $module) {
// Each installed module must be found in the installed modules list
if ($module->attributes->get('productType') == 'module' && $module->database->get('installed') == 1 && $module->database->get('active') == 0) {
$this->assertTrue(array_key_exists($name, $not_active_modules), sprintf('Module %s not found in the filtered list !', $name));
$this->assertArrayHasKey($name, $not_active_modules, sprintf('Module %s not found in the filtered list !', $name));
}
}
}
Expand All @@ -274,7 +274,7 @@ public function test_get_installed_but_disabled_modules()
foreach ($all_modules as $name => $module) {
// Each installed module must be found in the installed modules list
if ($module->database->get('installed') == 1 && $module->database->get('active') == 0) {
$this->assertTrue(array_key_exists($name, $installed_but_not_installed_modules), sprintf('Module %s not found in the filtered list !', $name));
$this->assertArrayHasKey($name, $installed_but_not_installed_modules, sprintf('Module %s not found in the filtered list !', $name));
}
}
}
Expand Down Expand Up @@ -340,7 +340,7 @@ public function test_should_not_be_able_to_return_theme()
$filters = new AddonListFilter();
$filters->setType(AddonListFilterType::THEME);

$this->assertEquals(0, count($this->moduleRepositoryStub->getFilteredList($filters)));
$this->assertCount(0, $this->moduleRepositoryStub->getFilteredList($filters));
}

public function teardown()
Expand Down
Expand Up @@ -70,7 +70,7 @@ public function testGetInstanceByName()

public function testGetInstanceByNameNotFound()
{
$this->setExpectedException('PrestaShopException');
$this->expectException('PrestaShopException');
$this->repository->getInstanceByName('not_found');
}

Expand Down
4 changes: 2 additions & 2 deletions tests-legacy/Unit/Core/Cldr/RepositoryTest.php
Expand Up @@ -62,11 +62,11 @@ public static function tearDownAfterClass()

public function testCacheFolderIsCreated()
{
$this->assertFalse(file_exists($this->cldrCacheFolder));
$this->assertFileNotExists($this->cldrCacheFolder);

mkdir($this->cldrCacheFolder, 0777, true);

$this->assertTrue(file_exists($this->cldrCacheFolder));
$this->assertFileExists($this->cldrCacheFolder);
}

public function testSetLocale()
Expand Down
Expand Up @@ -51,7 +51,7 @@ public function test_simple_check_hash_md5()

public function test_simple_encrypt()
{
$this->assertTrue(is_string($this->hashing->hash("123", _COOKIE_KEY_)));
$this->assertInternalType('string', $this->hashing->hash("123", _COOKIE_KEY_));
}

public function test_simple_first_hash()
Expand Down
2 changes: 1 addition & 1 deletion tests-legacy/Unit/Core/Filter/CollectionFilterTest.php
Expand Up @@ -29,7 +29,7 @@
use PrestaShop\PrestaShop\Core\Filter\CollectionFilter;
use PrestaShop\PrestaShop\Core\Filter\HashMapWhitelistFilter;

class CollectionFilterTest extends \PHPUnit_Framework_TestCase
class CollectionFilterTest extends \PHPUnit\Framework\TestCase
{

/**
Expand Down
Expand Up @@ -28,7 +28,7 @@

use PrestaShop\PrestaShop\Core\Filter\HashMapWhitelistFilter;

class HashMapWhitelistFilterTest extends \PHPUnit_Framework_TestCase
class HashMapWhitelistFilterTest extends \PHPUnit\Framework\TestCase
{

/**
Expand Down
Expand Up @@ -51,10 +51,11 @@ public function setUp()
}

/**
* @expectedException \PrestaShop\PrestaShop\Core\Foundation\Database\Exception
*/
public function test_call_to_invalid_method_throws_exception()
{
$this->expectException(\PrestaShop\PrestaShop\Core\Foundation\Database\Exception::class);

$this->repository->thisDoesNotExist();
}

Expand Down
Expand Up @@ -57,18 +57,20 @@ public function test_joinPaths_three_paths()
}

/**
* @expectedException \PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception
*/
public function test_joinPaths_one_path_throws()
{
$this->expectException(\PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception::class);

$this->fs->joinPaths('a');
}

/**
* @expectedException \PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception
*/
public function test_joinPaths_zero_path_throws()
{
$this->expectException(\PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception::class);

$this->fs->joinPaths();
}

Expand Down Expand Up @@ -112,19 +114,21 @@ public function test_listFilesRecursively()

/**
* Rationale: ls /some/non/existing/file => ls: cannot access /some/non/existing/file: No such file or directory
* @expectedException \PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception
*/
public function test_listEntriesRecursively_throws_if_path_does_not_exist()
{
$this->expectException(\PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception::class);

$this->fs->listEntriesRecursively('/some/w/h/e/r/e/over/the/rainbow');
}

/**
*
* @expectedException \PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception
*/
public function test_listEntriesRecursively_throws_when_path_is_a_file()
{
$this->expectException(\PrestaShop\PrestaShop\Core\Foundation\Filesystem\Exception::class);

$this->fs->listEntriesRecursively(__FILE__);
}
}