Skip to content

Commit

Permalink
Merge pull request #482 from portabilis/dusk-enderecamento
Browse files Browse the repository at this point in the history
Testes de browser para módulo de endereçamento
  • Loading branch information
edersoares committed Mar 1, 2019
2 parents aec6e18 + 34538d2 commit bee9ab8
Show file tree
Hide file tree
Showing 36 changed files with 1,522 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
matrix:

include:

- language: php

sudo: required

dist: xenial

php:
- 7.1
- 7.2

addons:
postgresql: 9.5
chrome: stable

env:
- APP_URL=http://localhost:8000
- APP_ENV=testing
- DB_CONNECTION=pgsql
- DB_HOST=localhost
Expand All @@ -20,5 +31,12 @@ matrix:
cache:
directories:
- $HOME/.composer/cache

before_script:
- composer new-install
- vendor/laravel/dusk/bin/chromedriver-linux > /dev/null 2>&1 &
- php artisan serve > /dev/null 2>&1 &

script:
- vendor/bin/phpunit
- php artisan dusk
19 changes: 19 additions & 0 deletions app/City.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class City extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'state_id',
'name',
'ibge'
];
}
17 changes: 17 additions & 0 deletions app/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'ibge'
];
}
17 changes: 17 additions & 0 deletions app/District.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class District extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'city_id', 'name', 'ibge'
];
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/LegacyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private function getHttpHeaders()
*/
private function getHttpStatusCode()
{
return http_response_code();
return http_response_code() ?: Response::HTTP_OK;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions app/Neighborhood.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Neighborhood extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'district_id',
'name',
'zone',
];
}
18 changes: 18 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Support\Str;
use Laravel\Dusk\Browser;
use Laravel\Dusk\DuskServiceProvider;
use Laravel\Dusk\ElementResolver;
use Laravel\Telescope\TelescopeServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -54,6 +55,22 @@ private function customBrowserForFakeAuth()
});
}

/**
* Add custom methods in ElementResolver class used by Laravel Dusk.
*
* @return void
*/
private function customElementResolver()
{
ElementResolver::macro('findByText', function ($text, $tag) {
foreach ($this->all($tag) as $element) {
if (Str::contains($element->getText(), $text)) {
return $element;
}
}
});
}

