1
0
Fork 0

Merge branch '1.5'

pull/6675/merge
Jordi Boggiano 2017-09-11 16:09:36 +02:00
commit dae575c197
2 changed files with 35 additions and 0 deletions

View File

@ -430,6 +430,11 @@ class JsonManipulator
return false;
}
// check that we are not leaving a dangling comma on the previous line if the last line was removed
if (preg_match('#,\s*$#', $matches['start']) && preg_match('#^\}$#', $matches['end'])) {
$matches['start'] = rtrim(preg_replace('#,(\s*)$#', '$1', $matches['start']), $this->indent);
}
$this->contents = $matches['start'] . $matches['end'];
if (preg_match('#^\{\s*\}\s*$#', $this->contents)) {
$this->contents = "{\n}";

View File

@ -2331,4 +2331,34 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
}
', $manipulator->getContents());
}
public function testRemoveMainKeyAtEndOfFile()
{
$manipulator = new JsonManipulator('{
"require": {
"package/a": "*"
}
}
');
$this->assertTrue($manipulator->addMainKey('homepage', 'http...'));
$this->assertTrue($manipulator->addMainKey('license', 'mit'));
$this->assertEquals('{
"require": {
"package/a": "*"
},
"homepage": "http...",
"license": "mit"
}
', $manipulator->getContents());
$this->assertTrue($manipulator->removeMainKey('homepage'));
$this->assertTrue($manipulator->removeMainKey('license'));
$this->assertEquals('{
"require": {
"package/a": "*"
}
}
', $manipulator->getContents());
}
}