From f2f8f0b3093829dae47c4d9bb6a6dfc9f53248ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Mon, 10 Nov 2025 20:54:23 +0400 Subject: [PATCH 1/5] Using snapshots for testing --- composer.json | 3 +- tests/commands/ApiControllerTest.php | 130 +--- ...trollerTest__testGenerateBootstrap__1.html | 568 ++++++++++++++++++ ...trollerTest__testGenerateBootstrap__2.html | 500 +++++++++++++++ ...trollerTest__testGenerateBootstrap__3.html | 500 +++++++++++++++ 5 files changed, 1593 insertions(+), 108 deletions(-) create mode 100644 tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html create mode 100644 tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__2.html create mode 100644 tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__3.html diff --git a/composer.json b/composer.json index 0f8a257..bceaa6b 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "scrivo/highlight.php": "^9.0" }, "require-dev": { - "phpunit/phpunit": "*" + "phpunit/phpunit": "*", + "spatie/phpunit-snapshot-assertions": "^3.0" }, "repositories": [ { diff --git a/tests/commands/ApiControllerTest.php b/tests/commands/ApiControllerTest.php index 57dc314..dd9d7a1 100644 --- a/tests/commands/ApiControllerTest.php +++ b/tests/commands/ApiControllerTest.php @@ -1,4 +1,5 @@ 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('

Abstract Class yiiunit\apidoc\data\api\animal\Animal

', $animalContent); - $this->assertStringContainsString('Available since version1.0', $animalContent); - $this->assertStringContainsString('Animal is a base class for animals.', $animalContent); - $this->assertContainsWithoutIndent( - << - public integer getSomething ( \$test ) - - - - \$test - - - - - return - integer - - -HTML - , $animalContent - ); - $this->assertContainsWithoutIndent( - << - \$name - string - Animal name. - yiiunit\apidoc\data\api\animal\Animal - -HTML - , $animalContent - ); - $this->assertContainsWithoutIndent( - << - \$birthDate - integer - Animal birth date as a UNIX timestamp. - yiiunit\apidoc\data\api\animal\Animal - -HTML - , $animalContent - ); - $this->assertContainsWithoutIndent( - << -getAge() -Returns animal age in seconds. -yiiunit\apidoc\data\api\animal\Animal - -HTML - , $animalContent - ); - $this->assertContainsWithoutIndent( - << - render() - Renders animal description. - yiiunit\apidoc\data\api\animal\Animal - -HTML - , $animalContent - ); - - $this->assertContainsWithoutIndent( - << - isOlder() - Checks whether the animal is older than the specified time. - yiiunit\apidoc\data\api\animal\Animal - -HTML - , $animalContent - ); - - $this->assertContainsWithoutIndent( - <<public boolean isOlder ( \$date ) - - \$date - integer - -

Date as a UNIX timestamp.

- - -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('Available since version1.1', $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>/is', '', $fileContent); + $fileContent = preg_replace('/<\/script>/is', '', $fileContent); + $fileContent = preg_replace('//is', '', $fileContent); + + $this->assertMatchesHtmlSnapshot($fileContent); + $filesCount++; + } + + $sourceFilesCount = count(FileHelper::findFiles($sourceFilesDir, ['recursive' => true])); + + $this->assertSame($sourceFilesCount, $filesCount); } } diff --git a/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html new file mode 100644 index 0000000..311eb4a --- /dev/null +++ b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html @@ -0,0 +1,568 @@ + + + + + + + + + + + + + Animal, yiiunit\apidoc\data\api\animal\Animal - Yii Framework 2.0 API Documentation + + + +
+ + + + +
+ +
+

Abstract Class yiiunit\apidoc\data\api\animal\Animal

+ + + + + + + + + + + + + + + + + + + +
Inheritance +yiiunit\apidoc\data\api\animal\Animal » +yii\base\BaseObject
Subclasses +yiiunit\apidoc\data\api\animal\Cat, yiiunit\apidoc\data\api\animal\Dog +
Available since version1.0
+ +
+

Animal is a base class for animals.

+ +
+ + + + + +
+

Public Properties

+ +

Hide inherited properties

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescriptionDefined By
$ageintegeranimal age in seconds.yiiunit\apidoc\data\api\animal\Animal
$birthDateintegerAnimal birth date as a UNIX timestamp.yiiunit\apidoc\data\api\animal\Animal
$namestringAnimal name.yiiunit\apidoc\data\api\animal\Animal
+
+ + + +
+

Public Methods

+ +

Hide inherited methods

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MethodDescriptionDefined By
getAge()Returns animal age in seconds.yiiunit\apidoc\data\api\animal\Animal
getSomething()yiiunit\apidoc\data\api\animal\Animal
isOlder()Checks whether the animal is older than the specified time.yiiunit\apidoc\data\api\animal\Animal
render()Renders animal description.yiiunit\apidoc\data\api\animal\Animal
+
+ + + + + + +

Property Details

+ +
+

Hide inherited properties

+ +
+
+ + + + $age + + public property + +
+ + +
+ +

animal age in seconds.

+
+ +
+public integer $age null +
+ + +
+
+
+ + + + $birthDate + + public property + +
+ + +
+ +

Animal birth date as a UNIX timestamp.

+
+ +
+public integer $birthDate null +
+ + +
+
+
+ + + + $name + + public property + +
+ + +
+ +

Animal name.

+
+ +
+public string $name null +
+ + +
+
+ +

Method Details

+ +
+

Hide inherited methods

+ +
+
+ + + + getAge() + + + public method + +
+ + +
+ +

Returns animal age in seconds.

+
+ + + + + + + + + + + +
+public integer getAge ( )
returninteger +

Animal age in seconds.

+
+ + + + +

+ +

+
+
+
+                public function getAge()
+{
+    return time() - $this->birthDate;
+}
+
+            
+
+
+ +
+
+
+ + + + getSomething() + + + public method + +
+ + +
+ +

+
+ + + + + + + + + + + + + + + + +
+public integer getSomething ( $test )
$test +
returninteger +
+ + + + +
+
+
+ + + + isOlder() + + + public method + +
+ + +
+ +

Checks whether the animal is older than the specified time.

+
+ + + + + + + + + + + +
+public boolean isOlder ( $date )
$dateinteger +

Date as a UNIX timestamp.

+
+ + + + +

+ +

+
+
+
+                public function isOlder($date)
+{
+    return $this->getAge() > $date;
+}
+
+            
+
+
+ +
+
+
+ + + + render() + + + public abstract method + +
+ + +
+ +

Renders animal description.

+
+ + + + + + + + + + + +
+public abstract string render ( )
returnstring +

HTML output.

+
+ + + + +

+ +

+
+
+
+                abstract public function render();
+
+            
+
+
+ +
+
+
+
+ + + + +
+ + + + + + diff --git a/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__2.html b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__2.html new file mode 100644 index 0000000..3752fce --- /dev/null +++ b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__2.html @@ -0,0 +1,500 @@ + + + + + + + + + + + + + Cat, yiiunit\apidoc\data\api\animal\Cat - Yii Framework 2.0 API Documentation + + + +
+ + + + +
+ +
+

Class yiiunit\apidoc\data\api\animal\Cat

+ + + + + + + + + + + + + + + +
Inheritance +yiiunit\apidoc\data\api\animal\Cat » +yiiunit\apidoc\data\api\animal\Animal » +yii\base\BaseObject
Available since version1.1
+ +
+

Cat represents a cat animal.

+ +
+ + + + + +
+

Public Properties

+ +

Hide inherited properties

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescriptionDefined By
$ageintegeranimal age in seconds.yiiunit\apidoc\data\api\animal\Animal
$birthDateintegerAnimal birth date as a UNIX timestamp.yiiunit\apidoc\data\api\animal\Animal
$namestringAnimal name.yiiunit\apidoc\data\api\animal\Animal
+
+ + + +
+

Public Methods

+ +

Hide inherited methods

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MethodDescriptionDefined By
getAge()Returns animal age in seconds.yiiunit\apidoc\data\api\animal\Animal
getSomething()yiiunit\apidoc\data\api\animal\Animal
isOlder()Checks whether the animal is older than the specified time.yiiunit\apidoc\data\api\animal\Animal
render()Renders animal description.yiiunit\apidoc\data\api\animal\Cat
+
+ + + + + + +

Method Details

+ +
+

Hide inherited methods

+ +
+
+ + + + getAge() + + + public method + +
+ + +
+

+ Defined in: + yiiunit\apidoc\data\api\animal\Animal::getAge()

+ +

Returns animal age in seconds.

+
+ + + + + + + + + + + +
+public integer getAge ( )
returninteger +

Animal age in seconds.

+
+ + + + +

+ +

+
+
+
+                public function getAge()
+{
+    return time() - $this->birthDate;
+}
+
+            
+
+
+ +
+
+
+ + + + getSomething() + + + public method + +
+ + + + + + + + + + + + + + + + + + + +
+public integer getSomething ( $test )
$test +
returninteger +
+ + + + +
+
+
+ + + + isOlder() + + + public method + +
+ + +
+

+ Defined in: + yiiunit\apidoc\data\api\animal\Animal::isOlder()

+ +

Checks whether the animal is older than the specified time.

+
+ + + + + + + + + + + +
+public boolean isOlder ( $date )
$dateinteger +

Date as a UNIX timestamp.

+
+ + + + +

+ +

+
+
+
+                public function isOlder($date)
+{
+    return $this->getAge() > $date;
+}
+
+            
+
+
+ +
+
+
+ + + + render() + + + public method + +
+ + +
+ +

Renders animal description.

+
+ + + + + + + + + + + +
+public string render ( )
returnstring +

HTML output.

+
+ + + + +

+ +

+
+
+
+                public function render()
+{
+    // this method has `inheritdoc` tag in brackets
+    return 'This is a cat';
+}
+
+            
+
+
+ +
+
+
+
+ + + + +
+ + + + + + diff --git a/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__3.html b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__3.html new file mode 100644 index 0000000..4c93569 --- /dev/null +++ b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__3.html @@ -0,0 +1,500 @@ + + + + + + + + + + + + + Dog, yiiunit\apidoc\data\api\animal\Dog - Yii Framework 2.0 API Documentation + + + +
+ + + + +
+ +
+

Class yiiunit\apidoc\data\api\animal\Dog

+ + + + + + + + + + + + + + + +
Inheritance +yiiunit\apidoc\data\api\animal\Dog » +yiiunit\apidoc\data\api\animal\Animal » +yii\base\BaseObject
Available since version1.1
+ +
+

Dog represents a dog animal.

+ +
+ + + + + +
+

Public Properties

+ +

Hide inherited properties

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescriptionDefined By
$ageintegeranimal age in seconds.yiiunit\apidoc\data\api\animal\Animal
$birthDateintegerAnimal birth date as a UNIX timestamp.yiiunit\apidoc\data\api\animal\Animal
$namestringAnimal name.yiiunit\apidoc\data\api\animal\Animal
+
+ + + +
+

Public Methods

+ +

Hide inherited methods

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MethodDescriptionDefined By
getAge()Returns animal age in seconds.yiiunit\apidoc\data\api\animal\Animal
getSomething()yiiunit\apidoc\data\api\animal\Animal
isOlder()Checks whether the animal is older than the specified time.yiiunit\apidoc\data\api\animal\Animal
render()Renders animal description.yiiunit\apidoc\data\api\animal\Dog
+
+ + + + + + +

Method Details

+ +
+

Hide inherited methods

+ +
+
+ + + + getAge() + + + public method + +
+ + +
+

+ Defined in: + yiiunit\apidoc\data\api\animal\Animal::getAge()

+ +

Returns animal age in seconds.

+
+ + + + + + + + + + + +
+public integer getAge ( )
returninteger +

Animal age in seconds.

+
+ + + + +

+ +

+
+
+
+                public function getAge()
+{
+    return time() - $this->birthDate;
+}
+
+            
+
+
+ +
+
+
+ + + + getSomething() + + + public method + +
+ + + + + + + + + + + + + + + + + + + +
+public integer getSomething ( $test )
$test +
returninteger +
+ + + + +
+
+
+ + + + isOlder() + + + public method + +
+ + +
+

+ Defined in: + yiiunit\apidoc\data\api\animal\Animal::isOlder()

+ +

Checks whether the animal is older than the specified time.

+
+ + + + + + + + + + + +
+public boolean isOlder ( $date )
$dateinteger +

Date as a UNIX timestamp.

+
+ + + + +

+ +

+
+
+
+                public function isOlder($date)
+{
+    return $this->getAge() > $date;
+}
+
+            
+
+
+ +
+
+
+ + + + render() + + + public method + +
+ + +
+ +

Renders animal description.

+
+ + + + + + + + + + + +
+public string render ( )
returnstring +

HTML output.

+
+ + + + +

+ +

+
+
+
+                public function render()
+{
+    // this method has `inheritdoc` tag without brackets
+    return 'This is a dog';
+}
+
+            
+
+
+ +
+
+
+
+ + + + +
+ + + + + + From 39bbb5f286d599b34e067358b042b5737e5a096a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Mon, 10 Nov 2025 21:20:31 +0400 Subject: [PATCH 2/5] remove ids --- tests/commands/ApiControllerTest.php | 1 + ...trollerTest__testGenerateBootstrap__1.html | 58 +++++++++---------- ...trollerTest__testGenerateBootstrap__2.html | 52 ++++++++--------- ...trollerTest__testGenerateBootstrap__3.html | 52 ++++++++--------- 4 files changed, 82 insertions(+), 81 deletions(-) diff --git a/tests/commands/ApiControllerTest.php b/tests/commands/ApiControllerTest.php index dd9d7a1..9c2dc0b 100644 --- a/tests/commands/ApiControllerTest.php +++ b/tests/commands/ApiControllerTest.php @@ -82,6 +82,7 @@ public function testGenerateBootstrap() $fileContent = preg_replace('/.*?<\/p>/is', '', $fileContent); $fileContent = preg_replace('/<\/script>/is', '', $fileContent); $fileContent = preg_replace('//is', '', $fileContent); + $fileContent = preg_replace('/\s+id\s*=\s*(["\'])[^"\']*\1/i', '', $fileContent); $this->assertMatchesHtmlSnapshot($fileContent); $filesCount++; diff --git a/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html index 311eb4a..e46a1e6 100644 --- a/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html +++ b/tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__1.html @@ -15,30 +15,30 @@
-