-
Notifications
You must be signed in to change notification settings - Fork 4
Create unit tests for app fixtures.php #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pfwd
merged 40 commits into
howToCodeWell:main
from
metinbaris:Create-unit-tests-for-AppFixtures.php
Oct 9, 2022
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
1edeae3
#90 Create AppFixturesTest
metinbaris 3fbaee6
#90 Update test func name
metinbaris 097aec5
#90 Ignore .vscode files
metinbaris 3500063
#90 Ignore .DS_Store files
metinbaris 198145b
#90 Add getDataSets test, update require_once
metinbaris 95c31f2
#90 Test Create Quiz
metinbaris 54849ec
#90 Test create question
metinbaris 1169412
#90 Add type declarations
metinbaris 54a208f
#90 Fix Answer entity iscorrect getter
metinbaris 4e3e625
#90 Test create answer
metinbaris dc4c794
#90 Test load function
metinbaris c0fb745
#90 Remove testLoad() integration test
metinbaris a51db2d
#90 Update access modifiers
metinbaris 383e038
#90 Remove invoking methods
metinbaris b04870b
#90 Update naming and spacing
metinbaris 73ad423
#90 Reduce function variables
metinbaris 052c81b
#90 Update AppFixturesTest
metinbaris 83b7138
#90 Remove func not needed
metinbaris b71c190
#90 Chunk load function
metinbaris ff76a27
Merge branch 'main' into Create-unit-tests-for-AppFixtures.php
metinbaris 39ce25d
#90 Add function returns and var types
metinbaris c5ae7dd
#90 Add php docblocks
metinbaris de2778b
#90 Update file require
metinbaris ef66dc5
#90 Add javascript-quiz to getFilePaths
metinbaris e5ddf19
#90 Fix naming
metinbaris 0a81aa2
#90 Update require_once to require
metinbaris 77880dd
#90 Add tests for chunked load function
metinbaris a9229c1
#90 Update readability
metinbaris 46668b9
#90 Update dockblocks for ObjectManager
metinbaris 759bba0
#90 Import Doctrine\Persistence\ObjectManager
metinbaris a9f9f02
#90 Add docblocks for properties
metinbaris 32295c9
#90 Import ReflectionClass
metinbaris 57a8dff
#90 Add file paths exists to tests
metinbaris 53ec1be
#90 Add dockblocks for invokeProperty function
metinbaris 6fb0c9c
#90 Update string definitions
metinbaris b90620d
#90 Fix docblock add func variable to match
metinbaris 3059f5a
#90 Add created quizzes to return
metinbaris 38f91d2
#90 Update var name
metinbaris 5cb4d82
#90 Add created questions to return
metinbaris 8411725
#90 Add created answers to return
metinbaris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
.idea | ||
.vscode | ||
.DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
api/tests/unit/config/fixtures/data_fixtures/AppFixturesTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\unit\config\fixtures\data_fixtures; | ||
|
||
use App\DataFixtures\AppFixtures; | ||
use App\Entity\Answer; | ||
use App\Entity\Question; | ||
use App\Entity\Quiz; | ||
use Doctrine\Persistence\ObjectManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
|
||
final class AppFixturesTest extends TestCase | ||
{ | ||
/** @var AppFixtures */ | ||
private $appFixtures; | ||
|
||
/** @var MockObject|ObjectManager */ | ||
private $objectManager; | ||
metinbaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
protected function setUp(): void | ||
{ | ||
$objectManager = $this->getMockBuilder(ObjectManager::class) | ||
->onlyMethods(['flush', 'persist']) | ||
->getMockForAbstractClass(); | ||
$this->objectManager = $objectManager; | ||
|
||
$appFixtures = new AppFixtures; | ||
$appFixtures = $this->invokeProperty($appFixtures, 'objectManager', $objectManager); | ||
$this->appFixtures = $appFixtures; | ||
} | ||
|
||
/** | ||
* @param object $object | ||
* @param string $propertyName | ||
* @param mixed $parameter | ||
*/ | ||
public function invokeProperty(object &$object, string $propertyName, mixed $parameter): object | ||
{ | ||
$reflection = new ReflectionClass(get_class($object)); | ||
$property = $reflection->getProperty($propertyName); | ||
$property->setAccessible(true); | ||
$property->setValue($object, $parameter); | ||
|
||
return $object; | ||
} | ||
|
||
public function testLoad() | ||
{ | ||
$loadFunction = $this->appFixtures->load($this->objectManager); | ||
|
||
self::assertNull($loadFunction); | ||
} | ||
|
||
public function testCreateQuizzes() | ||
{ | ||
$createdQuizzes = $this->appFixtures->createQuizzes(); | ||
|
||
self::assertIsArray($createdQuizzes); | ||
|
||
foreach ($createdQuizzes as $quiz) { | ||
self::assertInstanceOf(Quiz::class, $quiz); | ||
} | ||
} | ||
|
||
public function testCreateQuestions() | ||
{ | ||
$html = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes/html-quiz/quiz.php'; | ||
|
||
$quiz = new Quiz; | ||
$quiz->setTitle('Test title'); | ||
$quiz->setSlug('test-slug'); | ||
|
||
$createdQuestions = $this->appFixtures->createQuestions($html['questions'], $quiz); | ||
|
||
self::assertIsArray($createdQuestions); | ||
|
||
foreach ($createdQuestions as $question) { | ||
self::assertInstanceOf(Question::class, $question); | ||
} | ||
} | ||
|
||
public function testCreateAnswers() | ||
{ | ||
$question1 = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes/html-quiz/questions/question_1.php'; | ||
$question = new Question; | ||
$question->setContent('Test content'); | ||
|
||
$createdAnswers = $this->appFixtures->createAnswers($question1[0]['answers'], $question); | ||
|
||
self::assertIsArray($createdAnswers); | ||
|
||
foreach ($createdAnswers as $answer) { | ||
self::assertInstanceOf(Answer::class, $answer); | ||
} | ||
} | ||
|
||
public function testGetDataSets() | ||
{ | ||
$result = $this->appFixtures->getDataSets(); | ||
metinbaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self::assertIsArray($result); | ||
} | ||
|
||
public function testGetFilePathsMethod() | ||
{ | ||
$filePaths = $this->appFixtures->getFilePaths(); | ||
|
||
self::assertIsArray($filePaths); | ||
|
||
foreach ($filePaths as $filePath) { | ||
self::assertFileExists($filePath); | ||
} | ||
} | ||
|
||
public function testCreateQuiz() | ||
{ | ||
$data = [ | ||
'title' => 'Test Quiz Title', | ||
'slug' => 'test-quiz-title' | ||
]; | ||
|
||
$result = $this->appFixtures->createQuiz($data); | ||
|
||
self::assertInstanceOf(Quiz::class, $result); | ||
self::assertEquals("Test Quiz Title", $result->getTitle()); | ||
self::assertEquals("test-quiz-title", $result->getSlug()); | ||
} | ||
|
||
public function testCreateQuestion() | ||
{ | ||
$quiz = new Quiz; | ||
$quiz = $this->invokeProperty($quiz, 'id', 10); | ||
|
||
$data = [ | ||
'content' => 'Test quiz content', | ||
]; | ||
|
||
$result = $this->appFixtures->createQuestion($data, $quiz); | ||
|
||
self::assertInstanceOf(Question::class, $result); | ||
self::assertInstanceOf(Quiz::class, $result->getQuiz()); | ||
self::assertEquals(10, $result->getQuiz()->getId()); | ||
self::assertEquals("Test quiz content", $result->getContent()); | ||
} | ||
|
||
public function testCreateAnswer() | ||
{ | ||
$question = new Question; | ||
$question = $this->invokeProperty($question, 'id', 10); | ||
|
||
$data = [ | ||
'content' => 'Test answer content', | ||
'display_order' => 3, | ||
'is_correct' => false | ||
]; | ||
|
||
$result = $this->appFixtures->CreateAnswer($data, $question); | ||
|
||
self::assertInstanceOf(Answer::class, $result); | ||
self::assertEquals(10, $result->getQuestion()->getId()); | ||
self::assertEquals("Test answer content", $result->getContent()); | ||
self::assertEquals(3, $result->getDisplayOrder()); | ||
self::assertEquals(false, $result->getIsCorrect()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.