Skip to content

Commit

Permalink
minor fixes to unit tests (#10052)
Browse files Browse the repository at this point in the history
Use PHPUnit assertions better.
  • Loading branch information
gemal authored and markstory committed Jan 18, 2017
1 parent 322aa7a commit e808d23
Show file tree
Hide file tree
Showing 29 changed files with 166 additions and 166 deletions.
2 changes: 1 addition & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -700,7 +700,7 @@ public function assertRedirect($url = null, $message = '')
}
$result = $this->_response->header();
if ($url === null) {
$this->assertTrue(!empty($result['Location']), $message);
$this->assertNotEmpty($result['Location'], $message);

return;
}
Expand Down
38 changes: 19 additions & 19 deletions tests/TestCase/Cache/Engine/FileEngineTest.php
Expand Up @@ -95,7 +95,7 @@ public function testReadAndwrite()

$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'file_test');
$this->assertTrue(file_exists(TMP . 'tests/cake_test'));
$this->assertFileExists(TMP . 'tests/cake_test');

$result = Cache::read('test', 'file_test');
$expecting = $data;
Expand Down Expand Up @@ -166,7 +166,7 @@ public function testDeleteCache()

$result = Cache::delete('delete_test', 'file_test');
$this->assertTrue($result);
$this->assertFalse(file_exists(TMP . 'tests/delete_test'));
$this->assertFileNotExists(TMP . 'tests/delete_test');

$result = Cache::delete('delete_test', 'file_test');
$this->assertFalse($result);
Expand Down Expand Up @@ -205,30 +205,30 @@ public function testClear()
Cache::write('serialize_test1', $data, 'file_test');
Cache::write('serialize_test2', $data, 'file_test');
Cache::write('serialize_test3', $data, 'file_test');
$this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test1'));
$this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test2'));
$this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test3'));
$this->assertFileExists(TMP . 'tests/cake_serialize_test1');
$this->assertFileExists(TMP . 'tests/cake_serialize_test2');
$this->assertFileExists(TMP . 'tests/cake_serialize_test3');

sleep(1);
$result = Cache::clear(true, 'file_test');
$this->assertTrue($result);
$this->assertFalse(file_exists(TMP . 'tests/cake_serialize_test1'));
$this->assertFalse(file_exists(TMP . 'tests/cake_serialize_test2'));
$this->assertFalse(file_exists(TMP . 'tests/cake_serialize_test3'));
$this->assertFileNotExists(TMP . 'tests/cake_serialize_test1');
$this->assertFileNotExists(TMP . 'tests/cake_serialize_test2');
$this->assertFileNotExists(TMP . 'tests/cake_serialize_test3');

$data = 'this is a test of the emergency broadcasting system';
Cache::write('serialize_test1', $data, 'file_test');
Cache::write('serialize_test2', $data, 'file_test');
Cache::write('serialize_test3', $data, 'file_test');
$this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test1'));
$this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test2'));
$this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test3'));
$this->assertFileExists(TMP . 'tests/cake_serialize_test1');
$this->assertFileExists(TMP . 'tests/cake_serialize_test2');
$this->assertFileExists(TMP . 'tests/cake_serialize_test3');

$result = Cache::clear(false, 'file_test');
$this->assertTrue($result);
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
$this->assertFileNotExists(CACHE . 'cake_serialize_test1');
$this->assertFileNotExists(CACHE . 'cake_serialize_test2');
$this->assertFileNotExists(CACHE . 'cake_serialize_test3');
}

