bringyourownideas /
silverstripe-composer-update-checker
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BringYourOwnIdeas\UpdateChecker\Tests; |
||
| 4 | |||
| 5 | use BringYourOwnIdeas\Maintenance\Util\ComposerLoader; |
||
| 6 | use BringYourOwnIdeas\UpdateChecker\UpdateChecker; |
||
| 7 | use Composer\Composer; |
||
| 8 | use PHPUnit_Framework_TestCase; |
||
| 9 | use SilverStripe\Core\Injector\Injector; |
||
| 10 | use SilverStripe\Dev\SapphireTest; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @mixin PHPUnit_Framework_TestCase |
||
| 14 | */ |
||
| 15 | class UpdateCheckerTest extends SapphireTest |
||
| 16 | { |
||
| 17 | protected $usesDatabase = true; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var UpdateChecker |
||
| 21 | */ |
||
| 22 | protected $updateChecker; |
||
| 23 | |||
| 24 | protected function setUp(): void |
||
| 25 | { |
||
| 26 | parent::setUp(); |
||
| 27 | |||
| 28 | // Mock composer and composer loader |
||
| 29 | $composer = $this->getMockBuilder(Composer::Class)->getMock(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | $composerLoader = $this->getMockBuilder(ComposerLoader::class) |
||
| 31 | ->disableOriginalConstructor() |
||
| 32 | ->setMethods(['getComposer']) |
||
| 33 | ->getMock(); |
||
| 34 | $composerLoader->expects($this->once())->method('getComposer')->will($this->returnValue($composer)); |
||
| 35 | Injector::inst()->registerService($composerLoader, ComposerLoader::class); |
||
| 36 | |||
| 37 | // Partially mock UpdateChecker |
||
| 38 | $this->updateChecker = $this->getMockBuilder(UpdateChecker::class) |
||
| 39 | ->setMethods(['findLatestPackage']) |
||
| 40 | ->getMock(); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testCheckForUpdates() |
||
| 44 | { |
||
| 45 | $mockPackage = new \Composer\Package\Package('foo/bar', '2.3.4.0', '2.3.4'); |
||
| 46 | $mockPackage->setSourceReference('foobar123'); |
||
| 47 | |||
| 48 | // No available update |
||
| 49 | $this->updateChecker->expects($this->at(0)) |
||
| 50 | ->method('findLatestPackage') |
||
| 51 | ->will($this->returnValue(false)); |
||
| 52 | |||
| 53 | // There is latest version though |
||
| 54 | $this->updateChecker->expects($this->at(1)) |
||
| 55 | ->method('findLatestPackage') |
||
| 56 | ->will($this->returnValue($mockPackage)); |
||
| 57 | |||
| 58 | $result = $this->updateChecker->checkForUpdates($mockPackage, '~1.2.0'); |
||
| 59 | $this->assertArrayNotHasKey('AvailableVersion', $result, 'No available update is recorded'); |
||
| 60 | $this->assertArrayNotHasKey('AvailableHash', $result, 'No available update is recorded'); |
||
| 61 | $this->assertSame('2.3.4', $result['LatestVersion'], 'Latest version is returned'); |
||
| 62 | $this->assertSame('foobar123', $result['LatestHash'], 'Hash of latest version is returned'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 |