Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for v4 #55

Open
wants to merge 19 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phpunit.result.cache

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<env name="APP_KEY" value="base64:cuULT9tUWlbKtqv1Mk3jBw832yDSMEv0BvXx6hZOFQQ="/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="core_test"/>
<env name="DB_USERNAME" value="homestead"/>
<env name="DB_PASSWORD" value="secret"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value=""/>
</php>
</phpunit>
3 changes: 2 additions & 1 deletion src/Admin/Database/Factories/LocationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public function definition(): array
'location_lat' => $this->faker->latitude,
'location_lng' => $this->faker->longitude,
'options' => [
'auto_lat_lng' => FALSE,
'auto_lat_lng' => false,
],
'location_status' => $this->faker->boolean,
];
}
}
2 changes: 1 addition & 1 deletion src/Admin/Requests/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function rules()
'location_lng' => ['sometimes', 'numeric'],
'description' => ['max:3028'],
'location_status' => ['boolean'],
'permalink_slug' => ['alpha_dash', 'max:255'],
'permalink_slug' => ['alpha_dash', 'max:255', 'unique:locations'],
'options.gallery.title' => ['string', 'max:128'],
'options.gallery.description' => ['string', 'max:255'],
'delivery_areas' => ['array'],
Expand Down
1 change: 1 addition & 0 deletions src/Admin/Traits/Locationable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Igniter\Admin\Facades\AdminAuth;
use Igniter\Admin\Facades\AdminLocation;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Flame\Igniter;

trait Locationable
{
Expand Down
8 changes: 4 additions & 4 deletions src/Flame/Geolite/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function makeProvider($name): AbstractProvider
*/
public function getDefaultDriver()
{
return $this->container['config']['geocoder.default'] ?? 'nominatim';
return $this->container['config']['igniter.geocoder.default'] ?? 'nominatim';
}

protected function createProvider($name)
Expand All @@ -154,21 +154,21 @@ protected function createProvider($name)

protected function createChainProvider()
{
$providers = $this->container['config']['geocoder.providers'];
$providers = $this->container['config']['igniter.geocoder.providers'];

return new Provider\ChainProvider($this, $providers);
}

protected function createNominatimProvider()
{
$config = $this->container['config']['geocoder.providers.nominatim'];
$config = $this->container['config']['igniter.geocoder.providers.nominatim'];

return new Provider\NominatimProvider(new Client, $config);
}

protected function createGoogleProvider()
{
$config = $this->container['config']['geocoder.providers.google'];
$config = $this->container['config']['igniter.geocoder.providers.google'];

return new Provider\GoogleProvider(new Client, $config);
}
Expand Down
30 changes: 15 additions & 15 deletions src/Flame/Providers/SystemServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,33 +251,33 @@ protected function addTranslationDriver()
protected function setConfiguration()
{
Event::listen('currency.beforeRegister', function () {
app('config')->set('currency.default', setting('default_currency_code'));
app('config')->set('currency.converter', setting('currency_converter.api', 'openexchangerates'));
app('config')->set('currency.converters.openexchangerates.apiKey', setting('currency_converter.oer.apiKey'));
app('config')->set('currency.converters.fixerio.apiKey', setting('currency_converter.fixerio.apiKey'));
app('config')->set('currency.ratesCacheDuration', setting('currency_converter.refreshInterval'));
app('config')->set('currency.model', \Igniter\System\Models\Currency::class);
app('config')->set('igniter.currency.default', setting('default_currency_code'));
app('config')->set('igniter.currency.converter', setting('currency_converter.api', 'openexchangerates'));
app('config')->set('igniter.currency.converters.openexchangerates.apiKey', setting('currency_converter.oer.apiKey'));
app('config')->set('igniter.currency.converters.fixerio.apiKey', setting('currency_converter.fixerio.apiKey'));
app('config')->set('igniter.currency.ratesCacheDuration', setting('currency_converter.refreshInterval'));
app('config')->set('igniter.currency.model', \Igniter\System\Models\Currency::class);
});

$this->app->resolving('translator.localization', function ($localization, $app) {
$app['config']->set('localization.locale', setting('default_language', $app['config']['app.locale']));
$app['config']->set('localization.supportedLocales', setting('supported_languages', []) ?: ['en']);
$app['config']->set('localization.detectBrowserLocale', (bool)setting('detect_language', false));
$app['config']->set('igniter.localization.locale', setting('default_language', $app['config']['app.locale']));
$app['config']->set('igniter.localization.supportedLocales', setting('supported_languages', []) ?: ['en']);
$app['config']->set('igniter.localization.detectBrowserLocale', (bool)setting('detect_language', false));
});

$this->app->resolving('geocoder', function ($geocoder, $app) {
$app['config']->set('geocoder.default', setting('default_geocoder'));
$app['config']->set('igniter.geocoder.default', setting('default_geocoder'));

$region = $app['country']->getCountryCodeById(setting('country_id'));
$app['config']->set('geocoder.providers.google.region', $region);
$app['config']->set('geocoder.providers.nominatim.region', $region);
$app['config']->set('igniter.geocoder.providers.google.region', $region);
$app['config']->set('igniter.geocoder.providers.nominatim.region', $region);

$app['config']->set('geocoder.providers.google.apiKey', setting('maps_api_key'));
$app['config']->set('geocoder.precision', setting('geocoder_boundary_precision', 8));
$app['config']->set('igniter.geocoder.providers.google.apiKey', setting('maps_api_key'));
$app['config']->set('igniter.geocoder.precision', setting('geocoder_boundary_precision', 8));
});

Event::listen(CommandStarting::class, function () {
config()->set('system.activityRecordsTTL', (int)setting('activity_log_timeout', 60));
config()->set('igniter.system.activityRecordsTTL', (int)setting('activity_log_timeout', 60));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Flame/Translation/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ public function getBrowserLocale()

protected function getConfig(string $string)
{
return $this->config['localization.'.$string];
return $this->config['igniter.localization.'.$string];
}
}
10 changes: 9 additions & 1 deletion src/System/Libraries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ public function addressFormat($address, $useLineBreaks = true)
)
);

while (stripos($formattedAddress, '<br /><br />') !== false) {
$formattedAddress = str_replace('<br /><br />', '<br />', $formattedAddress);
}

if (substr($formattedAddress, -6) == '<br />') {
$formattedAddress = substr($formattedAddress, 0, -6);
}

if (!$useLineBreaks)
$formattedAddress = str_replace('<br />', ', ', $formattedAddress);

return $formattedAddress;
return trim($formattedAddress);
}

