public function testGetByClass() { $repository = new CheckRepository(); $repository->registerCheck($this->createMock(CheckInterface::class)); $check = $this->createMock(CheckInterface::class); $repository->registerCheck($check); $this->assertSame($check, $repository->getByClass(get_class($check))); }
/** * @param string $requiredCheck * @throws InvalidArgumentException */ public function requireListenableCheck($requiredCheck) { if (!$this->checkRepository->has($requiredCheck)) { throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck)); } $check = $this->checkRepository->getByClass($requiredCheck); if (!$check instanceof ListenableCheckInterface) { throw new InvalidArgumentException(sprintf('Check: "%s" is not a listenable check', $requiredCheck)); } $check->attach($this->eventDispatcher); }
/** * Queue a specific check to be run when the exercise is verified. When the exercise is verified * the check specified as the first argument will also be executed. Throws an `InvalidArgumentException` * if the check does not exist in the `CheckRepository`. * * @param string $requiredCheck The name of the required check. * @throws InvalidArgumentException If the check does not exist. */ public function requireCheck($requiredCheck) { if (!$this->checkRepository->has($requiredCheck)) { throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck)); } $check = $this->checkRepository->getByClass($requiredCheck); if ($check instanceof SimpleCheckInterface) { switch ($check->getPosition()) { case SimpleCheckInterface::CHECK_BEFORE: $this->checksToRunBefore[] = $check; break; case SimpleCheckInterface::CHECK_AFTER: $this->checksToRunAfter[] = $check; break; default: throw InvalidArgumentException::notValidParameter('position', [SimpleCheckInterface::CHECK_BEFORE, SimpleCheckInterface::CHECK_AFTER], $check->getPosition()); } return; } if (!$check instanceof ListenableCheckInterface) { throw new InvalidArgumentException(sprintf('Check: "%s" is not a listenable check', $requiredCheck)); } $check->attach($this->eventDispatcher); }
public function testWhenBeforeChecksFailTheyReturnImmediately() { $this->createExercise(); $this->check->expects($this->once())->method('check')->will($this->returnValue(new Failure('Failure', 'nope'))); $doNotRunMe = $this->getMock(SimpleCheckInterface::class, [], [], 'DoNotRunMeCheck'); $doNotRunMe->expects($this->never())->method('check'); $this->check->expects($this->once())->method('canRun')->with($this->exerciseType)->will($this->returnValue(true)); $this->check->expects($this->once())->method('getExerciseInterface')->will($this->returnValue(ExerciseInterface::class)); $doNotRunMe->expects($this->once())->method('canRun')->with($this->exerciseType)->will($this->returnValue(true)); $doNotRunMe->expects($this->once())->method('getExerciseInterface')->will($this->returnValue(ExerciseInterface::class)); $this->checkRepository->registerCheck($doNotRunMe); $this->exerciseDispatcher->requireCheck(get_class($this->check), ExerciseDispatcher::CHECK_BEFORE); $this->exerciseDispatcher->requireCheck(get_class($doNotRunMe), ExerciseDispatcher::CHECK_BEFORE); $result = $this->exerciseDispatcher->verify($this->exercise, $this->file); $this->assertInstanceOf(ResultAggregator::class, $result); $this->assertFalse($result->isSuccessful()); }
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->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); })); $this->checkRepository->registerCheck($this->check); $results = new ResultAggregator(); $eventDispatcher = new EventDispatcher($results); $dispatcher = new ExerciseDispatcher($this->getRunnerManager($this->exercise, $eventDispatcher), $results, $eventDispatcher, $this->checkRepository); $dispatcher->verify($this->exercise, new Input('app', ['program' => __DIR__ . '/../res/database/user-solution-alter-db.php'])); }