Skip to content

Commit 25cf9ae

Browse files
committed
AC-15399::[CE] PHPUnit 12: Upgrade Wishlist & Rating related test | Move Inline Anonymous Classes
1 parent fbed91c commit 25cf9ae

File tree

10 files changed

+310
-72
lines changed

10 files changed

+310
-72
lines changed

app/code/Magento/CatalogGraphQl/Test/Unit/Model/Resolver/Product/MediaGalleryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\CatalogGraphQl\Model\Resolver\Product\MediaGallery;
1414
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use PHPUnit\Framework\Attributes\DataProvider;
1617
use PHPUnit\Framework\MockObject\MockObject;
1718
use PHPUnit\Framework\TestCase;
1819
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -47,7 +48,7 @@ class MediaGalleryTest extends TestCase
4748
protected function setUp(): void
4849
{
4950
$this->fieldMock = $this->createMock(Field::class);
50-
$this->contextMock = $this->getMockForAbstractClass(ContextInterface::class);
51+
$this->contextMock = $this->createMock(ContextInterface::class);
5152
$this->infoMock = $this->createMock(ResolveInfo::class);
5253
$this->productMock = $this->getMockBuilder(Product::class)
5354
->disableOriginalConstructor()
@@ -56,12 +57,12 @@ protected function setUp(): void
5657
}
5758

