Example #1
0
 public function testVerifyReturnsFailureIfSolutionOutputDoesNotMatchUserOutput()
 {
     $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php'));
     $this->exercise->expects($this->once())->method('getSolution')->will($this->returnValue($solution));
     $this->exercise->expects($this->once())->method('getArgs')->will($this->returnValue([1, 2, 3]));
     $failure = $this->runner->verify(__DIR__ . '/../res/cli/user-wrong.php');
     $this->assertInstanceOf(StdOutFailure::class, $failure);
     $this->assertEquals('6', $failure->getExpectedOutput());
     $this->assertEquals('10', $failure->getActualOutput());
 }
 public function testGetters()
 {
     $tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName());
     $filePath = sprintf('%s/test.file', $tempPath);
     @mkdir($tempPath, 0775, true);
     touch($filePath);
     $solution = SingleFileSolution::fromFile($filePath);
     $this->assertSame($filePath, $solution->getEntryPoint());
     $this->assertSame($tempPath, $solution->getBaseDirectory());
     $this->assertFalse($solution->hasComposerFile());
     $this->assertInternalType('array', $solution->getFiles());
     $this->assertCount(1, $solution->getFiles());
     $this->assertSame($filePath, $solution->getFiles()[0]->__toString());
     unlink($filePath);
     rmdir($tempPath);
 }
 public function testAlteringDatabaseInSolutionDoesNotEffectDatabaseInUserSolution()
 {
     $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution-alter-db.php'));
     $this->exercise->expects($this->once())->method('getSolution')->will($this->returnValue($solution));
     $this->exercise->expects($this->any())->method('getArgs')->will($this->returnValue([]));
     $this->exercise->expects($this->atLeastOnce())->method('getType')->will($this->returnValue(ExerciseType::CLI()));
     $this->exercise->expects($this->once())->method('configure')->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
         $dispatcher->requireCheck(DatabaseCheck::class);
     }));
     $this->exercise->expects($this->once())->method('seed')->with($this->isInstanceOf(PDO::class))->will($this->returnCallback(function (PDO $db) {
         $db->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)');
         $stmt = $db->prepare('INSERT into users (name, age, gender) VALUES (:name, :age, :gender)');
         $stmt->execute([':name' => 'Jimi Hendrix', ':age' => 27, ':gender' => 'Male']);
     }));
     $this->exercise->expects($this->once())->method('verify')->with($this->isInstanceOf(PDO::class))->will($this->returnCallback(function (PDO $db) {
         $users = $db->query('SELECT * FROM users');
         $users = $users->fetchAll(PDO::FETCH_ASSOC);
         $this->assertEquals([['id' => 1, 'name' => 'Jimi Hendrix', 'age' => '27', 'gender' => 'Male'], ['id' => 2, 'name' => 'Kurt Cobain', 'age' => '27', 'gender' => 'Male']], $users);
     }));
     $results = new ResultAggregator();
     $eventDispatcher = new EventDispatcher($results);
     $checkRepository = new CheckRepository([$this->check]);
     $dispatcher = new ExerciseDispatcher(new RunnerFactory(), $results, $eventDispatcher, $checkRepository);
     $dispatcher->verify($this->exercise, __DIR__ . '/../res/database/user-solution-alter-db.php');
 }
 public function testVerifyReturnsFailureIfSolutionOutputHeadersDoesNotMatchUserOutputHeaders()
 {
     $solution = SingleFileSolution::fromFile(__DIR__ . '/../res/cgi/get-solution-header.php');
     $this->exercise->expects($this->once())->method('getSolution')->will($this->returnValue($solution));
     $request = (new Request())->withMethod('GET')->withUri(new Uri('http://some.site?number=5'));
     $this->exercise->expects($this->once())->method('getRequests')->will($this->returnValue([$request]));
     $failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-user-header-wrong.php'));
     $this->assertInstanceOf(CgiOutResult::class, $failure);
     $this->assertCount(1, $failure);
     $result = iterator_to_array($failure)[0];
     $this->assertInstanceOf(CgiOutRequestFailure::class, $result);
     $this->assertSame($result->getExpectedOutput(), $result->getActualOutput());
     $this->assertEquals(['Pragma' => 'cache', 'Content-type' => 'text/html; charset=UTF-8'], $result->getExpectedHeaders());
     $this->assertEquals(['Pragma' => 'no-cache', 'Content-type' => 'text/html; charset=UTF-8'], $result->getActualHeaders());
 }
 public function testVerifyReturnsFailureIfSolutionOutputDoesNotMatchUserOutput()
 {
     $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php'));
     $this->exercise->expects($this->once())->method('getSolution')->will($this->returnValue($solution));
     $this->exercise->expects($this->once())->method('getArgs')->will($this->returnValue([[1, 2, 3]]));
     $failure = $this->runner->verify(new Input('app', ['program' => __DIR__ . '/../res/cli/user-wrong.php']));
     $this->assertInstanceOf(CLiResult::class, $failure);
     $this->assertCount(1, $failure);
     $result = iterator_to_array($failure)[0];
     $this->assertInstanceOf(RequestFailure::class, $result);
     $this->assertEquals('6', $result->getExpectedOutput());
     $this->assertEquals('10', $result->getActualOutput());
 }
Example #6
0
 /**
  * @return SolutionInterface
  */
 public function getSolution()
 {
     return SingleFileSolution::fromFile(realpath(sprintf('%s/../../exercises/%s/solution/solution.php', dirname((new ReflectionClass(static::class))->getFileName()), $this->normaliseName($this->getName()))));
 }