diff --git a/src/Command/CompareCommand.php b/src/Command/CompareCommand.php index 8427822..5410c48 100644 --- a/src/Command/CompareCommand.php +++ b/src/Command/CompareCommand.php @@ -36,6 +36,8 @@ use Org_Heigl\JUnitDiff\Writer\FileSummary; use Org_Heigl\JUnitDiff\Writer\Legend; use Org_Heigl\JUnitDiff\Writer\Standard; +use Org_Heigl\JUnitDiff\Writer\Quiet; +use Org_Heigl\JUnitDiff\Writer\Summary; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -100,6 +102,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $writer->write($mergeResult); } + if ($output->getVerbosity() >= Output::VERBOSITY_QUIET) { + $writer = new Summary( + $style, + basename($input->getArgument('base')), + basename($input->getArgument('current')) + ); + $writer->write($mergeResult); + } } } diff --git a/src/MergeResult.php b/src/MergeResult.php index 0db26f7..2aba7f1 100644 --- a/src/MergeResult.php +++ b/src/MergeResult.php @@ -103,4 +103,42 @@ public function countCurrent() return $counter; } + + public function countNew() + { + $counter = 0; + foreach ($this->content as $value) { + if (isset($value['base'])) { + continue; + } + $counter++; + } + + return $counter; + } + + public function countRemoved() + { + $counter = 0; + foreach ($this->content as $value) { + if (isset($value['current'])) { + continue; + } + $counter++; + } + + return $counter; + } + + public function countChanged() + { + $counter = 0; + foreach ($this->content as $value) { + if (isset($value['base']) && isset($value['current']) && $value['base'] != $value['current']) { + $counter++; + } + } + + return $counter; + } } diff --git a/src/Style/DiffStyle.php b/src/Style/DiffStyle.php index a65f62e..dbacb08 100644 --- a/src/Style/DiffStyle.php +++ b/src/Style/DiffStyle.php @@ -33,5 +33,10 @@ class DiffStyle extends SymfonyStyle { - + public function writeQuiet($message) + { + $this->setVerbosity(self::VERBOSITY_NORMAL); + $this->text($message); + $this->setVerbosity(self::VERBOSITY_QUIET); + } } diff --git a/src/Writer/Summary.php b/src/Writer/Summary.php new file mode 100644 index 0000000..18140fa --- /dev/null +++ b/src/Writer/Summary.php @@ -0,0 +1,60 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author Andreas Heigl + * @copyright 2016-2016 Andreas Heigl + * @license http://www.opensource.org/licenses/mit-license.php MIT-License + * @since 16.06.2016 + * @link http://github.com/heiglandreas/org.heigl.junitdiff + */ + +namespace Org_Heigl\JUnitDiff\Writer; + +use Org_Heigl\JUnitDiff\MergeResult; +use Symfony\Component\Console\Style\StyleInterface; + +class Summary implements WriterInterface +{ + protected $style; + + protected $file1; + + protected $file2; + + public function __construct(StyleInterface $style, $file1, $file2) + { + $this->style = $style; + $this->file1 = $file1; + $this->file2 = $file2; + } + + public function write(MergeResult $mergeResult) + { + $this->style->writeQuiet(sprintf( + 'Added: %s , Removed: %s , Changed: %s ', + $mergeResult->countNew(), + $mergeResult->countRemoved(), + $mergeResult->countChanged() + )); + } +} diff --git a/tests/Command/CompareCommandTest.php b/tests/Command/CompareCommandTest.php index 18f7002..7ab344a 100644 --- a/tests/Command/CompareCommandTest.php +++ b/tests/Command/CompareCommandTest.php @@ -33,6 +33,7 @@ use Org_Heigl\JUnitDiff\Command\CompareCommand; use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Console\Output\OutputInterface; class CompareCommandTest extends \PHPUnit_Framework_TestCase { @@ -62,10 +63,34 @@ public function testExecute() + Wdv_Acl_DbTest::testSettingDefaultModelWithInstance - Wdv_Filter_HyphenCleanerTest::testHyphenCleanerFilter with data set #2 Analyzed 615 tests in total, 613 tests in file log1.xml and 613 tests in file log.xml + Added: 2 , Removed: 2 , Changed: 1 ', $commandTester->getDisplay()); } + public function testExecuteQuietly() + { + // mock the Kernel or create one depending on your needs + $application = new Application(); + $application->add(new CompareCommand()); + + $command = $application->find('compare'); + $commandTester = new CommandTester($command); + $commandTester->execute( + array( + 'base' => __DIR__ . '/../_assets/log1.xml', + 'current' => __DIR__ . '/../_assets/log.xml', + ), + array( + 'verbosity' => OutputInterface::VERBOSITY_QUIET + ) + ); + + $this->assertEquals(' + Added: 2 , Removed: 2 , Changed: 1 +', $commandTester->getDisplay()); + } + public function testThatNonExistingFilesRaiseError() { $application = new Application(); @@ -112,4 +137,4 @@ public function testThatNoFileRaisesError() $this->assertContains('[ERROR] File is not readable', $commandTester->getDisplay()); } -} \ No newline at end of file +} diff --git a/tests/MergeResultTest.php b/tests/MergeResultTest.php index 0eb321e..2b204c1 100644 --- a/tests/MergeResultTest.php +++ b/tests/MergeResultTest.php @@ -159,5 +159,79 @@ public function testThatCountingCurrentWorks() $this->assertEquals(3, $mergeResult->countCurrent()); } -} + public function testThatCountingNewWorks() + { + $style = M::mock('\Symfony\Component\Console\Style\StyleInterface'); + $mergeResult = new MergeResult($style); + + $this->assertAttributeEquals([], 'content', $mergeResult); + $mergeResult->addCurrent('e', 'b'); + $mergeResult->addCurrent('d', 'b'); + $mergeResult->addCurrent('c', 'b'); + $mergeResult->addCurrent('b', 'b'); + $mergeResult->addCurrent('a', 'b'); + $mergeResult->addBase('c', 'a'); + $mergeResult->addBase('b', 'b'); + + $this->assertAttributeEquals([ + 'c' => ['current' => 'b', 'base' => 'a'], + 'b' => ['current' => 'b', 'base' => 'b'], + 'a' => ['current' => 'b'], + 'd' => ['current' => 'b'], + 'e' => ['current' => 'b'], + ], 'content', $mergeResult); + + $this->assertEquals(3, $mergeResult->countNew()); + } + + public function testThatCountingRemovedWorks() + { + $style = M::mock('\Symfony\Component\Console\Style\StyleInterface'); + $mergeResult = new MergeResult($style); + + $this->assertAttributeEquals([], 'content', $mergeResult); + $mergeResult->addCurrent('c', 'b'); + $mergeResult->addCurrent('b', 'b'); + $mergeResult->addCurrent('a', 'b'); + $mergeResult->addBase('c', 'a'); + $mergeResult->addBase('b', 'b'); + $mergeResult->addBase('d', 'b'); + $mergeResult->addBase('e', 'b'); + + $this->assertAttributeEquals([ + 'c' => ['current' => 'b', 'base' => 'a'], + 'b' => ['current' => 'b', 'base' => 'b'], + 'a' => ['current' => 'b'], + 'd' => ['base' => 'b'], + 'e' => ['base' => 'b'], + ], 'content', $mergeResult); + + $this->assertEquals(2, $mergeResult->countRemoved()); + } + + public function testThatCountingChangedWorks() + { + $style = M::mock('\Symfony\Component\Console\Style\StyleInterface'); + $mergeResult = new MergeResult($style); + + $this->assertAttributeEquals([], 'content', $mergeResult); + $mergeResult->addCurrent('c', 'b'); + $mergeResult->addCurrent('b', 'b'); + $mergeResult->addCurrent('a', 'b'); + $mergeResult->addBase('c', 'a'); + $mergeResult->addBase('b', 'a'); + $mergeResult->addBase('d', 'b'); + $mergeResult->addBase('e', 'b'); + + $this->assertAttributeEquals([ + 'c' => ['current' => 'b', 'base' => 'a'], + 'b' => ['current' => 'b', 'base' => 'a'], + 'a' => ['current' => 'b'], + 'd' => ['base' => 'b'], + 'e' => ['base' => 'b'], + ], 'content', $mergeResult); + + $this->assertEquals(2, $mergeResult->countChanged()); + } +} diff --git a/tests/Writer/SummaryTest.php b/tests/Writer/SummaryTest.php new file mode 100644 index 0000000..3f164eb --- /dev/null +++ b/tests/Writer/SummaryTest.php @@ -0,0 +1,52 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author Andreas Heigl + * @copyright 2016-2016 Andreas Heigl + * @license http://www.opensource.org/licenses/mit-license.php MIT-License + * @since 16.06.2016 + * @link http://github.com/heiglandreas/org.heigl.junitdiff + */ + +namespace Org_Heigl\JUnitDiffTest\Writer; + +use Mockery as M; +use Org_Heigl\JUnitDiff\Writer\Quiet; +use Org_Heigl\JUnitDiff\Writer\Summary; + +class SummaryTest extends \PHPUnit_Framework_TestCase +{ + public function testThatQuietSummaryWorks() + { + $styleInterface = M::mock('\Symfony\Component\Console\Style\StyleInterface'); + $styleInterface->shouldReceive('writeQuiet') + ->with('Added: 3 , Removed: 5 , Changed: 7 '); + $mergeresult = M::mock('\Org_Heigl\JUnitDiff\MergeResult'); + $mergeresult->shouldReceive('countNew')->andReturn(3); + $mergeresult->shouldReceive('countRemoved')->andReturn(5); + $mergeresult->shouldReceive('countChanged')->andReturn(7); + + $quiet = new Summary($styleInterface, 'a', 'b'); + $this->assertNull($quiet->write($mergeresult)); + } +}