From c3542d4a2d614baf1b48a65eb6e296fbeac77fd4 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Tue, 26 Sep 2017 19:20:37 -0500 Subject: [PATCH] REFACTOR - remove AddressData and Distance DataExtensions migrate to dynamic/silverstripe-geocoder closes #171 --- .travis.yml | 56 ++++-------- _config/config.yml | 4 +- composer.json | 9 +- phpunit.xml.dist | 8 +- src/extensions/AddressDataExtension.php | 106 ----------------------- src/extensions/DistanceDataExtension.php | 37 -------- tests/AddressDataExtensionTest.php | 43 --------- tests/LocationCategoryTest.php | 2 +- tests/LocationTest.php | 2 +- tests/LocatorFormTest.php | 2 +- tests/LocatorTest.php | 2 +- 11 files changed, 34 insertions(+), 237 deletions(-) delete mode 100644 src/extensions/AddressDataExtension.php delete mode 100644 src/extensions/DistanceDataExtension.php delete mode 100644 tests/AddressDataExtensionTest.php diff --git a/.travis.yml b/.travis.yml index 4306e5c..6a82903 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,55 +1,33 @@ - language: php -sudo: false - -addons: - apt: - packages: - - tidy - -before_install: - - pip install --user codecov - env: global: - - DB=MYSQL CORE_RELEASE=4.0 - - MODULE_PATH=locator - - COVERAGE=0 + - COMPOSER_ROOT_VERSION=4.0.x-dev + - CODECOV_TOKEN= + - SCRUT_TOKEN= matrix: include: - - php: 7.0 - env: DB=SQLITE - - php: 7.0 - env: DB=PGSQL - - php: 7.0 - env: COVERAGE=1 - php: 5.6 - allow_failures: + env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1 - php: 7.0 - env: DB=SQLITE - + env: DB=MYSQL PHPUNIT_TEST=1 + - php: 7.1 + env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1 before_script: + # Init PHP - phpenv rehash - - composer self-update || true - - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - - cd ~/builds/ss - - mv "$MODULE_PATH/phpunit.xml.dist" . + - phpenv config-rm xdebug.ini + - echo 'memory_limit = 2048M' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini -#Execute tests with or without coverage -script: - # Execute tests with no coverage. This is the fastest option - - "if [ \"$COVERAGE\" = \"0\" ]; then vendor/bin/phpunit $MODULE_PATH/tests/; fi" + # Install composer dependencies + - composer require --prefer-dist --no-update silverstripe-themes/simple:~3.2 + - composer update --no-suggest --prefer-dist - # Execute tests with coverage. Do this for a small - - "if [ \"$COVERAGE\" = \"1\" ]; then vendor/bin/phpunit --coverage-clover=coverage.clover $MODULE_PATH/tests/; fi" +script: + - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit; fi + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml && wget https://scrutinizer-ci.com/ocular.phar; fi -# Upload code coverage when tests pass after_success: - - "if [ \"$COVERAGE\" = \"1\" ]; then mv coverage.clover ~/build/$TRAVIS_REPO_SLUG/; fi" - - cd ~/build/$TRAVIS_REPO_SLUG - - wget https://scrutinizer-ci.com/ocular.phar - - "if [ \"$COVERAGE\" = \"1\" ]; then travis_retry codecov && travis_retry php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi" + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml -t $CODECOV_TOKEN && travis_retry php ocular.phar code-coverage:upload --format=php-clover --access-token=$SCRUT_TOKEN coverage.xml; fi \ No newline at end of file diff --git a/_config/config.yml b/_config/config.yml index c412b66..76354d3 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -3,6 +3,6 @@ Name: locator --- Dynamic\Locator\Location: extensions: - - Dynamic\Locator\DistanceDataExtension - - Dynamic\Locator\AddressDataExtension + - Dynamic\SilverStripeGeocoder\DistanceDataExtension + - Dynamic\SilverStripeGeocoder\AddressDataExtension - SilverStripe\Versioned\Versioned('Stage','Live') \ No newline at end of file diff --git a/composer.json b/composer.json index f3aac8f..8fd35f3 100644 --- a/composer.json +++ b/composer.json @@ -20,12 +20,17 @@ } ], "require": { - "silverstripe/framework": "^4.0", - "silverstripe/cms": "^4.0", + "silverstripe/recipe-cms": "^1.0", "muskie9/data-to-arraylist": "^2.0", "dynamic/silverstripe-geocoder": "^1.0", "symbiote/silverstripe-gridfieldextensions": "^3.0" }, + "require-dev": { + "phpunit/PHPUnit": "^5.7" + }, + "config": { + "process-timeout": 600 + }, "suggest": { "unclecheese/betterbuttons": "Adds new form actions and buttons to GridField detail form for usability enhancements.", "unclecheese/bootstrap-forms": "Allows the creation of forms compatible with the Twitter Bootstrap CSS framework in SilverStripe." diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 907bfea..82ad35e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,13 @@ - + - locator/tests + tests - locator/src/ + src/ - locator/tests/ + tests/ diff --git a/src/extensions/AddressDataExtension.php b/src/extensions/AddressDataExtension.php deleted file mode 100644 index efc1acd..0000000 --- a/src/extensions/AddressDataExtension.php +++ /dev/null @@ -1,106 +0,0 @@ - 'Varchar(255)', - 'Address2' => 'Varchar(255)', - 'City' => 'Varchar(64)', - 'State' => 'Varchar(64)', - 'PostalCode' => 'Varchar(10)', - 'Country' => 'Varchar(2)', - 'Lat' => 'Decimal(10,7)', - 'Lng' => 'Decimal(10,7)', - ]; - - public function updateCMSFields(FieldList $fields) - { - //$fields->replaceField('Country', CountryDropdownField::create('Country')); / - /// CountryDropdownField is removed from SS4 - - $fields->replaceField('Lat', ReadonlyField::create('Lat')); - $fields->replaceField('Lng', ReadonlyField::create('Lng')); - - $fields->addFieldsToTab('Root.Address', [ - $fields->dataFieldByName('Address'), - $fields->dataFieldByName('Address2'), - $fields->dataFieldByName('City'), - $fields->dataFieldByName('State'), - $fields->dataFieldByName('PostalCode'), - $fields->dataFieldByName('Country'), - $fields->dataFieldByName('Lat'), - $fields->dataFieldByName('Lng'), - ]); - } - - /** - * Returns the full address as a simple string. - * - * @return string - */ - public function getFullAddress() { - $parts = array( - $this->owner->Address, - $this->owner->Address2, - $this->owner->City, - $this->owner->State, - $this->owner->PostalCode, - $this->owner->Country, - ); - return implode(', ', array_filter($parts)); - } - - /** - * @return bool - */ - public function hasAddress() { - return ( - $this->owner->Address - && $this->owner->City - && $this->owner->State - && $this->owner->PostalCode - ); - } - /** - * Returns TRUE if any of the address fields have changed. - * - * @param int $level - * @return bool - */ - public function isAddressChanged($level = 1) { - $fields = array('Address', 'Address2', 'City', 'State', 'PostalCode', 'Country'); - $changed = $this->owner->getChangedFields(false, $level); - foreach ($fields as $field) { - if (array_key_exists($field, $changed)) return true; - } - return false; - } - /** - * - */ - public function onBeforeWrite() - { - parent::onBeforeWrite(); - if ($this->hasAddress()) { - if (!$this->isAddressChanged()) { - return; - } - if ($address = $this->getFullAddress()) { - $geocoder = new GoogleGeocoder($address); - $response = $geocoder->getResult(); - $this->owner->Lat = $response->getLatitude(); - $this->owner->Lng = $response->getLongitude(); - } - } - } -} \ No newline at end of file diff --git a/src/extensions/DistanceDataExtension.php b/src/extensions/DistanceDataExtension.php deleted file mode 100644 index 2b2fb0a..0000000 --- a/src/extensions/DistanceDataExtension.php +++ /dev/null @@ -1,37 +0,0 @@ -getRequest()->getVar('Address'); - if ($this->owner->hasMethod('updateAddressValue')) { - $address = $this->owner->updateAddressValue($address); - } - if (class_exists(GoogleGeocoder::class)) { - if ($address) { // on frontend - $geocoder = new GoogleGeocoder($address); - $response = $geocoder->getResult(); - $Lat = $response->getLatitude(); - $Lng = $response->getLongitude(); - - $query - ->addSelect(array( - '( 3959 * acos( cos( radians(' . $Lat . ') ) * cos( radians( `Lat` ) ) * cos( radians( `Lng` ) - radians(' . $Lng . ') ) + sin( radians(' . $Lat . ') ) * sin( radians( `Lat` ) ) ) ) AS distance', - )); - } - } - } -} diff --git a/tests/AddressDataExtensionTest.php b/tests/AddressDataExtensionTest.php deleted file mode 100644 index 350ba6a..0000000 --- a/tests/AddressDataExtensionTest.php +++ /dev/null @@ -1,43 +0,0 @@ -objFromFixture(Location::class, 'dynamic'); - $fields = $object->getCMSFields(); - $this->assertInstanceOf(FieldList::class, $fields); - } - - /** - * - */ - public function testGetFullAddress() - { - - } - - /** - * - */ - public function testHasAddress() - { - - } -} \ No newline at end of file diff --git a/tests/LocationCategoryTest.php b/tests/LocationCategoryTest.php index 6dde93a..8f8aca4 100644 --- a/tests/LocationCategoryTest.php +++ b/tests/LocationCategoryTest.php @@ -17,7 +17,7 @@ class LocationCategoryTest extends SapphireTest /** * @var string */ - protected static $fixture_file = 'locator/tests/fixtures.yml'; + protected static $fixture_file = 'fixtures.yml'; /** * diff --git a/tests/LocationTest.php b/tests/LocationTest.php index a93d68e..79459aa 100644 --- a/tests/LocationTest.php +++ b/tests/LocationTest.php @@ -16,7 +16,7 @@ class LocationTest extends SapphireTest /** * @var string */ - protected static $fixture_file = 'locator/tests/fixtures.yml'; + protected static $fixture_file = 'fixtures.yml'; /** * diff --git a/tests/LocatorFormTest.php b/tests/LocatorFormTest.php index 36fd47c..d00b3ca 100644 --- a/tests/LocatorFormTest.php +++ b/tests/LocatorFormTest.php @@ -14,7 +14,7 @@ class LocatorFormTest extends FunctionalTest /** * @var string */ - protected static $fixture_file = 'locator/tests/fixtures.yml'; + protected static $fixture_file = 'fixtures.yml'; /** * diff --git a/tests/LocatorTest.php b/tests/LocatorTest.php index 59d1d38..820d841 100644 --- a/tests/LocatorTest.php +++ b/tests/LocatorTest.php @@ -17,7 +17,7 @@ class LocatorTest extends FunctionalTest /** * @var string */ - protected static $fixture_file = 'locator/tests/fixtures.yml'; + protected static $fixture_file = 'fixtures.yml'; /** *