Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Code coverage improvment.
Browse files Browse the repository at this point in the history
  • Loading branch information
amir9480 committed Nov 4, 2019
1 parent 8cf3867 commit 7e5b4ea
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CreateProvincesTable extends Migration
public function up()
{
Schema::create('provinces', function (Blueprint $table) {
$table->bigIncrements('id');
$table->tinyIncrements('id');
$table->string('name');
$table->string('slug')->unique()->index();
$table->text('description')->nullable();
Expand Down
4 changes: 2 additions & 2 deletions database/migrations/2014_10_11_000001_create_cities_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class CreateCitiesTable extends Migration
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->increments('id');
$table->string('name');
$table->string('slug')->unique()->index();
$table->text('description')->nullable();
$table->unsignedBigInteger('province_id');
$table->unsignedTinyInteger('province_id');

$table->foreign('province_id')->references('id')->on('provinces')->onDelete('cascade')->onUpdate('cascade');
});
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<server name="APP_KEY" value="base64:9u33KZJcTDCm1wC8XDHCKyBO99nkzjT2jOwuQ2Fsdyk="/>
<server name="APP_ENV" value="testing"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
Expand Down
14 changes: 8 additions & 6 deletions src/BalootFakerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Baloot;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;

class BalootFakerProvider extends \Faker\Provider\Base
Expand Down Expand Up @@ -52,7 +54,7 @@ public function iranPhone()

public function aparatVideo()
{
$videos = \Cache::rememberFor('sanjab_baloot_aparat_videos', now()->addHour(), function () {
$videos = Cache::remember('sanjab_baloot_aparat_videos', now()->addHour(), function () {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://www.aparat.com/etc/api/categoryVideos/cat/7/perpage/50',
Expand All @@ -72,13 +74,12 @@ public function aparatVideo()
}
}
}
curl_close($curl);
});
if (is_array($videos)) {
return 'https://www.aparat.com/v/'.array_random($videos);
return 'https://www.aparat.com/v/'.Arr::random($videos);
}

return 'https://www.aparat.com/v/'.array_random([
// @codeCoverageIgnoreStart
return 'https://www.aparat.com/v/'.Arr::random([
'IAN6z', 'xrAb8', 'w7NMS', '0fFhg', 'uCgQd',
'hK5fF', 'arsHC', '43aZ8', 'syI7N', 'XaN3o',
'YJpM1', 'TSAz1', 'sQBq4', 'Y7AZF', 'dNn3M',
Expand All @@ -92,6 +93,7 @@ public function aparatVideo()
'rUC8l', '6yoBO', 'aoE2p', 'slKDV', '2Dun4',
'L4J1F', 'dwWTy', 'a6PqK', 'bDaPX', 'pRPSt',
]);
// @codeCoverageIgnoreEnd
}

public function aparatVideos($count)
Expand All @@ -106,7 +108,7 @@ public function aparatVideos($count)

public function word()
{
return array_random(explode(' ', $this->generator->realText()));
return Arr::random(explode(' ', $this->generator->realText()));
}

public function sentence()
Expand Down
4 changes: 1 addition & 3 deletions src/EloquentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ public function getAttribute($key)
} elseif ($matches[2] == '_ftt') {
return $attributeValue->formatJalaliDatetime();
}

return $attributeValue;
}
}

