|
| 1 | +<?php namespace NumenCode\SyncOps\Tests\Console; |
| 2 | + |
| 3 | +use Mockery; |
| 4 | +use PluginTestCase; |
| 5 | +use NumenCode\SyncOps\Console\ProjectPush; |
| 6 | + |
| 7 | +class ProjectPushTest extends PluginTestCase |
| 8 | +{ |
| 9 | + public function tearDown(): void |
| 10 | + { |
| 11 | + Mockery::close(); |
| 12 | + |
| 13 | + parent::tearDown(); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Test function: handle |
| 18 | + * When there are no local changes (empty git status), the command should exit successfully |
| 19 | + * and print the appropriate informational message without attempting to add/commit/push. |
| 20 | + */ |
| 21 | + public function testHandleNoChangesReturnsSuccess(): void |
| 22 | + { |
| 23 | + $command = Mockery::mock(ProjectPush::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
| 24 | + |
| 25 | + // Expect initial status check |
| 26 | + $command->shouldReceive('runLocalCommand') |
| 27 | + ->once() |
| 28 | + ->with('git status --porcelain') |
| 29 | + ->andReturn(''); |
| 30 | + |
| 31 | + // Ensure no other git operations are attempted |
| 32 | + $command->shouldNotReceive('runLocalCommand')->with('git add --all'); |
| 33 | + $command->shouldNotReceive('runLocalCommand')->with(Mockery::on(function ($arg) { |
| 34 | + return is_string($arg) && strpos($arg, 'git commit -m') === 0; |
| 35 | + })); |
| 36 | + $command->shouldNotReceive('runLocalCommand')->with('git push'); |
| 37 | + |
| 38 | + // Output expectations (not strictly validating content except key line) |
| 39 | + $command->shouldReceive('newLine')->atLeast()->once(); |
| 40 | + $command->shouldReceive('line')->atLeast()->once(); |
| 41 | + $command->shouldReceive('info')->once()->with(Mockery::pattern('/No changes to commit/i')); |
| 42 | + |
| 43 | + $result = $command->handle(); |
| 44 | + |
| 45 | + $this->assertSame(ProjectPush::SUCCESS, $result); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test function: handle |
| 50 | + * When there are changes, the command should add, commit with provided message, and push, returning SUCCESS. |
| 51 | + */ |
| 52 | + public function testHandleWithChangesCommitsAndPushes(): void |
| 53 | + { |
| 54 | + $message = 'My custom commit message'; |
| 55 | + |
| 56 | + $command = Mockery::mock(ProjectPush::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
| 57 | + |
| 58 | + // Option should provide custom message |
| 59 | + $command->shouldReceive('option')->with('message')->andReturn($message); |
| 60 | + |
| 61 | + // Status indicates changes |
| 62 | + $command->shouldReceive('runLocalCommand') |
| 63 | + ->once() |
| 64 | + ->with('git status --porcelain') |
| 65 | + ->andReturn(" M file.txt"); |
| 66 | + |
| 67 | + // Add all |
| 68 | + $command->shouldReceive('runLocalCommand') |
| 69 | + ->once() |
| 70 | + ->with('git add --all') |
| 71 | + ->andReturn(''); |
| 72 | + |
| 73 | + // Commit with exact quoted message |
| 74 | + $command->shouldReceive('runLocalCommand') |
| 75 | + ->once() |
| 76 | + ->with('git commit -m "' . $message . '"') |
| 77 | + ->andReturn(''); |
| 78 | + |
| 79 | + // Push |
| 80 | + $command->shouldReceive('runLocalCommand') |
| 81 | + ->once() |
| 82 | + ->with('git push') |
| 83 | + ->andReturn(''); |
| 84 | + |
| 85 | + // Allow console outputs |
| 86 | + $command->shouldReceive('newLine')->atLeast()->once(); |
| 87 | + $command->shouldReceive('line')->atLeast()->once(); |
| 88 | + $command->shouldReceive('warn')->atLeast()->once(); |
| 89 | + $command->shouldReceive('info')->once()->with(Mockery::pattern('/successfully pushed/i')); |
| 90 | + |
| 91 | + $result = $command->handle(); |
| 92 | + |
| 93 | + $this->assertSame(ProjectPush::SUCCESS, $result); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test function: handle |
| 98 | + * If an exception occurs during any git operation (e.g., push), |
| 99 | + * the command should print error messages and return FAILURE. |
| 100 | + */ |
| 101 | + public function testHandleProcessFailureReturnsFailure(): void |
| 102 | + { |
| 103 | + $command = Mockery::mock(ProjectPush::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
| 104 | + |
| 105 | + // Simulate detected changes so that it proceeds to add/commit/push |
| 106 | + $command->shouldReceive('runLocalCommand') |
| 107 | + ->once() |
| 108 | + ->with('git status --porcelain') |
| 109 | + ->andReturn('M file'); |
| 110 | + |
| 111 | + $command->shouldReceive('option')->with('message')->andReturnNull(); // use default message |
| 112 | + |
| 113 | + // Allow add and commit to pass |
| 114 | + $command->shouldReceive('runLocalCommand')->once()->with('git add --all')->andReturn(''); |
| 115 | + $command->shouldReceive('runLocalCommand')->once()->with(Mockery::on(function ($arg) { |
| 116 | + return is_string($arg) && strpos($arg, 'git commit -m "Server changes"') === 0; |
| 117 | + }))->andReturn(''); |
| 118 | + |
| 119 | + // Fail on push with a generic exception |
| 120 | + $command->shouldReceive('runLocalCommand')->once()->with('git push')->andThrow(new \RuntimeException('push failed')); |
| 121 | + |
| 122 | + // Expect error outputs |
| 123 | + $command->shouldReceive('newLine')->atLeast()->once(); |
| 124 | + $command->shouldReceive('line')->atLeast()->once(); |
| 125 | + $command->shouldReceive('warn')->atLeast()->once(); |
| 126 | + $command->shouldReceive('error')->atLeast()->once(); |
| 127 | + |
| 128 | + $result = $command->handle(); |
| 129 | + |
| 130 | + $this->assertSame(ProjectPush::FAILURE, $result); |
| 131 | + } |
| 132 | +} |
0 commit comments