Skip to content

Commit

Permalink
Merge pull request #19 from salahhusa9/Tests-Repository-Source
Browse files Browse the repository at this point in the history
Tests Repository Source
  • Loading branch information
salahhusa9 committed Nov 11, 2023
2 parents 3a8760d + d151d6c commit 9d3ef41
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/Exceptions/GithubConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Salahhusa9\Updater\Exceptions;

use Exception;

class GithubConfigException extends Exception
{
/**
* GithubConfigException constructor.
*
* @param string $message
*/
public function __construct($message)
{
parent::__construct($message);
}
}
11 changes: 7 additions & 4 deletions src/RepositorySource/GithubRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Http;
use Salahhusa9\Updater\Contracts\Repository;
use Salahhusa9\Updater\Exceptions\GithubConfigException;

class GithubRepository implements Repository
{
Expand Down Expand Up @@ -80,18 +81,20 @@ public function getVersionsData(): \Illuminate\Support\Collection
*
* @return void
*/
private function checkConfig()
public function checkConfig()
{
if (config('updater.github_token') == null) {
throw new \Exception('Please set GITHUB_TOKEN in .env file');
throw new GithubConfigException('Please set GITHUB_TOKEN in .env file');
}

if (config('updater.github_username') == null) {
throw new \Exception('Please set GITHUB_USERNAME in .env file');
throw new GithubConfigException('Please set GITHUB_USERNAME in .env file');
}

if (config('updater.github_repository') == null) {
throw new \Exception('Please set GITHUB_REPOSITORY in .env file');
throw new GithubConfigException('Please set GITHUB_REPOSITORY in .env file');
}

return true;
}
}
119 changes: 119 additions & 0 deletions tests/RepositorySource/GithubRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Salahhusa9\Updater\Tests\RepositorySource;

use Illuminate\Support\Facades\Http;
use Salahhusa9\Updater\Exceptions\GithubConfigException;
use Salahhusa9\Updater\RepositorySource\GithubRepository;

class GithubRepositoryTest extends \Salahhusa9\Updater\Tests\TestCase
{
/** @test */
public function config_check()
{
// set env
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-updater');

$githubRepository = new GithubRepository();

$this->assertTrue($githubRepository->checkConfig());
}

/** @test */
public function config_check_exception()
{
$this->expectException(GithubConfigException::class);

$githubRepository = new GithubRepository();

$this->assertTrue($githubRepository->checkConfig());
}

/** @test */
public function it_can_get_latest_version()
{
// set env
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-updater');

Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-updater/releases/latest' => Http::response([
'tag_name' => 'v1.0.0',
], 200),
]);

$githubRepository = new GithubRepository();

$this->assertEquals('v1.0.0', $githubRepository->getLatestVersion());
}

/** @test */
public function it_can_get_latest_version_data()
{
// set env
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-updater');

Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-updater/releases/latest' => Http::response([
'tag_name' => 'v1.0.0',
], 200),
]);

$githubRepository = new GithubRepository();

$this->assertIsArray($githubRepository->getLatestVersionData()->toArray());
}

/** @test */
public function it_can_get_versions()
{
// set env
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-updater');

Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-updater/releases' => Http::response([
[
'tag_name' => 'v1.0.0',
],
[
'tag_name' => 'v1.0.1',
],
], 200),
]);

$githubRepository = new GithubRepository();

$this->assertIsArray($githubRepository->getVersions());
}

/** @test */
public function it_can_get_versions_data()
{
// set env
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-updater');

Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-updater/releases' => Http::response([
[
'tag_name' => 'v1.0.0',
],
[
'tag_name' => 'v1.0.1',
],
], 200),
]);

$githubRepository = new GithubRepository();

$this->assertIsArray($githubRepository->getVersionsData()->toArray());
}
}

0 comments on commit 9d3ef41

Please sign in to comment.