5859
/**
59-
* @dataProvider dataProviderForResolve
6060
* @param $expected
6161
* @param $productName
6262
* @return void
6363
* @throws Exception
6464
*/
65+
#[DataProvider('dataProviderForResolve')]
6566
public function testResolve($expected, $productName): void
6667
{
6768
$existingEntryMock = $this->getMockBuilder(Entry::class)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaStorage\Test\Unit\Helper;
9+
10+
use Magento\MediaStorage\Model\File\Storage\Database;
11+
12+
/**
13+
* Test helper for Database class
14+
*/
15+
class DatabaseTestHelper extends Database
16+
{
17+
/**
18+
* @var string
19+
*/
20+
private $content = 'content';
21+
22+
/**
23+
* @var bool
24+
*/
25+
private $id = true;
26+
27+
/**
28+
* Constructor - skip parent constructor to avoid dependencies
29+
*/
30+
public function __construct()
31+
{
32+
// Skip parent constructor to avoid dependency injection issues
33+
}
34+
35+
/**
36+
* Get content
37+
*
38+
* @return string
39+
*/
40+
public function getContent()
41+
{
42+
return $this->content;
43+
}
44+
45+
/**
46+
* Set content
47+
*
48+
* @param string $content
49+
* @return $this
50+
*/
51+
public function setContent($content)
52+
{
53+
$this->content = $content;
54+
return $this;
55+
}
56+
57+
/**
58+
* Get ID (alias for compatibility)
59+
*
60+
* @return bool
61+
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
62+
*/
63+
public function getId()
64+
{
65+
return $this->id;
66+
}
67+
68+
/**
69+
* Set ID
70+
*
71+
* @param bool $id
72+
* @return $this
73+
*/
74+
public function setId($id)
75+
{
76+
$this->id = $id;
77+
return $this;
78+
}
79+
80+
/**
81+
* Load by filename
82+
*
83+
* @param string $filename
84+
* @return $this
85+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
86+
*/
87+
public function loadByFilename($filename)
88+
{
89+
return $this;
90+
}
91+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaStorage\Test\Unit\Helper;
9+
10+
use Magento\MediaStorage\Model\File\Storage\Directory\Database;
11+
12+
/**
13+
* Test helper for Directory\Database class
14+
*/
15+
class DirectoryDatabaseTestHelper extends Database
16+
{
17+
/**
18+
* @var string
19+
*/
20+
private $path = '';
21+
22+
/**
23+
* @var string
24+
*/
25+
private $name = '';
26+
27+
/**
28+
* Constructor - skip parent constructor to avoid dependencies
29+
*/
30+
public function __construct()
31+
{
32+
// Skip parent constructor to avoid dependency injection issues
33+
}
34+
35+
/**
36+
* Set path
37+
*
38+
* @param string $path
39+
* @return $this
40+
*/
41+
public function setPath($path)
42+
{
43+
$this->path = $path;
44+
return $this;
45+
}
46+
47+
/**
48+
* Get path
49+
*
50+
* @return string
51+
*/
52+
public function getPath()
53+
{
54+
return $this->path;
55+
}
56+
57+
/**
58+
* Set name
59+
*
60+
* @param string $name
61+
* @return $this
62+
*/
63+
public function setName($name)
64+
{
65+
$this->name = $name;
66+
return $this;
67+
}
68+
69+
/**
70+
* Get name
71+
*
72+
* @return string
73+
*/
74+
public function getName()
75+
{
76+
return $this->name;
77+
}
78+
79+
/**
80+
* Save
81+
*
82+
* @return $this
83+
*/
84+
public function save()
85+
{
86+
return $this;
87+
}
88+
89+
/**
90+
* Get parent ID
91+
*
92+
* @return int
93+
*/
94+
public function getParentId()
95+
{
96+
return 1;
97+
}
98+
}

app/code/Magento/MediaStorage/Test/Unit/Helper/File/Storage/DatabaseTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\MediaStorage\Helper\File\Storage\Database;
1919
use Magento\MediaStorage\Model\File\Storage;
2020
use Magento\MediaStorage\Model\File\Storage\DatabaseFactory;
21+
use Magento\Framework\Model\Test\Unit\Helper\AbstractDbTestHelper;
2122
use Magento\MediaStorage\Model\File\Storage\File;
2223
use PHPUnit\Framework\Attributes\DataProvider;
2324
use PHPUnit\Framework\MockObject\MockObject;
@@ -359,18 +360,7 @@ public function testDeleteFolder($storage, $callNum)
359360
$this->dbStorageFactoryMock->expects($this->exactly($callNum))
360361
->method('create')
361362
->willReturn($dbModelMock);
362-
$resourceModelMock = new class extends AbstractDb {
363-
public function __construct()
364-
{
365-
}
366-
protected function _construct()
367-
{
368-
}
369-
public function deleteFolder($path)
370-
{
371-
return true;
372-
}
373-
};
363+
$resourceModelMock = new AbstractDbTestHelper();
374364
$dbModelMock->expects($this->exactly($callNum))
375365
->method('getResource')
376366
->willReturn($resourceModelMock);

app/code/Magento/MediaStorage/Test/Unit/Helper/File/StorageTest.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MediaStorage\Helper\File\Storage;
1515
use Magento\MediaStorage\Helper\File\Storage\Database as DatabaseHelper;
1616
use Magento\MediaStorage\Model\File\Storage\File;
17+
use Magento\Framework\Model\Test\Unit\Helper\AbstractModelTestHelper;
1718
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\MockObject\MockObject;
1920
use PHPUnit\Framework\TestCase;
@@ -131,24 +132,7 @@ public function testProcessStorageFile($expected, $storage, $callNum, $callSaveF
131132
->with($filename)
132133
->willReturn($relativePath);
133134

134-
$storageModelMock = new class extends AbstractModel {
135-
/**
136-
* @var \Magento\MediaStorage\Model\File\Storage\Database
137-
*/
138-
private $fileMock;
139-
140-
public function __construct()
141-
{
142-
}
143-
public function loadByFileName($filename)
144-
{
145-
return $this->fileMock;
146-
}
147-
public function setFileMock($fileMock)
148-
{
149-
$this->fileMock = $fileMock;
150-
}
151-
};
135+
$storageModelMock = new AbstractModelTestHelper();
152136
$this->storageMock->expects($this->exactly($callNum))
153137
->method('getStorageModel')
154138
->willReturn($storageModelMock);

app/code/Magento/MediaStorage/Test/Unit/Model/File/Storage/Directory/DatabaseTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MediaStorage\Model\File\Storage;
1616
use Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory;
1717
use Magento\MediaStorage\Model\ResourceModel\File\Storage\Directory\Database;
18+
use Magento\MediaStorage\Test\Unit\Helper\DirectoryDatabaseTestHelper;
1819
use PHPUnit\Framework\MockObject\MockObject;
1920
use PHPUnit\Framework\TestCase;
2021
use Psr\Log\LoggerInterface;
@@ -85,27 +86,7 @@ protected function setUp(): void
8586
$this->registryMock = $this->createMock(Registry::class);
8687
$this->helperStorageDatabase = $this->createMock(DatabaseHelper::class);
8788
$this->dateModelMock = $this->createMock(DateTime::class);
88-
$this->directoryMock = new class extends \Magento\MediaStorage\Model\File\Storage\Directory\Database {
89-
public function __construct()
90-
{
91-
}
92-
public function setPath($path)
93-
{
94-
return $this;
95-
}
96-
public function setName($name)
97-
{
98-
return $this;
99-
}
100-
public function save()
101-
{
102-
return $this;
103-
}
104-
public function getParentId()
105-
{
106-
return 1;
107-
}
108-
};
89+
$this->directoryMock = new DirectoryDatabaseTestHelper();
10990
$this->directoryFactoryMock = $this->createPartialMock(
11091
DatabaseFactory::class, // @phpstan-ignore-line
11192
['create']

app/code/Magento/MediaStorage/Test/Unit/Model/File/Storage/ResponseTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ protected function setUp(): void
5858
public function testSendResponse(): void
5959
{
6060
$filePath = 'file_path';
61-
$headers = $this->getMockBuilder(Headers::class)
62-
->getMock();
61+
$headers = $this->createMock(Headers::class);
6362
$this->response->setFilePath($filePath);
6463
$this->response->setHeaders($headers);
6564
$this->transferAdapter

app/code/Magento/MediaStorage/Test/Unit/Model/File/Storage/SynchronizationTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\MediaStorage\Model\File\Storage\Database;
1414
use Magento\MediaStorage\Model\File\Storage\DatabaseFactory;
1515
use Magento\MediaStorage\Model\File\Storage\Synchronization;
16+
use Magento\MediaStorage\Test\Unit\Helper\DatabaseTestHelper;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class SynchronizationTest extends TestCase
@@ -23,23 +24,7 @@ public function testSynchronize()
2324
$relativeFileName = 'config.xml';
2425

2526
$storageFactoryMock = $this->createPartialMock(DatabaseFactory::class, ['create']); // @phpstan-ignore-line
26-
$storageMock = new class extends Database {
27-
public function __construct()
28-
{
29-
}
30-
public function getContent()
31-
{
32-
return 'content';
33-
}
34-
public function getId()
35-
{
36-
return true;
37-
}
38-
public function loadByFilename($filename)
39-
{
40-
return $this;
41-
}
42-
};
27+
$storageMock = new DatabaseTestHelper();
4328
$storageFactoryMock->expects($this->once())->method('create')->willReturn($storageMock);
4429

4530
$file = $this->createPartialMock(

0 commit comments

Comments
 (0)