|
13 | 13 | namespace Gitonomy\Git\Tests;
|
14 | 14 |
|
15 | 15 | use Gitonomy\Git\Diff\Diff;
|
| 16 | +use Gitonomy\Git\Diff\File; |
| 17 | +use Gitonomy\Git\Repository; |
16 | 18 |
|
17 | 19 | class DiffTest extends AbstractTest
|
18 | 20 | {
|
@@ -151,6 +153,113 @@ public function testWorksWithUmlauts($repository)
|
151 | 153 | $this->assertSame('file_with_umlauts_\303\244\303\266\303\274', $files[0]->getNewName());
|
152 | 154 | }
|
153 | 155 |
|
| 156 | + public function testDeleteFileWithoutRaw() |
| 157 | + { |
| 158 | + $deprecationCalled = false; |
| 159 | + $self = $this; |
| 160 | + set_error_handler(function (int $errno, string $errstr) use ($self, &$deprecationCalled): void { |
| 161 | + $deprecationCalled = true; |
| 162 | + $self->assertSame('Using Diff::parse without raw information is deprecated. See https://github.com/gitonomy/gitlib/issues/227.', $errstr); |
| 163 | + }, E_USER_DEPRECATED); |
| 164 | + |
| 165 | + $diff = Diff::parse(<<<'DIFF' |
| 166 | + diff --git a/test b/test |
| 167 | + deleted file mode 100644 |
| 168 | + index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 |
| 169 | + |
| 170 | + DIFF); |
| 171 | + $firstFile = $diff->getFiles()[0]; |
| 172 | + |
| 173 | + restore_exception_handler(); |
| 174 | + |
| 175 | + $this->assertTrue($deprecationCalled); |
| 176 | + $this->assertFalse($firstFile->isCreation()); |
| 177 | + // TODO: Enable after #226 is merged |
| 178 | + //$this->assertTrue($firstFile->isDeletion()); |
| 179 | + //$this->assertFalse($firstFile->isChangeMode()); |
| 180 | + $this->assertSame('e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', $firstFile->getOldIndex()); |
| 181 | + $this->assertNull($firstFile->getNewIndex()); |
| 182 | + } |
| 183 | + |
| 184 | + public function testModeChangeFileWithoutRaw() |
| 185 | + { |
| 186 | + $deprecationCalled = false; |
| 187 | + $self = $this; |
| 188 | + set_error_handler(function (int $errno, string $errstr) use ($self, &$deprecationCalled): void { |
| 189 | + $deprecationCalled = true; |
| 190 | + $self->assertSame('Using Diff::parse without raw information is deprecated. See https://github.com/gitonomy/gitlib/issues/227.', $errstr); |
| 191 | + }, E_USER_DEPRECATED); |
| 192 | + |
| 193 | + $diff = Diff::parse(<<<'DIFF' |
| 194 | + diff --git a/a.out b/a.out |
| 195 | + old mode 100755 |
| 196 | + new mode 100644 |
| 197 | + |
| 198 | + DIFF); |
| 199 | + $firstFile = $diff->getFiles()[0]; |
| 200 | + |
| 201 | + restore_exception_handler(); |
| 202 | + |
| 203 | + $this->assertTrue($deprecationCalled); |
| 204 | + $this->assertFalse($firstFile->isCreation()); |
| 205 | + $this->assertFalse($firstFile->isDeletion()); |
| 206 | + $this->assertTrue($firstFile->isChangeMode()); |
| 207 | + $this->assertSame('', $firstFile->getOldIndex()); |
| 208 | + $this->assertSame('', $firstFile->getNewIndex()); |
| 209 | + } |
| 210 | + |
| 211 | + public function testModeChangeFileWithRaw() |
| 212 | + { |
| 213 | + $deprecationCalled = false; |
| 214 | + set_error_handler(function (int $errno, string $errstr) use (&$deprecationCalled): void { |
| 215 | + $deprecationCalled = true; |
| 216 | + }, E_USER_DEPRECATED); |
| 217 | + |
| 218 | + $diff = Diff::parse(<<<'DIFF' |
| 219 | + :100755 100644 d1af4b23d0cc9313e5b2d3ef2fb9696c94afaa81 d1af4b23d0cc9313e5b2d3ef2fb9696c94afaa81 M a.out |
| 220 | +
|
| 221 | + diff --git a/a.out b/a.out |
| 222 | + old mode 100755 |
| 223 | + new mode 100644 |
| 224 | + |
| 225 | + DIFF); |
| 226 | + $firstFile = $diff->getFiles()[0]; |
| 227 | + |
| 228 | + restore_exception_handler(); |
| 229 | + |
| 230 | + $this->assertFalse($deprecationCalled); |
| 231 | + $this->assertFalse($firstFile->isCreation()); |
| 232 | + $this->assertFalse($firstFile->isDeletion()); |
| 233 | + $this->assertTrue($firstFile->isChangeMode()); |
| 234 | + $this->assertSame('d1af4b23d0cc9313e5b2d3ef2fb9696c94afaa81', $firstFile->getOldIndex()); |
| 235 | + $this->assertSame('d1af4b23d0cc9313e5b2d3ef2fb9696c94afaa81', $firstFile->getNewIndex()); |
| 236 | + } |
| 237 | + |
| 238 | + public function testThrowErrorOnBlobGetWithoutIndex() |
| 239 | + { |
| 240 | + $repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->getMock(); |
| 241 | + $file = new File('oldName', 'newName', '100755', '100644', '', '', false); |
| 242 | + $file->setRepository($repository); |
| 243 | + |
| 244 | + try { |
| 245 | + $file->getOldBlob(); |
| 246 | + } catch(\RuntimeException $exception) { |
| 247 | + $this->assertSame('Index is missing to return Blob object.', $exception->getMessage()); |
| 248 | + } |
| 249 | + |
| 250 | + try { |
| 251 | + $file->getNewBlob(); |
| 252 | + } catch(\RuntimeException $exception) { |
| 253 | + $this->assertSame('Index is missing to return Blob object.', $exception->getMessage()); |
| 254 | + } |
| 255 | + |
| 256 | + $this->assertFalse($file->isCreation()); |
| 257 | + $this->assertFalse($file->isDeletion()); |
| 258 | + $this->assertTrue($file->isChangeMode()); |
| 259 | + $this->assertSame('', $file->getOldIndex()); |
| 260 | + $this->assertSame('', $file->getNewIndex()); |
| 261 | + } |
| 262 | + |
154 | 263 | public function testEmptyNewFile()
|
155 | 264 | {
|
156 | 265 | $diff = Diff::parse("diff --git a/test b/test\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\n");
|
|
0 commit comments