if ($key && preg_match('/^([A-Za-z0-9_]+)_aparat$/', $key, $matches)) {
$attributeValue = $this->getAttribute($matches[1]);
if (is_string($attributeValue) && $info = Arr::first(aparat_info([$attributeValue]))) {
return $info;
} elseif (is_array($attributeValue) && count($info = aparat_info($attributeValue))) {
} elseif (is_array($attributeValue) && count($info = aparat_info($attributeValue)) > 0) {
return $info;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Models/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function province()

/**
* Sluggable config.
* @codeCoverageIgnore
*
* @return array
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Province.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Province extends Model

/**
* Cities of province.
* @codeCoverageIgnore
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
Expand All @@ -33,6 +34,7 @@ public function cities()

/**
* Sluggable config.
* @codeCoverageIgnore
*
* @return array
*/
Expand Down
114 changes: 113 additions & 1 deletion tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

use Carbon\Carbon;
use Baloot\EloquentHelper;
use Baloot\Models\City;
use Baloot\Models\Province;
use Illuminate\Http\Request;
use Orchestra\Testbench\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;

class BasicTest extends TestCase
{
use DatabaseMigrations;

public function testEnToFa()
{
$this->assertEquals(en_to_fa('1234567890'), '۱۲۳۴۵۶۷۸۹۰');
Expand Down Expand Up @@ -74,6 +82,7 @@ public function testEloquentHelper()

protected $casts = [
'birth_date' => 'date',
'birth_date_fake' => 'date'
];
};
$model->setDateFormat('Y/j/d');
Expand All @@ -87,15 +96,118 @@ public function testEloquentHelper()
$model->birth_date_fa = '1370/1/1';
$this->assertEquals($model->birth_date->format('Y-m-d'), '1991-10-21');

// non-valid data
$model->birth_date_fake_fa = '1370/1/1/1/1';
$this->assertNull($model->birth_date_fake);

// aparat
$model->video = 'https://www.aparat.com/v/O4qSP';
if ($model->video_aparat instanceof \Baloot\Models\AparatVideo) {
$this->assertEquals('ایستگاه جوانمرد راستگو', $model->video_aparat->title);
$this->assertEquals($model->video_aparat->id, $model->video_aparat->reload()->id);
}

// Test aparat array
$model->videos = ['https://www.aparat.com/v/O4qSP', 'https://www.aparat.com/v/6ARN8'];
if (is_array($model->videos_aparat) &&
count($model->videos_aparat) == 2 &&
$model->videos_aparat[0] instanceof \Baloot\Models\AparatVideo &&
$model->videos_aparat[1] instanceof \Baloot\Models\AparatVideo
) {
$this->assertEquals('ایستگاه جوانمرد راستگو', $model->videos_aparat[0]->title);
$this->assertEquals('اولین تریلر رسمی فیلم The Grudge - زومجی', $model->videos_aparat[1]->title);
}
}

public function testFakerProvider()
{
$faker = app(\Faker\Generator::class);
$this->assertIsString($faker->word);
$this->assertIsString($faker->sentence);
$this->assertIsString($faker->paragraph);
$this->assertTrue(preg_match('/https:\/\/www.aparat.com\/v\/.+/', $faker->aparatVideo) > 0);
$this->assertTrue(preg_match('/https:\/\/www.aparat.com\/v\/.+/', $faker->aparatVideos(2)[0]) > 0);
$this->assertTrue(preg_match('/https:\/\/www.aparat.com\/v\/.+/', $faker->aparatVideos(2)[1]) > 0);
$this->assertTrue(preg_match('/09\d+/', $faker->iranMobile) > 0);
$this->assertTrue(preg_match('/0\d+/', $faker->iranPhone) > 0);

$tempFolder = 'temp_'.time();
$this->assertIsString($newFilePath = $faker->customImage(public_path($tempFolder), 32, 32, $tempFolder.'/'));
$this->assertTrue(file_exists(public_path($newFilePath)));

$this->assertISArray($newFilesPath = $faker->customImages(public_path($tempFolder), 32, 32, 2, $tempFolder.'/'));
$this->assertCount(2, $newFilesPath);
$this->assertIsString($newFilesPath[0]);
$this->assertIsString($newFilesPath[1]);
$this->assertTrue(file_exists(public_path($newFilesPath[0])));
$this->assertTrue(file_exists(public_path($newFilesPath[1])));
}

public function testValidationRules()
{
$this->assertTrue(Validator::make(['mobile' => '09371234567'], ['mobile' => 'iran_mobile'])->passes());
$this->assertFalse(Validator::make(['mobile' => '09371234'], ['mobile' => 'iran_mobile'])->passes());
$this->assertFalse(Validator::make(['mobile' => '09371234567aaa'], ['mobile' => 'iran_mobile'])->passes());
$this->assertFalse(Validator::make(['mobile' => 'aaa09371234567'], ['mobile' => 'iran_mobile'])->passes());
$this->assertFalse(Validator::make(['mobile' => '0937123456789'], ['mobile' => 'iran_mobile'])->passes());

$this->assertTrue(Validator::make(['phone' => '01112345678'], ['phone' => 'iran_phone'])->passes());
$this->assertFalse(Validator::make(['phone' => '01112345'], ['phone' => 'iran_phone'])->passes());
$this->assertFalse(Validator::make(['phone' => '01112345678aaa'], ['phone' => 'iran_phone'])->passes());
$this->assertFalse(Validator::make(['phone' => 'aaa01112345678'], ['phone' => 'iran_phone'])->passes());
$this->assertFalse(Validator::make(['phone' => '0111234567891'], ['phone' => 'iran_phone'])->passes());
}

public function testRouteBindings()
{
$this->app['router']->middleware('web')->group(function ($router) {
$router->get('/province/{province}', function ($province) {
return $province->id;
});
$router->get('/province_by_slug/{province_by_slug}', function (Province $province) {
return $province->id;
});
$router->get('/province_by_id/{province_by_id}', function (Province $province) {
return $province->id;
});

$router->get('/city/{city}', function (City $city) {
return $city->id;
});
$router->get('/city_by_slug/{city_by_slug}', function (City $city) {
return $city->id;
});
$router->get('/city_by_id/{city_by_id}', function (City $city) {
return $city->id;
});
});

$province = Province::inRandomOrder()->first();
$city = City::inRandomOrder()->first();
$this->get('/province/'.$province->id)->assertStatus(200)->assertSee($province->id);
$this->get('/province/'.$province->slug)->assertStatus(200)->assertSee($province->id);
$this->get('/province_by_id/'.$province->id)->assertStatus(200)->assertSee($province->id);
$this->get('/province_by_slug/'.$province->slug)->assertStatus(200)->assertSee($province->id);
$this->get('/province/random-stuff')->assertStatus(404);

$this->get('/city/'.$city->id)->assertStatus(200)->assertSee($city->id);
$this->get('/city/'.$city->slug)->assertStatus(200)->assertSee($city->id);
$this->get('/city_by_id/'.$city->id)->assertStatus(200)->assertSee($city->id);
$this->get('/city_by_slug/'.$city->slug)->assertStatus(200)->assertSee($city->id);
$this->get('/city/random-stuff')->assertStatus(404);
}

protected function setUp(): void
{
parent::setUp();
$this->artisan('vendor:publish', ['--provider' => 'Cviebrock\\EloquentSluggable\\ServiceProvider'])->run();
$this->runDatabaseMigrations();
$this->seed(\Baloot\Database\CitiesTableSeeder::class);
}


protected function getPackageProviders($app)
{
return [\Baloot\BalootServiceProvider::class];
return [\Cviebrock\EloquentSluggable\ServiceProvider::class, \Baloot\BalootServiceProvider::class];
}
}

0 comments on commit 7e5b4ea

Please sign in to comment.