/**
Expand Down Expand Up @@ -307,7 +307,7 @@ public function testKeyPath()
{
$result = Cache::write('views.countries.something', 'here', 'file_test');
$this->assertTrue($result);
$this->assertTrue(file_exists(TMP . 'tests/cake_views_countries_something'));
$this->assertFileExists(TMP . 'tests/cake_views_countries_something');

$result = Cache::read('views.countries.something', 'file_test');
$this->assertEquals('here', $result);
Expand All @@ -317,11 +317,11 @@ public function testKeyPath()

$result = Cache::write('domain.test.com:8080', 'here', 'file_test');
$this->assertTrue($result);
$this->assertTrue(file_exists(TMP . 'tests/cake_domain_test_com_8080'));
$this->assertFileExists(TMP . 'tests/cake_domain_test_com_8080');

$result = Cache::write('command>dir|more', 'here', 'file_test');
$this->assertTrue($result);
$this->assertTrue(file_exists(TMP . 'tests/cake_command_dir_more'));
$this->assertFileExists(TMP . 'tests/cake_command_dir_more');
}

/**
Expand Down Expand Up @@ -526,13 +526,13 @@ public function testClearingWithRepeatWrites()
$this->assertEquals('rchavik', Cache::read('user', 'repeat'));

Cache::delete('user', 'repeat');
$this->assertEquals(false, Cache::read('user', 'repeat'));
$this->assertFalse(Cache::read('user', 'repeat'));

$this->assertTrue(Cache::write('user', 'ADmad', 'repeat'));
$this->assertEquals('ADmad', Cache::read('user', 'repeat'));

Cache::clearGroup('users', 'repeat');
$this->assertEquals(false, Cache::read('user', 'repeat'));
$this->assertFalse(Cache::read('user', 'repeat'));

$this->assertTrue(Cache::write('user', 'markstory', 'repeat'));
$this->assertEquals('markstory', Cache::read('user', 'repeat'));
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -574,7 +574,7 @@ public function testShuffle()
{
$data = [1, 2, 3, 4];
$collection = (new Collection($data))->shuffle();
$this->assertEquals(count($data), count(iterator_to_array($collection)));
$this->assertCount(count($data), iterator_to_array($collection));

foreach ($collection as $value) {
$this->assertContains($value, $data);
Expand All @@ -590,7 +590,7 @@ public function testSample()
{
$data = [1, 2, 3, 4];
$collection = (new Collection($data))->sample(2);
$this->assertEquals(2, count(iterator_to_array($collection)));
$this->assertCount(2, iterator_to_array($collection));

foreach ($collection as $value) {
$this->assertContains($value, $data);
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/Console/ConsoleOptionParserTest.php
Expand Up @@ -588,7 +588,7 @@ public function testAddSubcommandObject()
$parser = new ConsoleOptionParser('test', false);
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
$result = $parser->subcommands();
$this->assertEquals(1, count($result));
$this->assertCount(1, $result);
$this->assertEquals('test', $result['test']->name());
}

Expand All @@ -602,7 +602,7 @@ public function testRemoveSubcommand()
$parser = new ConsoleOptionParser('test', false);
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
$result = $parser->subcommands();
$this->assertEquals(1, count($result));
$this->assertCount(1, $result);
$parser->removeSubcommand('test');
$result = $parser->subcommands();
$this->assertEquals(0, count($result), 'Remove a subcommand does not work');
Expand Down Expand Up @@ -704,10 +704,10 @@ public function testBuildFromArray()
$this->assertTrue(isset($options['other']));

$args = $parser->arguments();
$this->assertEquals(2, count($args));
$this->assertCount(2, $args);

$commands = $parser->subcommands();
$this->assertEquals(1, count($commands));
$this->assertCount(1, $commands);
}

/**
Expand Down Expand Up @@ -800,8 +800,8 @@ public function testToArray()
$this->assertTrue(isset($options['name']));
$this->assertTrue(isset($options['other']));

$this->assertEquals(2, count($result['arguments']));
$this->assertEquals(1, count($result['subcommands']));
$this->assertCount(2, $result['arguments']);
$this->assertCount(1, $result['subcommands']);
}

/**
Expand Down Expand Up @@ -830,7 +830,7 @@ public function testMerge()
$this->assertTrue(isset($options['file']));
$this->assertTrue(isset($options['output']));

$this->assertEquals(2, count($result['arguments']));
$this->assertEquals(6, count($result['options']));
$this->assertCount(2, $result['arguments']);
$this->assertCount(6, $result['options']);
}
}
18 changes: 9 additions & 9 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -505,7 +505,7 @@ public function testCreateFileNonInteractive()
$contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertEquals(file_get_contents($file), $contents);
}

Expand All @@ -527,11 +527,11 @@ public function testCreateFileNoReply()
->will($this->returnValue('n'));

touch($file);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);

$contents = "My content";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertTextEquals('', file_get_contents($file));
$this->assertFalse($result, 'Did not create file.');
}
Expand All @@ -554,11 +554,11 @@ public function testCreateFileOverwrite()
->will($this->returnValue('y'));

touch($file);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);

$contents = "My content";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertTextEquals($contents, file_get_contents($file));
$this->assertTrue($result, 'Did create file.');
}
Expand All @@ -576,7 +576,7 @@ public function testCreateFileOverwriteNonInteractive()
new Folder($path, true);

touch($file);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);

$this->io->expects($this->never())->method('askChoice');

Expand Down Expand Up @@ -609,10 +609,10 @@ public function testCreateFileOverwriteAll()

foreach ($files as $file => $contents) {
touch($file);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);

$result = $this->Shell->createFile($file, $contents);
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertTextEquals($contents, file_get_contents($file));
$this->assertTrue($result, 'Did create file.');
}
Expand All @@ -636,7 +636,7 @@ public function testCreateFileNoPermissions()
chmod($path, 0444);

$this->Shell->createFile($file, 'testing');
$this->assertFalse(file_exists($file));
$this->assertFileNotExists($file);

chmod($path, 0744);
rmdir($path);
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -367,10 +367,10 @@ public function testLoadAuthorizeResets()
{
$this->Controller->Auth->config('authorize', ['Controller']);
$result = $this->Controller->Auth->constructAuthorize();
$this->assertEquals(1, count($result));
$this->assertCount(1, $result);

$result = $this->Controller->Auth->constructAuthorize();
$this->assertEquals(1, count($result));
$this->assertCount(1, $result);
}

/**
Expand Down Expand Up @@ -410,10 +410,10 @@ public function testLoadAuthenticateResets()
{
$this->Controller->Auth->config('authenticate', ['Form']);
$result = $this->Controller->Auth->constructAuthenticate();
$this->assertEquals(1, count($result));
$this->assertCount(1, $result);

$result = $this->Controller->Auth->constructAuthenticate();
$this->assertEquals(1, count($result));
$this->assertCount(1, $result);
}

/**
Expand Down Expand Up @@ -445,7 +445,7 @@ public function testSameAuthenticateWithDifferentHashers()
]);

$objects = $this->Controller->Auth->constructAuthenticate();
$this->assertEquals(2, count($objects));
$this->assertCount(2, $objects);

$this->assertInstanceOf('Cake\Auth\FormAuthenticate', $objects['FormSimple']);
$this->assertInstanceOf('Cake\Auth\FormAuthenticate', $objects['FormBlowfish']);
Expand Down
Expand Up @@ -356,7 +356,7 @@ public function testIsAjaxParams()
$this->RequestHandler->initialize([]);
$this->Controller->beforeFilter($event);
$this->RequestHandler->startup($event);
$this->assertEquals(true, $this->Controller->request->params['isAjax']);
$this->assertTrue($this->Controller->request->params['isAjax']);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Controller/ComponentTest.php
Expand Up @@ -68,8 +68,8 @@ public function testNestedComponentLoading()

$this->assertInstanceOf(OrangeComponent::class, $Apple->Orange, 'class is wrong');
$this->assertInstanceOf(BananaComponent::class, $Apple->Orange->Banana, 'class is wrong');
$this->assertTrue(empty($Apple->Session));
$this->assertTrue(empty($Apple->Orange->Session));
$this->assertEmpty($Apple->Session);
$this->assertEmpty($Apple->Orange->Session);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Core/Configure/Engine/IniConfigTest.php
Expand Up @@ -233,7 +233,7 @@ public function testDump()
{
$engine = new IniConfig(TMP);
$result = $engine->dump('test', $this->testData);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);

$expected = <<<INI
[One]
Expand All @@ -253,7 +253,7 @@ public function testDump()
$this->assertTextEquals($expected, $result);

$result = $engine->dump('test', $this->testData);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);

$contents = file_get_contents($file);
$this->assertTextEquals($expected, $contents);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Core/Configure/Engine/JsonConfigTest.php
Expand Up @@ -154,7 +154,7 @@ public function testDump()
{
$engine = new JsonConfig(TMP);
$result = $engine->dump('test', $this->testData);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);
$expected = '{
"One": {
"two": "value",
Expand All @@ -176,7 +176,7 @@ public function testDump()
$this->assertTextEquals($expected, $contents);

$result = $engine->dump('test', $this->testData);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);

$contents = file_get_contents($file);
$this->assertTextEquals($expected, $contents);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Core/Configure/Engine/PhpConfigTest.php
Expand Up @@ -140,7 +140,7 @@ public function testDump()
{
$engine = new PhpConfig(TMP);
$result = $engine->dump('test', $this->testData);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);
$expected = <<<PHP
<?php
return array (
Expand Down Expand Up @@ -168,7 +168,7 @@ public function testDump()
$this->assertTextEquals($expected, $contents);

$result = $engine->dump('test', $this->testData);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);

$contents = file_get_contents($file);
$this->assertTextEquals($expected, $contents);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Core/ConfigureTest.php
Expand Up @@ -515,7 +515,7 @@ public function testDump()
Configure::config('test_Engine', new PhpConfig(TMP));

$result = Configure::dump('config_test', 'test_Engine');
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('return ', $result);
Expand All @@ -535,7 +535,7 @@ public function testDumpPartial()
Configure::write('Error', ['test' => 'value']);

$result = Configure::dump('config_test', 'test_Engine', ['Error']);
$this->assertTrue($result > 0);
$this->assertGreaterThan(0, $result);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('return ', $result);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Error/DebuggerTest.php
Expand Up @@ -117,7 +117,7 @@ public function testExcerpt()
$this->assertRegExp($pattern, $result[0]);

$return = Debugger::excerpt('[internal]', 2, 2);
$this->assertTrue(empty($return));
$this->assertEmpty($return);

$result = Debugger::excerpt(__FILE__, __LINE__, 5);
$this->assertCount(11, $result);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Error/ErrorHandlerTest.php
Expand Up @@ -178,7 +178,7 @@ public function testErrorSuppressed()
@include 'invalid.file';
//@codingStandardsIgnoreEnd
$result = ob_get_clean();
$this->assertTrue(empty($result));
$this->assertEmpty($result);
}

/**
Expand Down

0 comments on commit e808d23

Please sign in to comment.