/** @test */ public function it_should_generate_a_random_string_of_given_length() { $this->assertEquals(10, strlen($this->sut->generate(10))); $this->assertEquals(18, strlen($this->sut->generate(18))); $this->assertEquals(35, strlen($this->sut->generate(35))); $this->assertEquals(40, strlen($this->sut->generate(40))); }
private function regenerateSpeakerPhotoPath($speaker) { // If speaker photo does not exist, null it out and return. if (!$this->fileExists($speaker['photo_path'])) { echo "[info] {$speaker['name']}'s photo was not found in file system. Removing record of it from profile." . PHP_EOL; $this->execute("UPDATE users SET photo_path = '' WHERE id = {$speaker['id']}"); return; } // Need to guess extension. Cannot trust current file extensions. $file = new File(__DIR__ . '/../web/uploads/' . $speaker['photo_path']); $extension = $file->guessExtension(); // Otherwise, generate a new filename. $generator = new PseudoRandomStringGenerator(new Factory()); $newFileName = $generator->generate(40) . '.' . $extension; $oldFilePath = __DIR__ . '/../web/uploads/' . $speaker['photo_path']; $newFilePath = __DIR__ . '/../web/uploads/' . $newFileName; // If photo name is changed in file system, update record in database. if (rename($oldFilePath, $newFilePath)) { try { $this->execute("UPDATE users SET photo_path = '{$newFileName}' WHERE id = '{$speaker['id']}'"); echo "[info] Regenerated photo path for {$speaker['name']}." . PHP_EOL; } catch (\Exception $e) { // If update fails for any reason, revert filename in file system. rename($newFilePath, $oldFilePath); } } }