From 9d713589b84eef3cd8275e303a4ad90b016bf6e1 Mon Sep 17 00:00:00 2001 From: Gerson Felipe Schwinn Date: Thu, 13 Mar 2025 08:48:09 -0300 Subject: [PATCH 1/2] Refatorando testes com at --- .../Case/Console/Command/AclShellTest.php | 115 +++++++++--------- .../Console/Command/CommandListShellTest.php | 38 ++---- .../Case/Console/Command/TestStringOutput.php | 14 +++ 3 files changed, 83 insertions(+), 84 deletions(-) create mode 100644 lib/Cake/Test/Case/Console/Command/TestStringOutput.php diff --git a/lib/Cake/Test/Case/Console/Command/AclShellTest.php b/lib/Cake/Test/Case/Console/Command/AclShellTest.php index 69df644..f1322da 100644 --- a/lib/Cake/Test/Case/Console/Command/AclShellTest.php +++ b/lib/Cake/Test/Case/Console/Command/AclShellTest.php @@ -22,7 +22,20 @@ App::uses('Shell', 'Console'); App::uses('AclShell', 'Console/Command'); App::uses('ComponentCollection', 'Controller'); +App::uses('TestStringOutput', 'Test/Case/Console/Command'); +class AclShellNoExit extends AclShell { + + public array $dispatchShellArgs = []; + + protected function _stop($status = 0) {} + + + public function dispatchShell() + { + $this->dispatchShellArgs[] = func_get_args(); + } +} /** * AclShellTest class * @@ -37,6 +50,13 @@ class AclShellTest extends CakeTestCase { */ public $fixtures = array('core.aco', 'core.aro', 'core.aros_aco'); + /** + * @var \AclShellNoExit + */ + private $Task; + + private TestStringOutput $output; + /** * setUp method * @@ -47,14 +67,11 @@ public function setUp(): void { Configure::write('Acl.database', 'test'); Configure::write('Acl.classname', 'DbAcl'); - $out = $this->getMock('ConsoleOutput', array(), array(), '', false); - $in = $this->getMock('ConsoleInput', array(), array(), '', false); + $this->output = new TestStringOutput(); + $this->input = new TestStringOutput(); + + $this->Task = new AclShellNoExit($this->output, $this->output, $this->input); - $this->Task = $this->getMock( - 'AclShell', - array('in', 'out', 'hr', 'createFile', 'error', 'err', 'clear', 'dispatchShell'), - array($out, $out, $in) - ); $collection = new ComponentCollection(); $this->Task->Acl = new AclComponent($collection); $this->Task->params['datasource'] = 'test'; @@ -77,17 +94,11 @@ public function testViewWithModelForeignKeyOutput() { $this->Task->Acl->Aro->save(); $this->Task->args[0] = 'aro'; - $this->Task->expects($this->at(0))->method('out')->with('Aro tree:'); - $this->Task->expects($this->at(2))->method('out') - ->with($this->stringContains('[1] ROOT')); - - $this->Task->expects($this->at(4))->method('out') - ->with($this->stringContains('[3] Gandalf')); - - $this->Task->expects($this->at(6))->method('out') - ->with($this->stringContains('[5] MyModel.2')); - $this->Task->view(); + + $this->assertStringContainsString('[1] ROOT', $this->output->output); + $this->assertStringContainsString('[3] Gandalf', $this->output->output); + $this->assertStringContainsString('[5] MyModel.2', $this->output->output); } /** @@ -97,13 +108,12 @@ public function testViewWithModelForeignKeyOutput() { */ public function testViewWithArgument() { $this->Task->args = array('aro', 'admins'); - - $this->Task->expects($this->at(0))->method('out')->with('Aro tree:'); - $this->Task->expects($this->at(2))->method('out')->with(' [2] admins'); - $this->Task->expects($this->at(3))->method('out')->with(' [3] Gandalf'); - $this->Task->expects($this->at(4))->method('out')->with(' [4] Elrond'); - $this->Task->view(); + + $this->assertStringContainsString('Aro tree:', $this->output->output); + $this->assertStringContainsString(' [2] admins', $this->output->output); + $this->assertStringContainsString(' [3] Gandalf', $this->output->output); + $this->assertStringContainsString(' [4] Elrond', $this->output->output); } /** @@ -130,12 +140,11 @@ public function testParsingModelAndForeignKey() { */ public function testCreate() { $this->Task->args = array('aro', 'root', 'User.1'); - $this->Task->expects($this->at(0))->method('out')->with("New Aro 'User.1' created.", 2); - $this->Task->expects($this->at(1))->method('out')->with("New Aro 'User.3' created.", 2); - $this->Task->expects($this->at(2))->method('out')->with("New Aro 'somealias' created.", 2); $this->Task->create(); + $this->assertStringContainsString($this->output->styleText("New Aro 'User.1' created."), $this->output->output); + $Aro = ClassRegistry::init('Aro'); $Aro->cacheQueries = false; $result = $Aro->read(); @@ -147,6 +156,8 @@ public function testCreate() { $this->Task->args = array('aro', 'User.1', 'User.3'); $this->Task->create(); + $this->assertStringContainsString($this->output->styleText("New Aro 'User.3' created."), $this->output->output); + $Aro = ClassRegistry::init('Aro'); $result = $Aro->read(); $this->assertEquals('User', $result['Aro']['model']); @@ -156,6 +167,8 @@ public function testCreate() { $this->Task->args = array('aro', 'root', 'somealias'); $this->Task->create(); + $this->assertStringContainsString($this->output->styleText("New Aro 'somealias' created."), $this->output->output); + $Aro = ClassRegistry::init('Aro'); $result = $Aro->read(); $this->assertEquals('somealias', $result['Aro']['alias']); @@ -171,9 +184,8 @@ public function testCreate() { */ public function testDelete() { $this->Task->args = array('aro', 'AuthUser.1'); - $this->Task->expects($this->at(0))->method('out') - ->with("Aro deleted.", 2); $this->Task->delete(); + $this->assertStringContainsString($this->output->styleText("Aro deleted."), $this->output->output); $Aro = ClassRegistry::init('Aro'); $result = $Aro->findById(3); @@ -201,9 +213,9 @@ public function testSetParent() { */ public function testGrant() { $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create'); - $this->Task->expects($this->at(0))->method('out') - ->with($this->matchesRegularExpression('/granted/'), true); $this->Task->grant(); + + $this->assertStringContainsString('granted', $this->output->output); $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']); @@ -218,11 +230,10 @@ public function testGrant() { */ public function testDeny() { $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create'); - $this->Task->expects($this->at(0))->method('out') - ->with($this->stringContains('Permission denied'), true); - $this->Task->deny(); + $this->assertStringContainsString('Permission denied', $this->output->output); + $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']); $this->assertFalse(empty($node['Aco'][0])); @@ -235,26 +246,25 @@ public function testDeny() { * @return void */ public function testCheck() { - $this->Task->expects($this->at(0))->method('out') - ->with($this->matchesRegularExpression('/not allowed/'), true); - $this->Task->expects($this->at(1))->method('out') - ->with($this->matchesRegularExpression('/granted/'), true); - $this->Task->expects($this->at(2))->method('out') - ->with($this->matchesRegularExpression('/is.*allowed/'), true); - $this->Task->expects($this->at(3))->method('out') - ->with($this->matchesRegularExpression('/not.*allowed/'), true); $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*'); $this->Task->check(); + $this->assertStringContainsString('not allowed', $this->output->output); + $this->output->output = ''; $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create'); $this->Task->grant(); + $this->assertStringContainsString('granted', $this->output->output); + $this->output->output = ''; $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create'); $this->Task->check(); + $this->assertMatchesRegularExpression('/is.*allowed/', $this->output->output); + $this->output->output = ''; $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'delete'); $this->Task->check(); + $this->assertMatchesRegularExpression('/not.*allowed/', $this->output->output); } /** @@ -263,16 +273,15 @@ public function testCheck() { * @return void */ public function testInherit() { - $this->Task->expects($this->at(0))->method('out') - ->with($this->matchesRegularExpression('/Permission .*granted/'), true); - $this->Task->expects($this->at(1))->method('out') - ->with($this->matchesRegularExpression('/Permission .*inherited/'), true); $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create'); $this->Task->grant(); + $this->assertMatchesRegularExpression('/Permission .*granted/', $this->output->output); + $this->output->output = ''; $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all'); $this->Task->inherit(); + $this->assertMatchesRegularExpression('/Permission .*inherited/', $this->output->output); $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']); @@ -291,21 +300,15 @@ public function testGetPath() { $first = $node[0]['Aro']['id']; $second = $node[1]['Aro']['id']; $last = $node[2]['Aro']['id']; - $this->Task->expects($this->at(2))->method('out')->with('[' . $last . '] ROOT'); - $this->Task->expects($this->at(3))->method('out')->with(' [' . $second . '] admins'); - $this->Task->expects($this->at(4))->method('out')->with(' [' . $first . '] Elrond'); $this->Task->getPath(); + $linhas = explode("\n", $this->output->output); + $this->assertEquals('[' . $last . '] ROOT', $linhas[2]); + $this->assertEquals(' [' . $second . '] admins', $linhas[3]); + $this->assertEquals(' [' . $first . '] Elrond', $linhas[4]); } -/** - * test that initdb makes the correct call. - * - * @return void - */ public function testInitDb() { - $this->Task->expects($this->once())->method('dispatchShell') - ->with('schema create DbAcl'); - $this->Task->initdb(); + $this->assertEquals('schema create DbAcl', $this->Task->dispatchShellArgs[0][0]); } } diff --git a/lib/Cake/Test/Case/Console/Command/CommandListShellTest.php b/lib/Cake/Test/Case/Console/Command/CommandListShellTest.php index 5aaed71..87dfd42 100644 --- a/lib/Cake/Test/Case/Console/Command/CommandListShellTest.php +++ b/lib/Cake/Test/Case/Console/Command/CommandListShellTest.php @@ -21,21 +21,7 @@ App::uses('ConsoleInput', 'Console'); App::uses('Shell', 'Console'); App::uses('CommandTask', 'Console/Command/Task'); - -/** - * TestStringOutput - * - * @package Cake.Test.Case.Console.Command - */ -class TestStringOutput extends ConsoleOutput { - - public $output = ''; - - protected function _write($message) { - $this->output .= $message; - } - -} +App::uses('TestStringOutput', 'Test/Case/Console/Command'); /** * CommandListShellTest @@ -62,19 +48,15 @@ public function setUp(): void { CakePlugin::load(array('TestPlugin', 'TestPluginTwo')); $out = new TestStringOutput(); - $in = $this->getMock('ConsoleInput', array(), array(), '', false); - - $this->Shell = $this->getMock( - 'CommandListShell', - array('in', '_stop', 'clear'), - array($out, $out, $in) - ); - - $this->Shell->Command = $this->getMock( - 'CommandTask', - array('in', '_stop', 'clear'), - array($out, $out, $in) - ); + $in = $this->getMockBuilder('ConsoleInput')->getMock(); + + $this->Shell = $this->getMockBuilder('CommandListShell') + ->onlyMethods(array('in', '_stop', 'clear'))->setConstructorArgs(array($out, $out, $in)) + ->getMock(); + + $this->Shell->Command = $this->getMockBuilder('CommandTask') + ->onlyMethods(array('in', '_stop', 'clear')) + ->setConstructorArgs(array($out, $out, $in))->getMock(); } /** diff --git a/lib/Cake/Test/Case/Console/Command/TestStringOutput.php b/lib/Cake/Test/Case/Console/Command/TestStringOutput.php new file mode 100644 index 0000000..2dbd6b7 --- /dev/null +++ b/lib/Cake/Test/Case/Console/Command/TestStringOutput.php @@ -0,0 +1,14 @@ +output .= $message; + } + +} From e0bc7d6450eb87e2f7cee89ade0df956f44ebe79 Mon Sep 17 00:00:00 2001 From: Gerson Felipe Schwinn Date: Thu, 13 Mar 2025 08:57:12 -0300 Subject: [PATCH 2/2] Refatorando testes mock --- lib/Cake/Test/Case/Network/CakeResponseTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 53f2ac8..48fd113 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -215,7 +215,9 @@ public function testHeader() { * @return void */ public function testSend() { - $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', '_setCookies')); + $response = $this->getMockBuilder('CakeResponse') + ->onlyMethods(array('_sendHeader', '_sendContent', '_setCookies')) + ->getMock(); $response->header(array( 'Content-Language' => 'es', 'WWW-Authenticate' => 'Negotiate',