Skip to content

Commit

Permalink
Refactor tests to Pest (#476)
Browse files Browse the repository at this point in the history
* install Pest

* refactor `AlternateTest`

* refactor `CrawlProfileTest`

* refactor `ImageTest`

* refactor `SitemapGeneratorTest`

* refactor `SitemapIndexTest`

* refactor `SitemapTest`

* refactor `UrlTest`

* change test suit to Pest in `composer.json`

* cleanup

* change test suit in Github actions

* refactor `temporaryDirectory` method

* refactor helper functions

* cleanup
  • Loading branch information
alexmanase authored Nov 9, 2022
1 parent c93c535 commit 648f713
Show file tree
Hide file tree
Showing 17 changed files with 668 additions and 714 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ jobs:
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest

11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^6.23|^7.0",
"pestphp/pest": "^1.22",
"phpunit/phpunit": "^9.5",
"spatie/pest-plugin-snapshots": "^1.1",
"spatie/phpunit-snapshot-assertions": "^4.0",
"spatie/temporary-directory": "^2.0"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand All @@ -54,6 +59,6 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/pest"
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Spatie Test Suite">
Expand Down
36 changes: 11 additions & 25 deletions tests/AlternateTest.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
<?php

namespace Spatie\Sitemap\Test;

use Spatie\Sitemap\Tags\Alternate;

class AlternateTest extends TestCase
{
protected Alternate $alternate;

public function setUp(): void
{
parent::setUp();

$this->alternate = new Alternate('defaultUrl', 'en');
}
beforeEach(function () {
$this->alternate = new Alternate('defaultUrl', 'en');
});

/** @test */
public function url_can_be_set()
{
$this->alternate->setUrl('testUrl');
test('url can be set', function () {
$this->alternate->setUrl('testUrl');

$this->assertEquals('testUrl', $this->alternate->url);
}
expect($this->alternate->url)->toEqual('testUrl');
});

/** @test */
public function locale_can_be_set()
{
$this->alternate->setLocale('en');
test('locale can be set', function () {
$this->alternate->setLocale('en');

$this->assertEquals('en', $this->alternate->locale);
}
}
expect($this->alternate->locale)->toEqual('en');
});
110 changes: 45 additions & 65 deletions tests/CrawlProfileTest.php
Original file line number Diff line number Diff line change
@@ -1,94 +1,74 @@
<?php

namespace Spatie\Sitemap\Test;

use Spatie\Crawler\Crawler;
use Spatie\Crawler\CrawlProfiles\CrawlInternalUrls;
use Spatie\Crawler\CrawlProfiles\CrawlSubdomains;
use Spatie\Sitemap\Crawler\Profile;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\SitemapGenerator;
use Spatie\Sitemap\Test\CustomCrawlProfile;

class CrawlProfileTest extends TestCase
{
/**
* @var Crawler
*/
protected $crawler;

public function setUp(): void
{
parent::setUp();

$this->crawler = $this->createMock(Crawler::class);
beforeEach(function () {
$this->crawler = $this->createMock(Crawler::class);

$this->crawler->method('setCrawlObserver')->willReturn($this->crawler);
$this->crawler->method('setConcurrency')->willReturn($this->crawler);
}
$this->crawler->method('setCrawlObserver')->willReturn($this->crawler);
$this->crawler->method('setConcurrency')->willReturn($this->crawler);
});

/** @test */
public function it_can_use_the_default_profile()
{
$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(Profile::class))
->willReturn($this->crawler);
it('can use default profile', function () {
$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(Profile::class))
->willReturn($this->crawler);

$sitemapGenerator = new SitemapGenerator($this->crawler);
$sitemapGenerator = new SitemapGenerator($this->crawler);

$sitemap = $sitemapGenerator->setUrl('')->getSitemap();
$sitemap = $sitemapGenerator->setUrl('')->getSitemap();

$this->assertInstanceOf(Sitemap::class, $sitemap);
}
expect($sitemap)->toBeInstanceOf(Sitemap::class);
});

