Backup of database 9 Nov 17
[yaffs-website] / web / modules / contrib / permissions_by_term / tests / src / Unit / Base.php
1 <?php
2
3 namespace Drupal\Tests\permissions_by_term\Unit;
4
5 use \Symfony\Component\DependencyInjection\ContainerInterface;
6
7 trait Base
8 {
9
10   /**
11    * @param string $namespace
12    * @param array $methodsReturnMap
13    *
14    * @return mixed
15    */
16   public function createMock($namespace, array $methodsReturnMap = [])
17   {
18     $methodNames = array_keys($methodsReturnMap);
19     $mock = $this
20       ->getMockBuilder($namespace);
21
22     $mock->disableOriginalConstructor();
23
24     $mockedClass = $mock
25       ->setMethods($methodNames)
26       ->getMock();
27
28     foreach ($methodsReturnMap as $methodName => $methodReturn) {
29       $mockedClass
30         ->method($methodName)
31         ->will(
32           $this->returnValue($methodReturn)
33         );
34     }
35
36     return $mockedClass;
37   }
38
39   /**
40    * @param string $class
41    * @param string $propertyName
42    * @param mixed $value
43    */
44   public function modifyPropertyByReflection($class, $propertyName, $value)
45   {
46     $reflection = $this->makePropertyAccessible($class, $propertyName);
47     $reflection->setValue($class, $value);
48   }
49
50   /**
51    * @param string $class
52    * @param string $propertyName
53    * @return \ReflectionProperty
54    */
55   public function makePropertyAccessible($class, $propertyName)
56   {
57     $reflection = new \ReflectionProperty(get_class($class), $propertyName);
58     $reflection->setAccessible(true);
59
60     return $reflection;
61   }
62
63   /**
64    * @param null|array $methodReturnValues
65    * @param null|array $methodReturnValuesMap
66    *
67    * @return mixed
68    */
69   public function getContainerMock($methodReturnValues = null, $methodReturnValuesMap = null)
70   {
71     if (!is_array($methodReturnValues)) {
72       $methodReturnValues = [];
73     }
74
75     if (!is_array($methodReturnValuesMap)) {
76       $methodReturnValuesMap = [];
77     }
78
79     $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->setMethods(array_merge(array_keys($methodReturnValues), array_keys($methodReturnValuesMap)))->getMock();
80
81     foreach ($methodReturnValues as $methodName => $returnValue) {
82       $container->method($methodName)->will($this->returnValue($returnValue));
83     }
84
85     foreach ($methodReturnValuesMap as $methodName => $returnValueMap) {
86       $container->method($methodName)->will($this->returnValueMap($returnValueMap));
87     }
88
89     return $container;
90   }
91
92   /**
93    * @param string $string
94    * @return bool
95    */
96   public function containsHtml($string)
97   {
98     if ($string != strip_tags($string)) {
99       return true;
100     }
101
102     return false;
103   }
104
105   protected function collectServices($defaultServiceContainers, $testSpecificServices)
106   {
107     $serviceContainers = $defaultServiceContainers;
108
109     if (!empty($testSpecificServices)) {
110       $testSpecificServiceKeys = $this->extractTestSpecificContainerKeys($testSpecificServices);
111       $uniqueDefaultServiceContainers = $this->collectUndefinedServicesByDefaultStack($defaultServiceContainers, $testSpecificServiceKeys);
112       $serviceContainers = array_merge($uniqueDefaultServiceContainers, $testSpecificServices);
113     }
114
115     return $serviceContainers;
116   }
117
118   protected function extractTestSpecificContainerKeys($testSpecificServices)
119   {
120     foreach ($testSpecificServices as $testSpecificService) {
121       $testSpecificServiceKeys[] = $testSpecificService['0'];
122     }
123
124     return $testSpecificServiceKeys;
125   }
126
127   protected function collectUndefinedServicesByDefaultStack($defaultServiceContainers, $testSpecificServiceKeys)
128   {
129     foreach ($defaultServiceContainers as $serviceContainer) {
130       if (!in_array($serviceContainer['0'], $testSpecificServiceKeys)) {
131         $uniqueServiceContainers[] = $serviceContainer;
132       }
133     }
134
135     return $uniqueServiceContainers;
136   }
137
138 }