Skip to content

Commit 94a1d98

Browse files
OskarStarknicolas-grekas
authored andcommitted
Use createMock() and use import instead of FQCN
1 parent e0f6764 commit 94a1d98

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

Tests/PropertyAccessorArrayAccessTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1516
use Symfony\Component\PropertyAccess\PropertyAccess;
1617
use Symfony\Component\PropertyAccess\PropertyAccessor;
1718

@@ -47,7 +48,7 @@ public function testGetValue($collection, $path, $value)
4748

4849
public function testGetValueFailsIfNoSuchIndex()
4950
{
50-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchIndexException::class);
51+
$this->expectException(NoSuchIndexException::class);
5152
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
5253
->enableExceptionOnInvalidIndex()
5354
->getPropertyAccessor();

Tests/PropertyAccessorCollectionTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

14+
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
15+
1416
class PropertyAccessorCollectionTest_Car
1517
{
1618
private $axes;
@@ -121,8 +123,8 @@ public function testSetValueCallsAdderAndRemoverForCollections()
121123

122124
public function testSetValueCallsAdderAndRemoverForNestedCollections()
123125
{
124-
$car = $this->getMockBuilder(__CLASS__.'_CompositeCar')->getMock();
125-
$structure = $this->getMockBuilder(__CLASS__.'_CarStructure')->getMock();
126+
$car = $this->createMock(__CLASS__.'_CompositeCar');
127+
$structure = $this->createMock(__CLASS__.'_CarStructure');
126128
$axesBefore = $this->getContainer([1 => 'second', 3 => 'fourth']);
127129
$axesAfter = $this->getContainer([0 => 'first', 1 => 'second', 2 => 'third']);
128130

@@ -148,9 +150,9 @@ public function testSetValueCallsAdderAndRemoverForNestedCollections()
148150

149151
public function testSetValueFailsIfNoAdderNorRemoverFound()
150152
{
151-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
153+
$this->expectException(NoSuchPropertyException::class);
152154
$this->expectExceptionMessageMatches('/Could not determine access type for property "axes" in class "Mock_PropertyAccessorCollectionTest_CarNoAdderAndRemover_[^"]*"./');
153-
$car = $this->getMockBuilder(__CLASS__.'_CarNoAdderAndRemover')->getMock();
155+
$car = $this->createMock(__CLASS__.'_CarNoAdderAndRemover');
154156
$axesBefore = $this->getContainer([1 => 'second', 3 => 'fourth']);
155157
$axesAfter = $this->getContainer([0 => 'first', 1 => 'second', 2 => 'third']);
156158

@@ -187,7 +189,7 @@ public function testIsWritableReturnsFalseIfNoAdderNorRemoverExists()
187189

188190
public function testSetValueFailsIfAdderAndRemoverExistButValueIsNotTraversable()
189191
{
190-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
192+
$this->expectException(NoSuchPropertyException::class);
191193
$this->expectExceptionMessageMatches('/Could not determine access type for property "axes" in class "Symfony\\\\Component\\\\PropertyAccess\\\\Tests\\\\PropertyAccessorCollectionTest_Car[^"]*": The property "axes" in class "Symfony\\\\Component\\\\PropertyAccess\\\\Tests\\\\PropertyAccessorCollectionTest_Car[^"]*" can be defined with the methods "addAxis\(\)", "removeAxis\(\)" but the new value must be an array or an instance of \\\\Traversable, "string" given./');
192194
$car = new PropertyAccessorCollectionTest_Car();
193195

Tests/PropertyAccessorTest.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16+
use Symfony\Component\PropertyAccess\Exception\AccessException;
17+
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
1618
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
19+
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
20+
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
1721
use Symfony\Component\PropertyAccess\PropertyAccess;
1822
use Symfony\Component\PropertyAccess\PropertyAccessor;
1923
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
@@ -101,7 +105,7 @@ public function testGetValue($objectOrArray, $path, $value)
101105
*/
102106
public function testGetValueThrowsExceptionIfPropertyNotFound($objectOrArray, $path)
103107
{
104-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
108+
$this->expectException(NoSuchPropertyException::class);
105109
$this->propertyAccessor->getValue($objectOrArray, $path);
106110
}
107111

@@ -138,7 +142,7 @@ public function testGetValueThrowsExceptionIfIndexNotFoundAndIndexExceptionsEnab
138142
*/
139143
public function testGetValueThrowsExceptionIfUninitializedProperty()
140144
{
141-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\AccessException::class);
145+
$this->expectException(AccessException::class);
142146
$this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedProperty::$uninitialized" is not readable because it is typed "string". You should initialize it or declare a default value instead.');
143147

144148
$this->propertyAccessor->getValue(new UninitializedProperty(), 'uninitialized');
@@ -149,7 +153,7 @@ public function testGetValueThrowsExceptionIfUninitializedProperty()
149153
*/
150154
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetter()
151155
{
152-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\AccessException::class);
156+
$this->expectException(AccessException::class);
153157
$this->expectExceptionMessage('The method "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
154158

155159
$this->propertyAccessor->getValue(new UninitializedPrivateProperty(), 'uninitialized');
@@ -160,7 +164,7 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetter()
160164
*/
161165
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousClass()
162166
{
163-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\AccessException::class);
167+
$this->expectException(AccessException::class);
164168
$this->expectExceptionMessage('The method "class@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
165169

166170
$object = eval('return new class() {
@@ -180,7 +184,7 @@ public function getUninitialized(): array
180184
*/
181185
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousStdClass()
182186
{
183-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\AccessException::class);
187+
$this->expectException(AccessException::class);
184188
$this->expectExceptionMessage('The method "stdClass@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
185189

186190
$object = eval('return new class() extends \stdClass {
@@ -200,7 +204,7 @@ public function getUninitialized(): array
200204
*/
201205
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousChildClass()
202206
{
203-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\AccessException::class);
207+
$this->expectException(AccessException::class);
204208
$this->expectExceptionMessage('The method "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
205209

206210
$object = eval('return new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty {};');
@@ -259,7 +263,7 @@ public function testGetValueNotModifyObjectException()
259263

260264
public function testGetValueDoesNotReadMagicCallByDefault()
261265
{
262-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
266+
$this->expectException(NoSuchPropertyException::class);
263267
$this->propertyAccessor->getValue(new TestClassMagicCall('Bernhard'), 'magicCallProperty');
264268
}
265269

@@ -283,7 +287,7 @@ public function testGetValueReadsMagicCallThatReturnsConstant()
283287
*/
284288
public function testGetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $path)
285289
{
286-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException::class);
290+
$this->expectException(UnexpectedTypeException::class);
287291
$this->expectExceptionMessage('PropertyAccessor requires a graph of objects or arrays to operate on');
288292
$this->propertyAccessor->getValue($objectOrArray, $path);
289293
}
@@ -303,7 +307,7 @@ public function testSetValue($objectOrArray, $path)
303307
*/
304308
public function testSetValueThrowsExceptionIfPropertyNotFound($objectOrArray, $path)
305309
{
306-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
310+
$this->expectException(NoSuchPropertyException::class);
307311
$this->propertyAccessor->setValue($objectOrArray, $path, 'Updated');
308312
}
309313

@@ -347,15 +351,15 @@ public function testSetValueUpdatesMagicSet()
347351

348352
public function testSetValueThrowsExceptionIfThereAreMissingParameters()
349353
{
350-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
354+
$this->expectException(NoSuchPropertyException::class);
351355
$object = new TestClass('Bernhard');
352356

353357
$this->propertyAccessor->setValue($object, 'publicAccessorWithMoreRequiredParameters', 'Updated');
354358
}
355359

356360
public function testSetValueDoesNotUpdateMagicCallByDefault()
357361
{
358-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
362+
$this->expectException(NoSuchPropertyException::class);
359363
$author = new TestClassMagicCall('Bernhard');
360364

361365
$this->propertyAccessor->setValue($author, 'magicCallProperty', 'Updated');
@@ -377,7 +381,7 @@ public function testSetValueUpdatesMagicCallIfEnabled()
377381
*/
378382
public function testSetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $path)
379383
{
380-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException::class);
384+
$this->expectException(UnexpectedTypeException::class);
381385
$this->expectExceptionMessage('PropertyAccessor requires a graph of objects or arrays to operate on');
382386
$this->propertyAccessor->setValue($objectOrArray, $path, 'value');
383387
}
@@ -613,7 +617,7 @@ public function testIsWritableForReferenceChainIssue($object, $path, $value)
613617

614618
public function testThrowTypeError()
615619
{
616-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException::class);
620+
$this->expectException(InvalidArgumentException::class);
617621
$this->expectExceptionMessage('Expected argument of type "DateTime", "string" given at property path "date"');
618622
$object = new TypeHinted();
619623

@@ -622,7 +626,7 @@ public function testThrowTypeError()
622626

623627
public function testThrowTypeErrorWithNullArgument()
624628
{
625-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException::class);
629+
$this->expectException(InvalidArgumentException::class);
626630
$this->expectExceptionMessage('Expected argument of type "DateTime", "null" given');
627631
$object = new TypeHinted();
628632

@@ -674,7 +678,7 @@ public function testAttributeWithSpecialChars()
674678

675679
public function testThrowTypeErrorWithInterface()
676680
{
677-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException::class);
681+
$this->expectException(InvalidArgumentException::class);
678682
$this->expectExceptionMessage('Expected argument of type "Countable", "string" given');
679683
$object = new TypeHinted();
680684

@@ -694,7 +698,7 @@ public function testAnonymousClassRead()
694698

695699
public function testAnonymousClassReadThrowExceptionOnInvalidPropertyPath()
696700
{
697-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
701+
$this->expectException(NoSuchPropertyException::class);
698702
$obj = $this->generateAnonymousClass('bar');
699703

700704
$this->propertyAccessor->getValue($obj, 'invalid_property');
@@ -820,39 +824,39 @@ public function testAdderAndRemoverArePreferredOverSetterForSameSingularAndPlura
820824

821825
public function testAdderWithoutRemover()
822826
{
823-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
827+
$this->expectException(NoSuchPropertyException::class);
824828
$this->expectExceptionMessageMatches('/.*The add method "addFoo" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestAdderRemoverInvalidMethods" was found, but the corresponding remove method "removeFoo" was not found\./');
825829
$object = new TestAdderRemoverInvalidMethods();
826830
$this->propertyAccessor->setValue($object, 'foos', [1, 2]);
827831
}
828832

829833
public function testRemoverWithoutAdder()
830834
{
831-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
835+
$this->expectException(NoSuchPropertyException::class);
832836
$this->expectExceptionMessageMatches('/.*The remove method "removeBar" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestAdderRemoverInvalidMethods" was found, but the corresponding add method "addBar" was not found\./');
833837
$object = new TestAdderRemoverInvalidMethods();
834838
$this->propertyAccessor->setValue($object, 'bars', [1, 2]);
835839
}
836840

837841
public function testAdderAndRemoveNeedsTheExactParametersDefined()
838842
{
839-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
843+
$this->expectException(NoSuchPropertyException::class);
840844
$this->expectExceptionMessageMatches('/.*The method "addFoo" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestAdderRemoverInvalidArgumentLength" requires 0 arguments, but should accept only 1\. The method "removeFoo" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestAdderRemoverInvalidArgumentLength" requires 2 arguments, but should accept only 1\./');
841845
$object = new TestAdderRemoverInvalidArgumentLength();
842846
$this->propertyAccessor->setValue($object, 'foo', [1, 2]);
843847
}
844848

845849
public function testSetterNeedsTheExactParametersDefined()
846850
{
847-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
851+
$this->expectException(NoSuchPropertyException::class);
848852
$this->expectExceptionMessageMatches('/.*The method "setBar" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestAdderRemoverInvalidArgumentLength" requires 2 arguments, but should accept only 1\./');
849853
$object = new TestAdderRemoverInvalidArgumentLength();
850854
$this->propertyAccessor->setValue($object, 'bar', [1, 2]);
851855
}
852856

853857
public function testSetterNeedsPublicAccess()
854858
{
855-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException::class);
859+
$this->expectException(NoSuchPropertyException::class);
856860
$this->expectExceptionMessageMatches('/.*The method "setFoo" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestClassSetValue" was found but does not have public access./');
857861
$object = new TestClassSetValue(0);
858862
$this->propertyAccessor->setValue($object, 'foo', 1);

Tests/PropertyPathTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
16+
use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
1517
use Symfony\Component\PropertyAccess\PropertyPath;
1618

1719
class PropertyPathTest extends TestCase
@@ -25,13 +27,13 @@ public function testToString()
2527

2628
public function testDotIsRequiredBeforeProperty()
2729
{
28-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException::class);
30+
$this->expectException(InvalidPropertyPathException::class);
2931
new PropertyPath('[index]property');
3032
}
3133

3234
public function testDotCannotBePresentAtTheBeginning()
3335
{
34-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException::class);
36+
$this->expectException(InvalidPropertyPathException::class);
3537
new PropertyPath('.property');
3638
}
3739

@@ -53,25 +55,25 @@ public function providePathsContainingUnexpectedCharacters()
5355
*/
5456
public function testUnexpectedCharacters($path)
5557
{
56-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException::class);
58+
$this->expectException(InvalidPropertyPathException::class);
5759
new PropertyPath($path);
5860
}
5961

6062
public function testPathCannotBeEmpty()
6163
{
62-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException::class);
64+
$this->expectException(InvalidPropertyPathException::class);
6365
new PropertyPath('');
6466
}
6567

6668
public function testPathCannotBeNull()
6769
{
68-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException::class);
70+
$this->expectException(InvalidArgumentException::class);
6971
new PropertyPath(null);
7072
}
7173

7274
public function testPathCannotBeFalse()
7375
{
74-
$this->expectException(\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException::class);
76+
$this->expectException(InvalidArgumentException::class);
7577
new PropertyPath(false);
7678
}
7779

0 commit comments

Comments
 (0)