Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"scrivo/highlight.php": "^9.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5"
"phpunit/phpunit": "^8.5",
"spatie/phpunit-snapshot-assertions": "^3.0 || ^4.0"
},
"repositories": [
{
Expand Down
133 changes: 26 additions & 107 deletions tests/commands/ApiControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
Expand All @@ -7,7 +8,9 @@

namespace yiiunit\apidoc\commands;

use Spatie\Snapshots\MatchesSnapshots;
use Yii;
use yii\helpers\FileHelper;
use yiiunit\apidoc\support\controllers\ApiControllerMock;
use yiiunit\apidoc\TestCase;

Expand All @@ -16,6 +19,8 @@
*/
class ApiControllerTest extends TestCase
{
use MatchesSnapshots;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -61,118 +66,32 @@ public function testNoFiles()

public function testGenerateBootstrap()
{
$output = $this->generateApi(Yii::getAlias('@yiiunit/apidoc/data/api'), '@runtime', ['template' => 'bootstrap']);
$sourceFilesDir = Yii::getAlias('@yiiunit/apidoc/data/api');
$output = $this->generateApi($sourceFilesDir, '@runtime', ['template' => 'bootstrap']);

$this->assertNotEmpty($output);
$this->assertStringContainsString('generating search index...done.', $output);

$filesCount = 0;
$outputPath = Yii::getAlias('@runtime');

// Class `Animal` :
$animalFile = $outputPath . DIRECTORY_SEPARATOR . 'yiiunit-apidoc-data-api-animal-animal.html';
$this->assertTrue(file_exists($animalFile));
$animalContent = file_get_contents($animalFile);
$this->assertStringContainsString('<h1>Abstract Class yiiunit\apidoc\data\api\animal\Animal</h1>', $animalContent);
$this->assertStringContainsString('<th>Available since version</th><td>1.0</td>', $animalContent);
$this->assertStringContainsString('Animal is a base class for animals.', $animalContent);
$this->assertContainsWithoutIndent(
<<<HTML
<td colspan="3" class="signature">
<span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.integer">integer</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#getSomething()-detail">getSomething</a></strong> ( <span style="color: #0000BB">\$test</span> )
</td>
</tr>
<tr>
<td class="param-name-col"><span style="color: #0000BB">\$test</span></td>
<td class="param-type-col"></td>
<td class="param-desc-col"> </td>
</tr>
<tr>
<th class="param-name-col">return</th>
<td class="param-type-col"><a href="https://www.php.net/language.types.integer">integer</a></td>
<td class="param-desc-col"></td>
</tr>
HTML
, $animalContent
);
$this->assertContainsWithoutIndent(
<<<HTML
<tr id="\$name" class="">
<td><a href="yiiunit-apidoc-data-api-animal-animal.html#\$name-detail">\$name</a></td>
<td><a href="https://www.php.net/language.types.string">string</a></td>
<td>Animal name.</td>
<td><a href="yiiunit-apidoc-data-api-animal-animal.html">yiiunit\apidoc\data\api\animal\Animal</a></td>
</tr>
HTML
, $animalContent
);
$this->assertContainsWithoutIndent(
<<<HTML
<tr id="\$birthDate" class="">
<td><a href="yiiunit-apidoc-data-api-animal-animal.html#\$birthDate-detail">\$birthDate</a></td>
<td><a href="https://www.php.net/language.types.integer">integer</a></td>
<td>Animal birth date as a UNIX timestamp.</td>
<td><a href="yiiunit-apidoc-data-api-animal-animal.html">yiiunit\apidoc\data\api\animal\Animal</a></td>
</tr>
HTML
, $animalContent
);
$this->assertContainsWithoutIndent(
<<<HTML
<tr id="getAge()" class="">
<td><a href="yiiunit-apidoc-data-api-animal-animal.html#getAge()-detail">getAge()</a></td>
<td>Returns animal age in seconds.</td>
<td><a href="yiiunit-apidoc-data-api-animal-animal.html">yiiunit\apidoc\data\api\animal\Animal</a></td>
</tr>
HTML
, $animalContent
);
$this->assertContainsWithoutIndent(
<<<HTML
<tr id="render()" class="">
<td><a href="yiiunit-apidoc-data-api-animal-animal.html#render()-detail">render()</a></td>
<td>Renders animal description.</td>
<td><a href="yiiunit-apidoc-data-api-animal-animal.html">yiiunit\apidoc\data\api\animal\Animal</a></td>
</tr>
HTML
, $animalContent
);

$this->assertContainsWithoutIndent(
<<<HTML
<tr id="isOlder()" class="">
<td><a href="yiiunit-apidoc-data-api-animal-animal.html#isOlder()-detail">isOlder()</a></td>
<td>Checks whether the animal is older than the specified time.</td>
<td><a href="yiiunit-apidoc-data-api-animal-animal.html">yiiunit\apidoc\data\api\animal\Animal</a></td>
</tr>
HTML
, $animalContent
);

$this->assertContainsWithoutIndent(
<<<HTML
<tr><td colspan="3" class="signature"><span class="signature-defs">public</span> <span class="signature-type"><a href="https://www.php.net/language.types.boolean">boolean</a></span> <strong><a href="yiiunit-apidoc-data-api-animal-animal.html#isOlder()-detail">isOlder</a></strong> ( <span style="color: #0000BB">\$date</span> )</td></tr>
<tr>
<td class="param-name-col"><span style="color: #0000BB">\$date</span></td>
<td class="param-type-col"><a href="https://www.php.net/language.types.integer">integer</a></td>
<td class="param-desc-col">
<p>Date as a UNIX timestamp.</p>
</td>
</tr>
HTML
, $animalContent
);

// Class `Dog` :
$dogFile = $outputPath . DIRECTORY_SEPARATOR . 'yiiunit-apidoc-data-api-animal-dog.html';
$this->assertTrue(file_exists($dogFile));
$dogContent = file_get_contents($dogFile);
$this->assertStringContainsString('<th>Available since version</th><td>1.1</td>', $dogContent);
$this->assertStringNotContainsString('@inheritdoc', $dogContent);

// Class `Cat` :
$catFile = $outputPath . DIRECTORY_SEPARATOR . 'yiiunit-apidoc-data-api-animal-cat.html';
$this->assertTrue(file_exists($catFile));
$catContent = file_get_contents($catFile);
$this->assertStringNotContainsString('@inheritdoc', $catContent);
foreach (glob("{$outputPath}/yiiunit-apidoc-data-api*") as $filePath) {
$fileContent = file_get_contents($filePath);

// Deleting dynamic content
$fileContent = preg_replace('/<p\s+class="pull-right">.*?<\/p>/is', '', $fileContent);
$fileContent = preg_replace('/<script\s+src=".*?"><\/script>/is', '', $fileContent);
$fileContent = preg_replace('/<link\s+href=".*?" rel="stylesheet">/is', '', $fileContent);
$fileContent = preg_replace('/\s+id\s*=\s*(["\'])[^"\']*\1/i', '', $fileContent);
$fileContent = preg_replace('/\s+href\s*=\s*(["\'])#[^"\']*\1/i', '', $fileContent);
$fileContent = preg_replace('/\s+data-target\s*=\s*(["\'])#[^"\']*\1/i', '', $fileContent);

$this->assertMatchesHtmlSnapshot($fileContent);
$filesCount++;
}

$sourceFilesCount = count(FileHelper::findFiles($sourceFilesDir, ['recursive' => true]));

$this->assertSame($sourceFilesCount, $filesCount);
}
}
Loading
Loading