diff --git a/.editorconfig b/.editorconfig index 042d009..6537ca4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,15 +1,15 @@ -# This file is for unifying the coding style for different editors and IDEs -# editorconfig.org - root = true [*] -end_of_line = lf charset = utf-8 +end_of_line = lf +insert_final_newline = true indent_style = space indent_size = 4 trim_trailing_whitespace = true -insert_final_newline = true [*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 7ccb1f3..22f739a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,6 +7,5 @@ .gitattributes export-ignore .gitignore export-ignore .scrutinizer.yml export-ignore -.travis.yml export-ignore phpunit.xml.dist export-ignore CONTRIBUTING.md export-ignore diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..10b997e --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,46 @@ +name: run-tests + +on: [push] + +jobs: + tests: + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + php: [7.2, 7.3, 7.4] + laravel: [6.*] + dependency-version: [prefer-lowest, prefer-stable] + + name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} + + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Cache dependencies + uses: actions/cache@v1 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php }} + extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv + coverage: xdebug + + - name: Install dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + + - name: Execute tests + run: | + mkdir -p build/logs + vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover + + - name: Scrutinizer CI + run: | + wget https://scrutinizer-ci.com/ocular.phar + php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b4d863c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - nightly - -matrix: - allow_failures: - - php: nightly - -before_script: - - travis_retry composer self-update - - travis_retry composer install --prefer-source --no-interaction - -script: - - composer validate - - mkdir -p build/logs - - ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/LICENSE.md b/LICENSE.md index 35b5760..2ba7e11 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015-2019 | ARCANEDEV - LaravelSitemap +Copyright (c) 2015-2020 | ARCANEDEV - LaravelSitemap Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 070ab36..dbf850e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Laravel Sitemap [![Packagist License][badge_license]](LICENSE.md) [![For Laravel][badge_laravel]][link-github-repo] -[![Travis Status][badge_build]][link-travis] +[![Github Workflow Status][badge_build]][link-github-status] [![Coverage Status][badge_coverage]][link-scrutinizer] [![Scrutinizer Code Quality][badge_quality]][link-scrutinizer] [![SensioLabs Insight][badge_insight]][link-insight] @@ -43,7 +43,7 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. [badge_laravel]: https://img.shields.io/badge/Laravel-5.4%20to%206.x-orange.svg?style=flat-square [badge_license]: https://img.shields.io/packagist/l/arcanedev/laravel-sitemap.svg?style=flat-square -[badge_build]: https://img.shields.io/travis/ARCANEDEV/LaravelSitemap.svg?style=flat-square +[badge_build]: https://img.shields.io/github/workflow/status/ARCANEDEV/LaravelSitemap/run-tests?style=flat-square [badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/LaravelSitemap.svg?style=flat-square [badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/LaravelSitemap.svg?style=flat-square [badge_insight]: https://img.shields.io/sensiolabs/i/40aabf97-8e78-4574-9560-a9ad89704937.svg?style=flat-square @@ -54,9 +54,9 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. [link-author]: https://github.com/arcanedev-maroc [link-github-repo]: https://github.com/ARCANEDEV/LaravelSitemap +[link-github-status]: https://github.com/ARCANEDEV/LaravelSitemap/actions [link-github-issues]: https://github.com/ARCANEDEV/LaravelSitemap/issues [link-contributors]: https://github.com/ARCANEDEV/LaravelSitemap/graphs/contributors [link-packagist]: https://packagist.org/packages/arcanedev/laravel-sitemap -[link-travis]: https://travis-ci.org/ARCANEDEV/LaravelSitemap [link-scrutinizer]: https://scrutinizer-ci.com/g/ARCANEDEV/LaravelSitemap/?branch=master [link-insight]: https://insight.sensiolabs.com/projects/40aabf97-8e78-4574-9560-a9ad89704937 diff --git a/composer.json b/composer.json index a7cd84c..aa2416e 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "php": ">=7.2.0", "ext-dom": "*", "ext-json": "*", - "arcanedev/support": "^5.0" + "arcanedev/support": "^5.1.1" }, "require-dev": { "orchestra/testbench": "^4.0", diff --git a/helpers.php b/helpers.php index 1925eb1..ad870c5 100644 --- a/helpers.php +++ b/helpers.php @@ -6,9 +6,9 @@ /** * Get the Sitemap Manager instance. * - * @return SitemapManager + * @return \Arcanedev\LaravelSitemap\Contracts\SitemapManager */ - function sitemap() { + function sitemap(): SitemapManager { return app(SitemapManager::class); } } diff --git a/src/Contracts/Entities/ChangeFrequency.php b/src/Contracts/Entities/ChangeFrequency.php index 3acddc2..0a68d29 100644 --- a/src/Contracts/Entities/ChangeFrequency.php +++ b/src/Contracts/Entities/ChangeFrequency.php @@ -1,4 +1,10 @@ -mapWithKeys(function ($key) use ($locale) { return [$key => trans("sitemap::frequencies.$key", [], $locale)]; @@ -55,9 +59,9 @@ public static function all($locale = null) * @param string|null $default * @param string|null $locale * - * @return string|null + * @return string|mixed|null */ - public static function get($key, $default = null, $locale = null) + public static function get($key, $default = null, string $locale = null) { return static::all($locale)->get($key, $default); } @@ -65,11 +69,11 @@ public static function get($key, $default = null, $locale = null) /** * Check if the given frequency exists. * - * @param string $key + * @param string $key * * @return bool */ - public static function has($key) + public static function has(string $key): bool { return static::keys()->flip()->has($key); } diff --git a/src/Entities/Sitemap.php b/src/Entities/Sitemap.php index 4e60533..602c5a6 100644 --- a/src/Entities/Sitemap.php +++ b/src/Entities/Sitemap.php @@ -1,4 +1,8 @@ -path = $path; @@ -60,7 +64,7 @@ public function setPath($path) * * @return string|null */ - public function getPath() + public function getPath(): ?string { return $this->path; } @@ -70,7 +74,7 @@ public function getPath() * * @return \Illuminate\Support\Collection */ - public function getUrls() + public function getUrls(): Collection { return $this->urls; } @@ -80,7 +84,7 @@ public function getUrls() * * @param \Illuminate\Support\Collection $urls * - * @return self + * @return $this */ public function setUrls(Collection $urls) { @@ -97,7 +101,7 @@ public function setUrls(Collection $urls) /** * Make a sitemap instance. * - * @return self + * @return $this */ public static function make() { @@ -112,7 +116,7 @@ public static function make() * * @return \Arcanedev\LaravelSitemap\Entities\Url|null */ - public function getUrl($loc, $default = null) + public function getUrl(string $loc, $default = null) { return $this->getUrls()->get($loc, $default); } @@ -134,11 +138,11 @@ public function add(UrlContract $url) /** * Add many urls to the collection. * - * @param array|\Illuminate\Support\Collection $urls + * @param iterable|mixed $urls * - * @return self + * @return $this */ - public function addMany($urls) + public function addMany(iterable $urls) { foreach ($urls as $url) { $this->add($url); @@ -153,9 +157,9 @@ public function addMany($urls) * @param string $loc * @param callable $callback * - * @return self + * @return $this */ - public function create($loc, callable $callback) + public function create(string $loc, callable $callback) { return $this->add(tap(Url::make($loc), $callback)); } @@ -167,7 +171,7 @@ public function create($loc, callable $callback) * * @return bool */ - public function has($url) + public function has(string $url): bool { return $this->urls->has($url); } @@ -177,7 +181,7 @@ public function has($url) * * @return int */ - public function count() + public function count(): int { return $this->urls->count(); } @@ -187,7 +191,7 @@ public function count() * * @return array */ - public function toArray() + public function toArray(): array { return $this->getUrls()->values()->toArray(); } @@ -199,7 +203,7 @@ public function toArray() * * @return string */ - public function toJson($options = 0) + public function toJson($options = 0): string { return json_encode($this->jsonSerialize(), $options); } @@ -209,7 +213,7 @@ public function toJson($options = 0) * * @return array */ - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } @@ -219,7 +223,7 @@ public function jsonSerialize() * * @return bool */ - public function isExceeded() + public function isExceeded(): bool { return $this->count() > $this->getMaxSize(); } @@ -229,15 +233,19 @@ public function isExceeded() * * @return \Illuminate\Support\Collection */ - public function chunk() + public function chunk(): Collection { - return $this->urls->chunk($this->getMaxSize())->mapWithKeys(function ($item, $index) { - $pathInfo = pathinfo($this->getPath()); - $index = $index + 1; - $path = $pathInfo['dirname'].'/'.$pathInfo['filename'].'-'.$index.'.'.$pathInfo['extension']; + return $this->urls + ->chunk($this->getMaxSize()) + ->mapWithKeys(function ($item, $index) { + $pathInfo = pathinfo($this->getPath()); + $index = $index + 1; + $path = $pathInfo['dirname'].'/'.$pathInfo['filename'].'-'.$index.'.'.$pathInfo['extension']; - return [$index => (new Sitemap)->setPath($path)->setUrls($item)]; - }); + return [ + $index => (new Sitemap)->setPath($path)->setUrls($item), + ]; + }); } /* ----------------------------------------------------------------- @@ -250,8 +258,8 @@ public function chunk() * * @return int */ - protected function getMaxSize() + protected function getMaxSize(): int { - return config('sitemap.urls-max-size', 50000); + return (int) config('sitemap.urls-max-size', 50000); } } diff --git a/src/Entities/Url.php b/src/Entities/Url.php index 2d8c462..08df6e3 100644 --- a/src/Entities/Url.php +++ b/src/Entities/Url.php @@ -1,8 +1,13 @@ -escape($this->get('loc')); } @@ -57,7 +62,7 @@ public function getLoc() * * @param string $loc * - * @return self + * @return $this */ public function setLoc($loc) { @@ -69,7 +74,7 @@ public function setLoc($loc) * * @return \DateTimeInterface */ - public function getLastMod() + public function getLastMod(): DateTimeInterface { return $this->get('lastmod'); } @@ -81,7 +86,7 @@ public function getLastMod() * * @return string */ - public function formatLastMod($format = DateTime::ATOM) + public function formatLastMod(string $format = DateTimeInterface::ATOM): string { return $this->getLastMod()->format($format); } @@ -92,9 +97,9 @@ public function formatLastMod($format = DateTime::ATOM) * @param string|\DateTimeInterface $lastModDate * @param string $format * - * @return self + * @return $this */ - public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s') + public function setLastMod($lastModDate, string $format = 'Y-m-d H:i:s') { if (is_string($lastModDate)) $lastModDate = DateTime::createFromFormat($format, $lastModDate); @@ -107,7 +112,7 @@ public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s') * * @return string */ - public function getChangeFreq() + public function getChangeFreq(): string { return $this->get('changefreq'); } @@ -117,9 +122,9 @@ public function getChangeFreq() * * @param string $changeFreq * - * @return self + * @return $this */ - public function setChangeFreq($changeFreq) + public function setChangeFreq(string $changeFreq) { return $this->set('changefreq', strtolower(trim($changeFreq))); } @@ -129,7 +134,7 @@ public function setChangeFreq($changeFreq) * * @return float */ - public function getPriority() + public function getPriority(): float { return $this->get('priority'); } @@ -137,9 +142,9 @@ public function getPriority() /** * Set the priority. * - * @param float $priority + * @param float|mixed $priority * - * @return self + * @return $this */ public function setPriority($priority) { @@ -153,7 +158,7 @@ public function setPriority($priority) * * @return string|null */ - public function getTitle() + public function getTitle(): ?string { return $this->escape($this->get('title')); } @@ -161,11 +166,11 @@ public function getTitle() /** * Get the title. * - * @param string $title + * @param string|null $title * - * @return self + * @return $this */ - public function setTitle($title) + public function setTitle(?string $title) { return $this->set('title', $title); } @@ -176,9 +181,9 @@ public function setTitle($title) * @param string $key * @param mixed $value * - * @return self + * @return $this */ - public function set($key, $value) + public function set(string $key, $value) { $this->attributes[$key] = $value; @@ -195,7 +200,7 @@ public function set($key, $value) * * @param string $loc * - * @return \Arcanedev\LaravelSitemap\Entities\Url + * @return $this */ public static function make($loc) { @@ -207,7 +212,7 @@ public static function make($loc) * * @param array $attributes * - * @return \Arcanedev\LaravelSitemap\Entities\Url + * @return $this */ public static function makeFromArray(array $attributes) { @@ -221,7 +226,7 @@ public static function makeFromArray(array $attributes) * * @return bool */ - public function has($key) + public function has(string $key): bool { return ! is_null($this->get($key)); } @@ -246,18 +251,19 @@ public function toArray() /** * Escape the given value. * - * @param string $value + * @param string|mixed $value * - * @return string + * @return string|null */ protected function escape($value) { if (is_null($value)) return $value; - return config('sitemap.escaping', true) - ? htmlentities($value, ENT_XML1, 'UTF-8') - : $value; + if (config('sitemap.escaping', true)) + $value = htmlentities($value, ENT_XML1, 'UTF-8'); + + return $value; } /** @@ -269,9 +275,9 @@ protected function escape($value) * * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException */ - protected function checkLoc($loc) + protected function checkLoc($loc): string { - if (is_null($loc) || ! is_string($loc)) + if ( ! is_string($loc)) throw new SitemapException('The [loc] attribute is required and must be string value.'); return $loc; @@ -280,13 +286,13 @@ protected function checkLoc($loc) /** * Check the priority value. * - * @param float $priority + * @param float|mixed $priority * * @return float * * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException */ - protected function checkPriority($priority) + protected function checkPriority($priority): float { if ( ! is_numeric($priority)) throw new SitemapException("The [priority] value must be numeric."); diff --git a/src/Exceptions/SitemapException.php b/src/Exceptions/SitemapException.php index 149e41d..e64c039 100644 --- a/src/Exceptions/SitemapException.php +++ b/src/Exceptions/SitemapException.php @@ -1,4 +1,8 @@ -registerConfig(); - $this->singleton(Contracts\SitemapManager::class, SitemapManager::class); + $this->singleton(SitemapManagerContract::class, SitemapManager::class); } @@ -59,7 +64,7 @@ public function boot(): void public function provides(): array { return [ - Contracts\SitemapManager::class, + SitemapManagerContract::class, ]; } } diff --git a/src/SitemapBuilder.php b/src/SitemapBuilder.php index d9c4706..f467da1 100644 --- a/src/SitemapBuilder.php +++ b/src/SitemapBuilder.php @@ -1,4 +1,8 @@ -isEmpty()) return null; @@ -75,7 +79,7 @@ public function build($name, Collection $sitemaps, $format) * * @return string|null */ - protected function renderSitemap($format, Sitemap $sitemap = null, $key = null) + protected function renderSitemap(string $format, Sitemap $sitemap = null, string $key = null) { if (is_null($sitemap)) return null; @@ -85,9 +89,10 @@ protected function renderSitemap($format, Sitemap $sitemap = null, $key = null) $chunks = $sitemap->chunk(); - return is_null($key) - ? static::renderSitemapIndex($format, $chunks) - : static::renderSitemap($format, $chunks->get($key)); + if (is_null($key)) + return static::renderSitemapIndex($format, $chunks); + + return static::renderSitemap($format, $chunks->get($key)); } /** @@ -100,7 +105,7 @@ protected function renderSitemap($format, Sitemap $sitemap = null, $key = null) * * @return string|null */ - protected function renderSitemapIndex($format, $sitemaps) + protected function renderSitemapIndex(string $format, Collection $sitemaps): ?string { return static::render($format, 'sitemap-index', compact('sitemaps')); } @@ -116,9 +121,11 @@ protected function renderSitemapIndex($format, $sitemaps) * * @return string|null */ - protected function render($format, $type, array $data) + protected function render(string $format, string $type, array $data): ?string { - switch ($format= strtolower(trim($format))) { + $format = strtolower(trim($format)); + + switch ($format) { case 'xml': case 'rss': return static::renderXml($format, $type, $data); @@ -140,7 +147,7 @@ protected function render($format, $type, array $data) * * @return string */ - protected function renderXml($format, $type, array $data) + protected function renderXml(string $format, string $type, array $data): string { return tap(new DOMDocument('1.0'), function (DOMDocument $document) use ($format, $type, $data) { $document->preserveWhiteSpace = false; @@ -162,7 +169,7 @@ protected function renderXml($format, $type, array $data) * * @return string */ - protected function renderView($type, $format, array $data) + protected function renderView(string $type, string $format, array $data): string { return view("sitemap::{$type}.{$format}", $data)->render(); } diff --git a/src/SitemapManager.php b/src/SitemapManager.php index 30af65c..6641ac2 100644 --- a/src/SitemapManager.php +++ b/src/SitemapManager.php @@ -1,9 +1,14 @@ -add($name, tap(Sitemap::make()->setPath($name), $callback)); } @@ -80,9 +85,9 @@ public function create($name, callable $callback) * @param string $name * @param \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap $sitemap * - * @return self + * @return $this */ - public function add($name, SitemapContract $sitemap) + public function add(string $name, SitemapContract $sitemap) { $this->sitemaps->put($name, $sitemap); @@ -94,7 +99,7 @@ public function add($name, SitemapContract $sitemap) * * @return \Illuminate\Support\Collection */ - public function all() + public function all(): Collection { return $this->sitemaps; } @@ -105,9 +110,9 @@ public function all() * @param string $name * @param mixed|null $default * - * @return mixed + * @return \Arcanedev\LaravelSitemap\Entities\Sitemap|mixed|null */ - public function get($name, $default = null) + public function get(string $name, $default = null) { return $this->sitemaps->get($name, $default); } @@ -119,7 +124,7 @@ public function get($name, $default = null) * * @return bool */ - public function has($name) + public function has(string $name): bool { if ( ! Str::contains($name, '.')) return $this->sitemaps->has($name); @@ -140,7 +145,7 @@ public function has($name) * * @param string|array $names * - * @return self + * @return $this */ public function forget($names) { @@ -154,7 +159,7 @@ public function forget($names) * * @return int */ - public function count() + public function count(): int { return $this->sitemaps->count(); } @@ -168,7 +173,7 @@ public function count() * * @return string|null */ - public function render($name = null) + public function render(string $name = null): ?string { return SitemapBuilder::make()->build($name, $this->sitemaps, $this->format); } @@ -181,9 +186,9 @@ public function render($name = null) * * @throws \Throwable * - * @return self + * @return $this */ - public function save($path, $name = null) + public function save(string $path, string $name = null) { if ($this->sitemaps->isEmpty()) return $this; @@ -208,9 +213,9 @@ public function save($path, $name = null) * * @throws \Throwable * - * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response + * @return \Illuminate\Http\Response|mixed */ - public function respond($name = null, $status = 200, array $headers = []) + public function respond(string $name = null, int $status = Response::HTTP_OK, array $headers = []) { return response($this->render($name), $status, array_merge($this->getResponseHeaders(), $headers)); } @@ -220,7 +225,7 @@ public function respond($name = null, $status = 200, array $headers = []) * * @return array */ - public function toArray() + public function toArray(): array { return $this->all()->toArray(); } @@ -232,7 +237,7 @@ public function toArray() * * @return string */ - public function toJson($options = 0) + public function toJson($options = 0): string { return json_encode($this->jsonSerialize(), $options); } @@ -242,7 +247,7 @@ public function toJson($options = 0) * * @return array */ - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } @@ -255,7 +260,7 @@ public function jsonSerialize() * * @throws \Throwable */ - private function saveMultiple($path, $sitemap) + private function saveMultiple(string $path, SitemapContract $sitemap) { $pathInfo = pathinfo($path); $chunks = $sitemap->chunk(); @@ -263,7 +268,7 @@ private function saveMultiple($path, $sitemap) foreach ($chunks as $key => $item) { file_put_contents( $pathInfo['dirname'].DS.$pathInfo['filename'].'-'.$key.'.'.$pathInfo['extension'], - SitemapBuilder::make()->build($key, $chunks, $this->format) + SitemapBuilder::make()->build((string) $key, $chunks, $this->format) ); } } @@ -273,7 +278,7 @@ private function saveMultiple($path, $sitemap) * * @return array */ - protected function getResponseHeaders() + protected function getResponseHeaders(): array { return Arr::get([ 'xml' => ['Content-Type' => 'application/xml'], diff --git a/tests/Entities/ChangeFrequencyTest.php b/tests/Entities/ChangeFrequencyTest.php index 8fd7b74..128443e 100644 --- a/tests/Entities/ChangeFrequencyTest.php +++ b/tests/Entities/ChangeFrequencyTest.php @@ -1,4 +1,8 @@ -sitemap->count()); - $this->sitemap->add($this->createUrlSample()); + $this->sitemap->add(static::createUrlSample()); static::assertSame(1, $this->sitemap->count()); } /** @test */ - public function it_can_create_and_add_url_to_collection() + public function it_can_create_and_add_url_to_collection(): void { static::assertSame(0, $this->sitemap->count()); @@ -127,9 +136,9 @@ public function it_can_create_and_add_url_to_collection() } /** @test */ - public function it_can_check_if_has_an_existing_url() + public function it_can_check_if_has_an_existing_url(): void { - $url = $this->createUrlSample(); + $url = static::createUrlSample(); static::assertFalse($this->sitemap->has($url->getLoc())); @@ -139,9 +148,9 @@ public function it_can_check_if_has_an_existing_url() } /** @test */ - public function it_can_get_url_by_its_loc() + public function it_can_get_url_by_its_loc(): void { - $url = $this->createUrlSample(); + $url = static::createUrlSample(); static::assertNull($this->sitemap->getUrl($url->getLoc())); @@ -151,11 +160,11 @@ public function it_can_get_url_by_its_loc() } /** @test */ - public function it_can_convert_to_array() + public function it_can_convert_to_array(): void { static::assertSame([], $this->sitemap->toArray()); - $this->sitemap->add($this->createUrlSample()); + $this->sitemap->add(static::createUrlSample()); $expected = [ [ @@ -171,14 +180,14 @@ public function it_can_convert_to_array() } /** @test */ - public function it_can_convert_to_json() + public function it_can_convert_to_json(): void { $expected = '[]'; static::assertSame($expected, json_encode($this->sitemap)); static::assertSame($expected, $this->sitemap->toJson()); - $this->sitemap->add($this->createUrlSample()); + $this->sitemap->add(static::createUrlSample()); $expected = json_encode($this->sitemap->toArray()); @@ -187,7 +196,7 @@ public function it_can_convert_to_json() } /** @test */ - public function it_should_treats_urls_as_the_basic_collection_class() + public function it_should_treats_urls_as_the_basic_collection_class(): void { $this->sitemap = $this->createBlogSitemap(); @@ -197,7 +206,7 @@ public function it_should_treats_urls_as_the_basic_collection_class() } /** @test */ - public function it_can_check_if_number_of_urls_is_exceeded() + public function it_can_check_if_number_of_urls_is_exceeded(): void { // Max is 500 for tests @@ -218,9 +227,9 @@ public function it_can_check_if_number_of_urls_is_exceeded() } /** @test */ - public function it_can_set_urls() + public function it_can_set_urls(): void { - $this->sitemap->add($this->createUrlSample()); + $this->sitemap->add(static::createUrlSample()); static::assertSame(1, $this->sitemap->count()); @@ -230,7 +239,7 @@ public function it_can_set_urls() ['loc' => 'http://example.com/contact'], ]); - $now = new \DateTime; + $now = new DateTime; $this->sitemap->setUrls($urls->transform(function (array $item) use ($now) { return Url::makeFromArray($item)->setLastMod($now); @@ -238,7 +247,7 @@ public function it_can_set_urls() static::assertSame(3, $this->sitemap->count()); - $formattedDate = $now->format(\DateTime::ATOM); + $formattedDate = $now->format(DateTime::ATOM); $expected = [ [ @@ -266,9 +275,9 @@ public function it_can_set_urls() } /** @test */ - public function it_can_add_many_urls_to_the_collection() + public function it_can_add_many_urls_to_the_collection(): void { - $this->sitemap->add($this->createUrlSample()); + $this->sitemap->add(static::createUrlSample()); static::assertSame(1, $this->sitemap->count()); @@ -278,7 +287,7 @@ public function it_can_add_many_urls_to_the_collection() ['loc' => 'http://example.com/contact'], ]); - $now = new \DateTime; + $now = new DateTime; $this->sitemap->addMany($urls->transform(function (array $item) use ($now) { return Url::makeFromArray($item)->setLastMod($now); @@ -286,7 +295,7 @@ public function it_can_add_many_urls_to_the_collection() static::assertSame(4, $this->sitemap->count()); - $formattedDate = $now->format(\DateTime::ATOM); + $formattedDate = $now->format(DateTime::ATOM); $expected = [ [ @@ -329,7 +338,7 @@ public function it_can_add_many_urls_to_the_collection() * * @return \Arcanedev\LaravelSitemap\Entities\Url */ - private function createUrlSample() + private static function createUrlSample(): Url { return Url::make('http://example.com') ->setChangeFreq(ChangeFrequency::ALWAYS) diff --git a/tests/Entities/UrlTest.php b/tests/Entities/UrlTest.php index a80d623..bd3dc64 100644 --- a/tests/Entities/UrlTest.php +++ b/tests/Entities/UrlTest.php @@ -1,8 +1,17 @@ -url = new Url([ 'title' => 'Home page', 'loc' => 'http://example.com', - 'lastmod' => $now = new \DateTime, + 'lastmod' => $now = new DateTime, 'changefreq' => 'hourly', 'priority' => 1.0, ]); @@ -73,7 +82,7 @@ public function it_can_also_instantiate_with_array() $expected = [ 'title' => 'Home page', 'loc' => 'http://example.com', - 'lastmod' => $now->format(\DateTime::ATOM), + 'lastmod' => $now->format(DateTime::ATOM), 'changefreq' => 'hourly', 'priority' => 1.0, ]; @@ -82,10 +91,10 @@ public function it_can_also_instantiate_with_array() } /** @test */ - public function it_can_make() + public function it_can_make(): void { static::assertInstanceOf( - \Arcanedev\LaravelSitemap\Entities\Url::class, + Url::class, $url = Url::make($loc = 'http://example.com') ); @@ -93,11 +102,11 @@ public function it_can_make() } /** @test */ - public function it_can_make_from_array() + public function it_can_make_from_array(): void { $url = Url::makeFromArray(['loc' => 'http://example.com']); // With minimal attributes - static::assertInstanceOf(\Arcanedev\LaravelSitemap\Entities\Url::class, $url); + static::assertInstanceOf(Url::class, $url); $expected = [ 'loc' => 'http://example.com', @@ -109,7 +118,7 @@ public function it_can_make_from_array() static::assertSame($expected, $url->toArray()); - $now = new \DateTime(); + $now = new DateTime(); $url = Url::makeFromArray([ 'title' => 'Contact Page', 'loc' => 'http://example.com/contact', @@ -121,7 +130,7 @@ public function it_can_make_from_array() $expected = [ 'title' => 'Contact Page', 'loc' => 'http://example.com/contact', - 'lastmod' => $now->format(\DateTime::ATOM), + 'lastmod' => $now->format(DateTime::ATOM), 'changefreq' => 'monthly', 'priority' => 0.5, ]; @@ -130,7 +139,7 @@ public function it_can_make_from_array() } /** @test */ - public function it_can_get_and_set_location() + public function it_can_get_and_set_location(): void { static::assertSame('http://example.com', $this->url->getLoc()); @@ -140,22 +149,22 @@ public function it_can_get_and_set_location() } /** @test */ - public function it_can_get_and_set_last_modification_date() + public function it_can_get_and_set_last_modification_date(): void { - static::assertInstanceOf(\DateTime::class, $this->url->getLastMod()); + static::assertInstanceOf(DateTime::class, $this->url->getLastMod()); static::assertSame(date('Y-m-d H:i'), $this->url->getLastMod()->format('Y-m-d H:i')); $this->url->setLastMod($date = '2017-01-01 00:00:00'); // String date static::assertSame($date, $this->url->getLastMod()->format('Y-m-d H:i:s')); - $this->url->setLastMod($date = new \DateTime); + $this->url->setLastMod($date = new DateTime); static::assertSame($date, $this->url->getLastMod()); } /** @test */ - public function it_can_get_and_set_change_freq() + public function it_can_get_and_set_change_freq(): void { static::assertSame(ChangeFrequency::DAILY, $this->url->getChangeFreq()); @@ -165,7 +174,7 @@ public function it_can_get_and_set_change_freq() } /** @test */ - public function it_can_get_and_set_priority() + public function it_can_get_and_set_priority(): void { static::assertSame(0.8, $this->url->getPriority()); @@ -175,7 +184,7 @@ public function it_can_get_and_set_priority() } /** @test */ - public function it_can_get_and_set_title() + public function it_can_get_and_set_title(): void { static::assertNull($this->url->getTitle()); @@ -185,7 +194,7 @@ public function it_can_get_and_set_title() } /** @test */ - public function it_can_convert_to_array() + public function it_can_convert_to_array(): void { $this->url->setLastMod('2017-01-01 00:00:00'); @@ -201,7 +210,7 @@ public function it_can_convert_to_array() } /** @test */ - public function it_can_convert_to_json() + public function it_can_convert_to_json(): void { $this->url->setLastMod('2017-01-01 00:00:00'); @@ -212,7 +221,7 @@ public function it_can_convert_to_json() } /** @test */ - public function it_must_escape_the_url_location() + public function it_must_escape_the_url_location(): void { $url = Url::make('http://www.example.com/ümlat.php&q=name') ->setTitle('world'); @@ -222,47 +231,47 @@ public function it_must_escape_the_url_location() } /** @test */ - public function it_must_fail_if_loc_is_invalid_1() + public function it_must_fail_if_loc_is_invalid_1(): void { - $this->expectException(\Arcanedev\LaravelSitemap\Exceptions\SitemapException::class); + $this->expectException(SitemapException::class); $this->expectExceptionMessage('The [loc] attribute is required and must be string value.'); Url::make(null); } /** @test */ - public function it_must_fail_if_loc_is_invalid_2() + public function it_must_fail_if_loc_is_invalid_2(): void { - $this->expectException(\Arcanedev\LaravelSitemap\Exceptions\SitemapException::class); + $this->expectException(SitemapException::class); $this->expectExceptionMessage('The [loc] attribute is required and must be string value.'); Url::make(true); } /** @test */ - public function it_must_fail_if_priority_is_invalid_1() + public function it_must_fail_if_priority_is_invalid_1(): void { - $this->expectException(\Arcanedev\LaravelSitemap\Exceptions\SitemapException::class); + $this->expectException(SitemapException::class); $this->expectExceptionMessage('The [priority] value must be numeric.'); Url::make($this->baseUrl)->setPriority('foo'); } /** @test */ - public function it_must_fail_if_priority_is_invalid_2() + public function it_must_fail_if_priority_is_invalid_2(): void { - $this->expectException(\Arcanedev\LaravelSitemap\Exceptions\SitemapException::class); + $this->expectException(SitemapException::class); $this->expectExceptionMessage('The [priority] value must be between `0.0` and `1.0`, `2` was given.'); Url::make($this->baseUrl)->setPriority('2.0'); } /** @test */ - public function it_can_manipulate_extra_attributes() + public function it_can_manipulate_extra_attributes(): void { $url = Url::makeFromArray([ 'loc' => 'http://example.com', - 'lastmod' => \Carbon\Carbon::create(2017, 01, 01, 00, 00, 00), + 'lastmod' => Carbon::create(2017, 01, 01, 00, 00, 00), 'changefreq' => ChangeFrequency::DAILY, 'priority' => 1.0, 'title' => 'Hello world', diff --git a/tests/LaravelSitemapServiceProviderTest.php b/tests/LaravelSitemapServiceProviderTest.php index 37599fe..d3f075c 100644 --- a/tests/LaravelSitemapServiceProviderTest.php +++ b/tests/LaravelSitemapServiceProviderTest.php @@ -1,4 +1,10 @@ -provider = $this->app->getProvider(\Arcanedev\LaravelSitemap\LaravelSitemapServiceProvider::class); + $this->provider = $this->app->getProvider(LaravelSitemapServiceProvider::class); } protected function tearDown(): void @@ -41,14 +47,14 @@ protected function tearDown(): void */ /** @test */ - public function it_can_be_instantiated() + public function it_can_be_instantiated(): void { $expectations = [ \Illuminate\Support\ServiceProvider::class, \Illuminate\Contracts\Support\DeferrableProvider::class, \Arcanedev\Support\Providers\ServiceProvider::class, \Arcanedev\Support\Providers\PackageServiceProvider::class, - \Arcanedev\LaravelSitemap\LaravelSitemapServiceProvider::class, + LaravelSitemapServiceProvider::class, ]; foreach ($expectations as $expected) { @@ -57,7 +63,7 @@ public function it_can_be_instantiated() } /** @test */ - public function it_can_provides() + public function it_can_provides(): void { $expected = [ \Arcanedev\LaravelSitemap\Contracts\SitemapManager::class, diff --git a/tests/SitemapManagerTest.php b/tests/SitemapManagerTest.php index cee7880..dbeb229 100644 --- a/tests/SitemapManagerTest.php +++ b/tests/SitemapManagerTest.php @@ -1,7 +1,12 @@ -manager = $this->app->make(\Arcanedev\LaravelSitemap\Contracts\SitemapManager::class); + $this->manager = $this->app->make(SitemapManager::class); } protected function tearDown(): void @@ -52,10 +57,10 @@ protected function tearDown(): void */ /** @test */ - public function it_can_be_instantiated() + public function it_can_be_instantiated(): void { $expectations = [ - \Arcanedev\LaravelSitemap\Contracts\SitemapManager::class, + SitemapManager::class, \Arcanedev\LaravelSitemap\SitemapManager::class, ]; @@ -65,14 +70,14 @@ public function it_can_be_instantiated() } /** @test */ - public function it_should_return_empty_sitemaps_collection_on_creation() + public function it_should_return_empty_sitemaps_collection_on_creation(): void { static::assertCount(0, $this->manager->all()); static::assertSame(0, $this->manager->count()); } /** @test */ - public function it_can_add_a_sitemap_to_collection() + public function it_can_add_a_sitemap_to_collection(): void { $this->manager->add('pages', $sitemap = new Sitemap); @@ -83,7 +88,7 @@ public function it_can_add_a_sitemap_to_collection() } /** @test */ - public function it_can_get_a_sitemap_by_its_name() + public function it_can_get_a_sitemap_by_its_name(): void { $this->manager->add('blog', $sitemap = new Sitemap); @@ -92,11 +97,11 @@ public function it_can_get_a_sitemap_by_its_name() $sitemap = $this->manager->get('blog'); - static::assertInstanceOf(\Arcanedev\LaravelSitemap\Entities\Sitemap::class, $sitemap); + static::assertInstanceOf(Sitemap::class, $sitemap); } /** @test */ - public function it_can_create_and_add_a_sitemap_to_collection() + public function it_can_create_and_add_a_sitemap_to_collection(): void { $this->manager->create('pages', function (Sitemap $sitemap) { $sitemap->add(Url::make('http://example.com')); @@ -111,7 +116,7 @@ public function it_can_create_and_add_a_sitemap_to_collection() } /** @test */ - public function it_can_forget_sitemaps_from_collection() + public function it_can_forget_sitemaps_from_collection(): void { $this->populatedManager(); @@ -123,7 +128,7 @@ public function it_can_forget_sitemaps_from_collection() } /** @test */ - public function it_can_convert_to_array() + public function it_can_convert_to_array(): void { static::assertSame([], $this->manager->toArray()); @@ -141,7 +146,7 @@ public function it_can_convert_to_array() } /** @test */ - public function it_can_convert_to_json() + public function it_can_convert_to_json(): void { $expected = '[]'; @@ -158,7 +163,7 @@ public function it_can_convert_to_json() /** @test */ - public function it_can_render_sitemaps() + public function it_can_render_sitemaps(): void { $this->populatedManager(); @@ -170,13 +175,13 @@ public function it_can_render_sitemaps() } /** @test */ - public function it_should_return_null_when_rendering_an_empty_manager() + public function it_should_return_null_when_rendering_an_empty_manager(): void { static::assertNull($this->manager->render()); } /** @test */ - public function it_can_save_sitemap_index() + public function it_can_save_sitemap_index(): void { $directory = __DIR__.'/__temp__'; $this->manager->save($path = "$directory/sitemap.xml"); @@ -194,7 +199,7 @@ public function it_can_save_sitemap_index() } /** @test */ - public function it_can_save_sitemaps() + public function it_can_save_sitemaps(): void { $directory = __DIR__.'/__temp__'; @@ -213,7 +218,7 @@ public function it_can_save_sitemaps() } /** @test */ - public function it_can_save_the_single_sitemap_instead_of_index() + public function it_can_save_the_single_sitemap_instead_of_index(): void { $directory = __DIR__.'/__temp__'; $path = "$directory/sitemap.xml"; @@ -229,7 +234,7 @@ public function it_can_save_the_single_sitemap_instead_of_index() } /** @test */ - public function it_can_switch_sitemap_format_to_txt() + public function it_can_switch_sitemap_format_to_txt(): void { $this->manager->format('txt'); @@ -245,7 +250,7 @@ public function it_can_switch_sitemap_format_to_txt() } /** @test */ - public function it_can_switch_sitemap_format_to_rss() + public function it_can_switch_sitemap_format_to_rss(): void { $this->manager->format('rss'); @@ -261,7 +266,7 @@ public function it_can_switch_sitemap_format_to_rss() } /** @test */ - public function it_must_return_null_if_format_is_invalid() + public function it_must_return_null_if_format_is_invalid(): void { $this->populatedManager(); @@ -273,7 +278,7 @@ public function it_must_return_null_if_format_is_invalid() } /** @test */ - public function it_can_chunk_a_huge_sitemap_on_render() + public function it_can_chunk_a_huge_sitemap_on_render(): void { $this->manager->add('blog', $this->createBlogSitemap(499)); @@ -291,7 +296,7 @@ public function it_can_chunk_a_huge_sitemap_on_render() } /** @test */ - public function it_can_chunk_a_huge_sitemap_on_save() + public function it_can_chunk_a_huge_sitemap_on_save(): void { $this->manager->add('blog', $this->createBlogSitemap(499)); @@ -316,13 +321,13 @@ public function it_can_chunk_a_huge_sitemap_on_save() } /** @test */ - public function it_can_respond_with_http_response() + public function it_can_respond_with_http_response(): void { $response = $this->manager->respond(); - static::assertInstanceOf(\Illuminate\Http\Response::class, $response); + static::assertInstanceOf(Response::class, $response); - static::assertSame(200, $response->getStatusCode()); + static::assertSame(Response::HTTP_OK, $response->getStatusCode()); static::assertSame('', $response->getContent()); static::assertSame('application/xml', $response->headers->get('content-type')); @@ -330,19 +335,19 @@ public function it_can_respond_with_http_response() $response = $this->manager->respond(); - static::assertSame(200, $response->getStatusCode()); + static::assertSame(Response::HTTP_OK, $response->getStatusCode()); static::assertMatchesXmlSnapshot($response->getContent()); static::assertSame('application/xml', $response->headers->get('content-type')); $response = $this->manager->format('txt')->respond(); - static::assertSame(200, $response->getStatusCode()); + static::assertSame(Response::HTTP_OK, $response->getStatusCode()); static::assertMatchesSnapshot($response->getContent()); static::assertSame('text/plain', $response->headers->get('content-type')); $response = $this->manager->format('rss')->respond('blog'); - static::assertSame(200, $response->getStatusCode()); + static::assertSame(Response::HTTP_OK, $response->getStatusCode()); static::assertMatchesSnapshot($response->getContent()); static::assertSame('application/rss+xml', $response->headers->get('content-type')); } @@ -355,7 +360,7 @@ public function it_can_respond_with_http_response() /** * Populate the manager with sitemaps. */ - private function populatedManager() + private function populatedManager(): void { $this->manager->add('pages', $this->createPagesSitemap()); $this->manager->add('blog', $this->createBlogSitemap()); diff --git a/tests/TestCase.php b/tests/TestCase.php index 78d3558..d328a4d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,8 +1,11 @@ -set('sitemap.urls-max-size', 100); } @@ -54,7 +57,7 @@ protected function getEnvironmentSetUp($app) * @param array $array * @param string $message */ - public static function assertArrayHasKeys(array $keys, $array, $message = '') + public static function assertArrayHasKeys(array $keys, $array, $message = ''): void { foreach ($keys as $key) { static::assertArrayHasKey($key, $array, $message); @@ -67,9 +70,11 @@ public static function assertArrayHasKeys(array $keys, $array, $message = '') */ /** + * Create 'pages' sitemap. + * * @return \Arcanedev\LaravelSitemap\Entities\Sitemap */ - protected function createPagesSitemap() + protected function createPagesSitemap(): Sitemap { return tap(new Sitemap, function (Sitemap $sitemap) { $baseUrl = 'http://example.com'; @@ -83,11 +88,13 @@ protected function createPagesSitemap() } /** + * Create `blog` sitemap. + * * @param int $times * * @return \Arcanedev\LaravelSitemap\Entities\Sitemap */ - protected function createBlogSitemap($times = 10) + protected function createBlogSitemap($times = 10): Sitemap { return tap(new Sitemap, function (Sitemap $sitemap) use ($times) { $baseUrl = 'http://example.com';