This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
224 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace PiedWeb\CMSBundle\Entity\SharedTrait; | ||
|
||
use Exception; | ||
|
||
class CustomPropertiesException extends Exception | ||
{ | ||
public function __construct(string $name) | ||
{ | ||
$message = 'use `setStandAloneCustomProperties` for an index custom property (`'.$name.'`)'; | ||
|
||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace PiedWeb\CMSBundle\Tests\Entity\SharedTrait; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PiedWeb\CMSBundle\Entity\SharedTrait\CustomPropertiesTrait; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class CustomPropertiesTraitTest extends TestCase | ||
{ | ||
protected static function customPorperties($firstValue = 'test', $secondValue = 'test 2') | ||
{ | ||
return Yaml::dump([ | ||
'newCustomPropertyNotIndexed' => $firstValue, | ||
'customProperties' => $secondValue, | ||
]); | ||
} | ||
|
||
protected static function standStandAloneCustomProperties($firstValue = 'test') | ||
{ | ||
return Yaml::dump(['newCustomPropertyNotIndexed' => $firstValue]); | ||
} | ||
|
||
public function testStandAloneCustomProperties() | ||
{ | ||
$customProperties = $this->getCustomPropertiesTrait(); | ||
|
||
$this->assertNull($customProperties->getCustomProperties()); | ||
|
||
$customProperties->setCustomProperties(static::customPorperties()); | ||
|
||
$this->assertNull($customProperties->validateCustomProperties($this->getExceptionContextInterface())); | ||
$this->assertSame($customProperties->getCustomProperties(), static::customPorperties()); | ||
$this->assertSame($customProperties->getStandAloneCustomProperties(), static::standStandAloneCustomProperties()); | ||
|
||
$customProperties->setStandAloneCustomProperties(static::standStandAloneCustomProperties('test 1234'), true); | ||
$this->assertSame(static::customPorperties('test 1234'), $customProperties->getCustomProperties()); | ||
} | ||
|
||
/** | ||
* @return CustomPropertiesTrait | ||
*/ | ||
protected function getCustomPropertiesTrait() | ||
{ | ||
$mock = $this->getMockForTrait(CustomPropertiesTrait::class); | ||
//$mock->method('getTitle')->willReturn(true); | ||
|
||
return $mock; | ||
} | ||
|
||
/** | ||
* @return ExecutionContextInterface | ||
*/ | ||
protected function getExceptionContextInterface() | ||
{ | ||
$mockConstraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class); | ||
$mockConstraintViolationBuilder->method('atPath')->willReturnSelf(); | ||
$mockConstraintViolationBuilder->method('addViolation')->willReturnSelf(); | ||
|
||
$mock = $this->createMock(ExecutionContextInterface::class); | ||
$mock->method('buildViolation')->willReturnCallback(function ($arg) use ($mockConstraintViolationBuilder) { | ||
if (\in_array($arg, ['page.customProperties.malformed', 'page.customProperties.notStandAlone'])) { | ||
new \Error(); | ||
} else { | ||
return $mockConstraintViolationBuilder; | ||
} | ||
}); | ||
|
||
return $mock; | ||
} | ||
} |