public function getCountryNameById($id = null)
Expand Down
195 changes: 195 additions & 0 deletions tests/Admin/Models/LocationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace Tests\Admin\Models;

use Igniter\Admin\Models\Location;

it('should fail to create a location when no name is provided', function () {
try {
$location = Location::factory()->make();
$location->location_name = null;
$location->save();
$this->assertFalse(true);
}
catch (\Exception $e) {
$this->assertFalse(false);
}
});

it('should fail to create a location when no email is provided', function () {
try {
$location = Location::factory()->make();
$location->location_email = null;
$location->save();
$this->assertFalse(true);
}
catch (\Exception $e) {
$this->assertFalse(false);
}
});

it('should fail to create a location when no address line 1 is provided', function () {
try {
$location = Location::factory()->make();
$location->location_address_1 = null;
$location->save();
$this->assertFalse(true);
}
catch (\Exception $e) {
$this->assertFalse(false);
}
});

it('should be able to create and delete a location', function () {
$location = Location::factory()->make();
$location->save();

$location->delete();

$this->assertNull($location->fresh());
});

it('should be able to get and set a location option', function () {
$location = Location::factory()->make();
$location->save();

$location->setOption('test_option', true);

$this->assertNotNull($location->getOption('test_option'));
});

it('should create a latitude and longitude for the location when requested and address data is provided', function () {
$location = Location::factory()->make();
$location->setOption('auto_lat_lng', true);
$location->location_address_1 = '53 Church Road';
$location->location_city = 'London';
$location->location_postcode = 'SE19 2TJ';
$location->location_lat = null;
$location->location_lng = null;
$location->save();

$this->assertNotNull($location->location_lat);
$this->assertNotNull($location->location_lng);
});

it('should fail to create a location with a duplicate slug', function () {
$location1 = Location::factory()->make();
$location1->permalink_slug = 'test';
$location1->save();

$location2 = Location::factory()->make();
$location2->permalink_slug = 'test';
$location2->save();

$this->assertFalse(true);
});

it('should filter locations by status', function () {
$location1 = Location::factory()->make();
$location1->location_status = true;
$location1->save();

$location2 = Location::factory()->make();
$location2->location_status = false;
$location2->save();

$locations = Location::query()->isEnabled()->get();

$this->assertCount(1, $locations);
});

it('should filter locations by offer delivery', function () {
$location1 = Location::factory()->make();
$location1->save();
$location1->setOption('offer_delivery', true);

$location2 = Location::factory()->make();
$location2->save();
$location2->setOption('offer_delivery', false);

$locations = Location::query()->listFrontEnd([
'hasDelivery' => true,
'pageLimit' => null,
])->get();

$this->assertCount(1, $locations);
});

it('should filter locations by offer collection', function () {
$location1 = Location::factory()->make();
$location1->save();
$location1->setOption('offer_collection', false);

$location2 = Location::factory()->make();
$location2->save();
$location2->setOption('offer_collection', true);

$locations = Location::query()->listFrontEnd([
'hasCollection' => true,
'pageLimit' => null,
])->get();

$this->assertCount(1, $locations);
});

it('should sort locations alphabetically by name ascending', function () {
$location1 = Location::factory()->make();
$location1->location_name = 'Test 1';
$location1->save();

$location2 = Location::factory()->make();
$location2->location_name = 'A Test 2';
$location2->save();

$locations = Location::query()->listFrontEnd([
'hasCollection' => true,
'pageLimit' => null,
'sort' => 'location_name asc',
])->get();

$this->assertSame($locations->first()->location_name, $location2->location_name);
});

it('should sort locations alphabetically by name descending', function () {
$location1 = Location::factory()->make();
$location1->location_name = 'Test 1';
$location1->save();

$location2 = Location::factory()->make();
$location2->location_name = 'A Test 2';
$location2->save();

$locations = Location::query()->listFrontEnd([
'hasCollection' => true,
'pageLimit' => null,
'sort' => 'location_name desc',
])->get();

$this->assertSame($locations->first()->location_name, $location1->location_name);
});

it('can be made default when enabled', function () {
$location = Location::factory()->make();
$location->location_status = true;
$location->save();

$location->makeDefault();

$this->assertSame(Location::getDefault()->getKey(), $location->getKey());
});

it('cant be made default when not enabled', function () {
try {

$location = Location::factory()->make();
$location->location_status = false;
$location->save();

$location->makeDefault();

$this->assertNotSame(Location::getDefault()->getKey(), $location->getKey());

} catch (\Exception $e) {
$this->assertFalse(false);
}
});
Loading