Skip to content

Commit

Permalink
Merge branch 'ChDeinert-feature/output-summary'
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Jan 23, 2017
2 parents c66919a + 41dc927 commit af43e12
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/Command/CompareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
}
38 changes: 38 additions & 0 deletions src/MergeResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 6 additions & 1 deletion src/Style/DiffStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
60 changes: 60 additions & 0 deletions src/Writer/Summary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Copyright (c) Andreas Heigl<[email protected]>
*
* 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<[email protected]>
* @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:<bg=green;fg=black> %s </>, Removed:<bg=red;fg=yellow> %s </>, Changed:<bg=blue;fg=yellow> %s </>',
$mergeResult->countNew(),
$mergeResult->countRemoved(),
$mergeResult->countChanged()
));
}
}
27 changes: 26 additions & 1 deletion tests/Command/CompareCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -112,4 +137,4 @@ public function testThatNoFileRaisesError()

$this->assertContains('[ERROR] File is not readable', $commandTester->getDisplay());
}
}
}
76 changes: 75 additions & 1 deletion tests/MergeResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
52 changes: 52 additions & 0 deletions tests/Writer/SummaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Copyright (c) Andreas Heigl<[email protected]>
*
* 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<[email protected]>
* @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:<bg=green;fg=black> 3 </>, Removed:<bg=red;fg=yellow> 5 </>, Changed:<bg=blue;fg=yellow> 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));
}
}

0 comments on commit af43e12

Please sign in to comment.