Add PHP tests and fix bug in PHP builtin driver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
diff --git a/src/userdiff.h b/src/userdiff.h
index 2257035..9318b54 100644
--- a/src/userdiff.h
+++ b/src/userdiff.h
@@ -186,7 +186,7 @@ PATTERNS("csharp",
"|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
PATTERNS("php",
- "^[ \t]*((public|private|protected|static|final)[ \t]+)*((class|function)[ \t].*)$",
+ "^[ \t]*(((public|private|protected|static|final)[ \t]+)*((class|function)[ \t].*))$",
/* -- */
"[a-zA-Z_][a-zA-Z0-9_]*"
"|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
diff --git a/tests/resources/userdiff/.gitted/index b/tests/resources/userdiff/.gitted/index
index df041cf..b69d5cf 100644
Binary files a/tests/resources/userdiff/.gitted/index and b/tests/resources/userdiff/.gitted/index differ
diff --git a/tests/resources/userdiff/.gitted/info/refs b/tests/resources/userdiff/.gitted/info/refs
index 261695f..b074314 100644
--- a/tests/resources/userdiff/.gitted/info/refs
+++ b/tests/resources/userdiff/.gitted/info/refs
@@ -1 +1 @@
-802734c801bffc466d69876bc4814f747aaac4fe refs/heads/master
+60e3f7b244a5305e2c9fa4ef0e897f3b14f3b8dd refs/heads/master
diff --git a/tests/resources/userdiff/.gitted/objects/info/packs b/tests/resources/userdiff/.gitted/objects/info/packs
index 6970fd7..0c5fc2a 100644
--- a/tests/resources/userdiff/.gitted/objects/info/packs
+++ b/tests/resources/userdiff/.gitted/objects/info/packs
@@ -1,2 +1,2 @@
-P pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack
+P pack-1652578900ac63564f2a24b9714529821276ceb9.pack
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.idx b/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.idx
deleted file mode 100644
index 6d723f5..0000000
Binary files a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.idx and /dev/null differ
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack b/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack
deleted file mode 100644
index 5b263e2..0000000
Binary files a/tests/resources/userdiff/.gitted/objects/pack/pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack and /dev/null differ
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.idx b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.idx
new file mode 100644
index 0000000..6f4381c
Binary files /dev/null and b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.idx differ
diff --git a/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.pack b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.pack
new file mode 100644
index 0000000..39bd1d8
Binary files /dev/null and b/tests/resources/userdiff/.gitted/objects/pack/pack-1652578900ac63564f2a24b9714529821276ceb9.pack differ
diff --git a/tests/resources/userdiff/.gitted/packed-refs b/tests/resources/userdiff/.gitted/packed-refs
index 6d24ee4..802f67c 100644
--- a/tests/resources/userdiff/.gitted/packed-refs
+++ b/tests/resources/userdiff/.gitted/packed-refs
@@ -1,2 +1,2 @@
# pack-refs with: peeled fully-peeled
-802734c801bffc466d69876bc4814f747aaac4fe refs/heads/master
+60e3f7b244a5305e2c9fa4ef0e897f3b14f3b8dd refs/heads/master
diff --git a/tests/resources/userdiff/.gitted/refs/dummy-marker.txt b/tests/resources/userdiff/.gitted/refs/dummy-marker.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/resources/userdiff/.gitted/refs/dummy-marker.txt
diff --git a/tests/resources/userdiff/after/file.php b/tests/resources/userdiff/after/file.php
new file mode 100644
index 0000000..967d646
--- /dev/null
+++ b/tests/resources/userdiff/after/file.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Faker;
+
+/**
+ * Proxy for other generators, to return only unique values. Works with
+ * Faker\Generator\Base->unique()
+ */
+class UniqueGenerator
+{
+ protected $generator;
+ protected $maxRetries;
+ protected $moreStuff;
+ protected $uniques = array();
+
+ public function __construct(Generator $generator, $maxRetries)
+ {
+ $this->generator = $generator;
+ $this->maxRetries = $maxRetries + 1;
+ }
+
+ /**
+ * Catch and proxy all generator calls but return only unique values
+ */
+ public function __get($attribute)
+ {
+ return $this->__call($attribute, array());
+ }
+
+ /**
+ * Catch and proxy all generator calls with arguments but return only unique values
+ */
+ public function __call($name, $arguments)
+ {
+ $i = 0;
+ if (!isset($this->uniques[$name])) {
+ $this->uniques[$name] = array();
+ }
+ do {
+ $res = call_user_func_array(array($this->generator, $name), $arguments);
+ $i++;
+ if ($i >= $this->maxRetries) {
+ throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
+ }
+ } while (in_array($res, $this->uniques[$name]));
+ $this->uniques[$name][]= $res;
+
+ return $res;
+ }
+}
diff --git a/tests/resources/userdiff/before/file.php b/tests/resources/userdiff/before/file.php
new file mode 100644
index 0000000..63250ad
--- /dev/null
+++ b/tests/resources/userdiff/before/file.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Faker;
+
+/**
+ * Proxy for other generators, to return only unique values. Works with
+ * Faker\Generator\Base->unique()
+ */
+class UniqueGenerator
+{
+ protected $generator;
+ protected $maxRetries;
+ protected $uniques = array();
+
+ public function __construct(Generator $generator, $maxRetries)
+ {
+ $this->generator = $generator;
+ $this->maxRetries = $maxRetries;
+ }
+
+ /**
+ * Catch and proxy all generator calls but return only unique values
+ */
+ public function __get($attribute)
+ {
+ return $this->__call($attribute, array());
+ }
+
+ /**
+ * Catch and proxy all generator calls with arguments but return only unique values
+ */
+ public function __call($name, $arguments)
+ {
+ if (!isset($this->uniques[$name])) {
+ $this->uniques[$name] = array();
+ }
+ $i = 0;
+ do {
+ $res = call_user_func_array(array($this->generator, $name), $arguments);
+ $i++;
+ if ($i > $this->maxRetries) {
+ throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
+ }
+ } while (in_array($res, $this->uniques[$name]));
+ $this->uniques[$name][]= $res;
+
+ return $res;
+ }
+}
diff --git a/tests/resources/userdiff/expected/driver/diff.php b/tests/resources/userdiff/expected/driver/diff.php
new file mode 100644
index 0000000..9711b5b
--- /dev/null
+++ b/tests/resources/userdiff/expected/driver/diff.php
@@ -0,0 +1,26 @@
+diff --git a/files/file.php b/files/file.php
+index 63250ad..967d646 100644
+--- a/files/file.php
++++ b/files/file.php
+@@ -12,2 +12,3 @@ class UniqueGenerator
+ protected $maxRetries;
++ protected $moreStuff;
+ protected $uniques = array();
+@@ -17,3 +18,3 @@ public function __construct(Generator $generator, $maxRetries)
+ $this->generator = $generator;
+- $this->maxRetries = $maxRetries;
++ $this->maxRetries = $maxRetries + 1;
+ }
+@@ -33,10 +34,10 @@ public function __call($name, $arguments)
+ {
++ $i = 0;
+ if (!isset($this->uniques[$name])) {
+ $this->uniques[$name] = array();
+ }
+- $i = 0;
+ do {
+ $res = call_user_func_array(array($this->generator, $name), $arguments);
+ $i++;
+- if ($i > $this->maxRetries) {
++ if ($i >= $this->maxRetries) {
+ throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
diff --git a/tests/resources/userdiff/expected/nodriver/diff.php b/tests/resources/userdiff/expected/nodriver/diff.php
new file mode 100644
index 0000000..e77c094
--- /dev/null
+++ b/tests/resources/userdiff/expected/nodriver/diff.php
@@ -0,0 +1,26 @@
+diff --git a/files/file.php b/files/file.php
+index 63250ad..967d646 100644
+--- a/files/file.php
++++ b/files/file.php
+@@ -12,2 +12,3 @@ class UniqueGenerator
+ protected $maxRetries;
++ protected $moreStuff;
+ protected $uniques = array();
+@@ -17,3 +18,3 @@ class UniqueGenerator
+ $this->generator = $generator;
+- $this->maxRetries = $maxRetries;
++ $this->maxRetries = $maxRetries + 1;
+ }
+@@ -33,10 +34,10 @@ class UniqueGenerator
+ {
++ $i = 0;
+ if (!isset($this->uniques[$name])) {
+ $this->uniques[$name] = array();
+ }
+- $i = 0;
+ do {
+ $res = call_user_func_array(array($this->generator, $name), $arguments);
+ $i++;
+- if ($i > $this->maxRetries) {
++ if ($i >= $this->maxRetries) {
+ throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
diff --git a/tests/resources/userdiff/files/file.php b/tests/resources/userdiff/files/file.php
new file mode 100644
index 0000000..967d646
--- /dev/null
+++ b/tests/resources/userdiff/files/file.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Faker;
+
+/**
+ * Proxy for other generators, to return only unique values. Works with
+ * Faker\Generator\Base->unique()
+ */
+class UniqueGenerator
+{
+ protected $generator;
+ protected $maxRetries;
+ protected $moreStuff;
+ protected $uniques = array();
+
+ public function __construct(Generator $generator, $maxRetries)
+ {
+ $this->generator = $generator;
+ $this->maxRetries = $maxRetries + 1;
+ }
+
+ /**
+ * Catch and proxy all generator calls but return only unique values
+ */
+ public function __get($attribute)
+ {
+ return $this->__call($attribute, array());
+ }
+
+ /**
+ * Catch and proxy all generator calls with arguments but return only unique values
+ */
+ public function __call($name, $arguments)
+ {
+ $i = 0;
+ if (!isset($this->uniques[$name])) {
+ $this->uniques[$name] = array();
+ }
+ do {
+ $res = call_user_func_array(array($this->generator, $name), $arguments);
+ $i++;
+ if ($i >= $this->maxRetries) {
+ throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries));
+ }
+ } while (in_array($res, $this->uniques[$name]));
+ $this->uniques[$name][]= $res;
+
+ return $res;
+ }
+}