/** @test */
public function it_can_use_the_custom_profile()
{
config(['sitemap.crawl_profile' => CustomCrawlProfile::class]);
it('can use the custom profile', function () {
config(['sitemap.crawl_profile' => CustomCrawlProfile::class]);

$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(CustomCrawlProfile::class))
->willReturn($this->crawler);
$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(CustomCrawlProfile::class))
->willReturn($this->crawler);

$sitemapGenerator = new SitemapGenerator($this->crawler);
$sitemapGenerator = new SitemapGenerator($this->crawler);

$sitemap = $sitemapGenerator->setUrl('')->getSitemap();
$sitemap = $sitemapGenerator->setUrl('')->getSitemap();

$this->assertInstanceOf(Sitemap::class, $sitemap);
}
expect($sitemap)->toBeInstanceOf(Sitemap::class);
});

/** @test */
public function it_can_use_the_subdomain_profile()
{
config(['sitemap.crawl_profile' => CrawlSubdomains::class]);
it('can use the subdomain profile', function () {
config(['sitemap.crawl_profile' => CrawlSubdomains::class]);

$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(CrawlSubdomains::class))
->willReturn($this->crawler);
$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(CrawlSubdomains::class))
->willReturn($this->crawler);

$sitemapGenerator = new SitemapGenerator($this->crawler);
$sitemapGenerator = new SitemapGenerator($this->crawler);

$sitemap = $sitemapGenerator->setUrl('')->getSitemap();
$sitemap = $sitemapGenerator->setUrl('')->getSitemap();

$this->assertInstanceOf(Sitemap::class, $sitemap);
}
expect($sitemap)->toBeInstanceOf(Sitemap::class);
});

/** @test */
public function it_can_use_the_internal_profile()
{
config(['sitemap.crawl_profile' => CrawlInternalUrls::class]);
it('can use the internal profile', function () {
config(['sitemap.crawl_profile' => CrawlInternalUrls::class]);

$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(CrawlInternalUrls::class))
->willReturn($this->crawler);
$this->crawler
->method('setCrawlProfile')
->with($this->isInstanceOf(CrawlInternalUrls::class))
->willReturn($this->crawler);

$sitemapGenerator = new SitemapGenerator($this->crawler);
$sitemapGenerator = new SitemapGenerator($this->crawler);

$sitemap = $sitemapGenerator->setUrl('')->getSitemap();
$sitemap = $sitemapGenerator->setUrl('')->getSitemap();

$this->assertInstanceOf(Sitemap::class, $sitemap);
}
}
expect($sitemap)->toBeInstanceOf(Sitemap::class);
});
22 changes: 8 additions & 14 deletions tests/ImageTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<?php

namespace Spatie\Sitemap\Test;

use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;

class ImageTest extends TestCase
{
public function testXmlHasImage()
{
$expected_xml = '<?xml version="1.0" encoding="UTF-8"?>
test('XML has image', function () {
$expected_xml = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>https://localhost</loc>
Expand All @@ -23,12 +18,11 @@ public function testXmlHasImage()
</url>
</urlset>';

$sitemap = Sitemap::create();
$url = Url::create('https://localhost')->addImage('https://localhost/favicon.ico', 'Favicon');
$sitemap->add($url);
$sitemap = Sitemap::create();
$url = Url::create('https://localhost')->addImage('https://localhost/favicon.ico', 'Favicon');
$sitemap->add($url);

$render_output = $sitemap->render();
$render_output = $sitemap->render();

$this->assertXmlStringEqualsXmlString($expected_xml, $render_output);
}
}
expect($render_output)->toEqualXmlString($expected_xml);
});
63 changes: 63 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Carbon\Carbon;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use function PHPUnit\Framework\assertXmlStringEqualsXmlString;

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
*/

uses(\Spatie\Sitemap\Test\TestCase::class)
->beforeEach(function () {
$this->now = Carbon::create('2016', '1', '1', '0', '0', '0');

Carbon::setTestNow($this->now);
})
->in('.');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
*/

expect()->extend('toEqualXmlString', function (string $expected_xml) {
/** @var string */
$value = $this->value;

assertXmlStringEqualsXmlString($value, $expected_xml);

return $this;
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
*/

function checkIfTestServerIsRunning(): void
{
try {
file_get_contents('http://localhost:4020');
} catch (Throwable $e) {
handleTestServerNotRunning();
}
}

function handleTestServerNotRunning(): void
{
if (getenv('TRAVIS')) {
test()->fail('The test server is not running on Travis.');
}

test()->markTestSkipped('The test server is not running.');
}

function temporaryDirectory(): TemporaryDirectory
{
return (new TemporaryDirectory())->force()->create();
}
Loading

0 comments on commit 648f713

Please sign in to comment.