/**
* Load migrations from other repositories or packages.
*
Expand All @@ -78,6 +95,7 @@ public function boot()
if ($this->app->environment('development', 'dusk', 'local', 'testing')) {
$this->registerRoutesForFakeAuth();
$this->customBrowserForFakeAuth();
$this->customElementResolver();
}

if ($this->app->runningInConsole()) {
Expand Down
17 changes: 17 additions & 0 deletions app/State.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class State extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'country_id', 'name', 'abbreviation', 'ibge'
];
}
21 changes: 21 additions & 0 deletions database/factories/CityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\State;
use Faker\Generator as Faker;

$factory->define(App\City::class, function (Faker $faker) {
return [
'state_id' => function () {

$state = State::query()->inRandomOrder()->first();

if (empty($state)) {
$state = factory(State::class)->create();
}

return $state->getKey();
},
'name' => $faker->city,
'ibge' => $faker->randomNumber(6),
];
});
10 changes: 10 additions & 0 deletions database/factories/CountryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Faker\Generator as Faker;

$factory->define(App\Country::class, function (Faker $faker) {
return [
'name' => $faker->country,
'ibge' => $faker->randomNumber(6),
];
});
21 changes: 21 additions & 0 deletions database/factories/DistrictFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\City;
use Faker\Generator as Faker;

$factory->define(App\District::class, function (Faker $faker) {
return [
'city_id' => function () {

$city = City::query()->inRandomOrder()->first();

if (empty($city)) {
$city = factory(City::class)->create();
}

return $city->getKey();
},
'name' => $faker->name,
'ibge' => $faker->randomNumber(6),
];
});
21 changes: 21 additions & 0 deletions database/factories/NeighborhoodFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\District;
use Faker\Generator as Faker;

$factory->define(App\Neighborhood::class, function (Faker $faker) {
return [
'district_id' => function () {

$district = District::query()->inRandomOrder()->first();

if (empty($district)) {
$district = factory(District::class)->create();
}

return $district->getKey();
},
'name' => $faker->name,
'zone' => $faker->randomElement([1, 2]),
];
});
28 changes: 28 additions & 0 deletions database/factories/StateFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use App\Country;
use Faker\Generator as Faker;

$factory->define(App\State::class, function (Faker $faker) {
return [
'country_id' => function () use ($faker) {

$country = Country::query()->inRandomOrder()->first();

if (empty($country) || $faker->boolean()) {
$country = factory(Country::class)->create();
}

return $country->getKey();
},
'name' => $faker->colorName . ' State',
'abbreviation' => function () use ($faker) {
return $faker->unique()->randomElement([
'AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH', 'II', 'JJ', 'KK',
'LL', 'MM', 'NN', 'OO', 'PP', 'QQ', 'RR', 'SS', 'TT', 'UU', 'VV',
'WW', 'XX', 'YY', 'ZZ'
]);
},
'ibge' => $faker->randomNumber(6),
];
});
2 changes: 1 addition & 1 deletion ieducar/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
date_default_timezone_set($coreExt['Config']->app->locale->timezone);

$tenantEnv = $_SERVER['HTTP_HOST'] ?? null;
$devEnv = ['development', 'local'];
$devEnv = ['development', 'local', 'testing', 'dusk'];

if ($coreExt['Config']->hasEnviromentSection($tenantEnv)) {
$coreExt['Config']->changeEnviroment($tenantEnv);
Expand Down
86 changes: 86 additions & 0 deletions tests/Browser/CityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Tests\Browser;

use Illuminate\Foundation\Testing\WithFaker;
use Tests\Browser\Pages\City\CreatePage;
use Tests\Browser\Pages\City\DetailPage;
use Tests\Browser\Pages\City\ListingPage;
use Tests\Browser\Pages\City\UpdatePage;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;

class CityTest extends DuskTestCase
{
use WithFaker;

/**
* Test city flow.
*
* @return void
*
* @throws \Throwable
*/
public function testFlowForCityPages()
{
$this->browse(function (Browser $browser) {
$country = 45; // Brasil
$state = 'SC'; // Santa Catarina
$cityName = $this->faker->city;
$cityNameAfterUpdate = $this->faker->city;

$browser->loginLegacy();

$browser->visit(new ListingPage())
->press(' Novo ');

$browser->on(new CreatePage())
->select('@select-country', $country)
->waitUsing(10, 1000, function () use ($browser) {
return $browser->resolver->findOrFail('[name=sigla_uf]')->isEnabled();
})
->select('@select-state', $state)
->type('@input-name', $cityName)
->press('@button-save');

$browser->on(new ListingPage())
->type('@input-name', $cityName)
->press('Buscar');

$browser->on(new ListingPage());

$cityId = $browser->resolver->findByText($cityName, 'a')->getAttribute('data-id');

$browser->clickLink($cityName);

$browser->on(new DetailPage($cityId))
->press(' Editar ');

$browser->on(new UpdatePage($cityId))
->type('@input-name', $cityNameAfterUpdate)
->press('@button-save');

$browser->on(new ListingPage())
->type('@input-name', $cityNameAfterUpdate)
->press('Buscar')
->clickLink($cityNameAfterUpdate);

$browser->on(new DetailPage($cityId));
});
}

/**
* Test city listing.
*
* @return void
*
* @throws \Throwable
*/
public function testCityListing()
{
$this->browse(function (Browser $browser) {
$browser->loginLegacy();
$browser->visit(new ListingPage());
});
}
}

0 comments on commit bee9ab8

Please sign in to comment.