|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.actions.generation.generator; |
| 7 | + |
| 8 | +import com.intellij.openapi.project.Project; |
| 9 | +import com.intellij.psi.PsiFile; |
| 10 | +import com.magento.idea.magento2plugin.actions.generation.data.CrontabXmlData; |
| 11 | +import com.magento.idea.magento2plugin.magento.files.CrontabXmlTemplate; |
| 12 | + |
| 13 | +public class CrontabXmlGeneratorTest extends BaseGeneratorTestCase { |
| 14 | + private static final String EXPECTED_DIRECTORY = "src/app/code/Foo/Bar/etc"; |
| 15 | + private static final String MODULE_NAME = "Foo_Bar"; |
| 16 | + private static final String DEFAULT_CRON_GROUP = "default"; |
| 17 | + private static final String INDEX_CRON_GROUP = "index"; |
| 18 | + private static final String CRONJOB_INSTANCE_ONE = "Foo\\Bar\\Cron\\TestOne"; |
| 19 | + private static final String CRONJOB_NAME_ONE = "test_cron_job_one"; |
| 20 | + private static final String CRONJOB_INSTANCE_TWO = "Foo\\Bar\\Cron\\TestTwo"; |
| 21 | + private static final String CRONJOB_NAME_TWO = "test_cron_job_two"; |
| 22 | + private static final String CRONJOB_SCHEDULE = "* * * * *"; |
| 23 | + private static final String CRONJOB_SCHEDULE_CONFIG_PATH = "path/to/config"; |
| 24 | + |
| 25 | + /** |
| 26 | + * Test generating crontab with schedule. |
| 27 | + */ |
| 28 | + public void testGenerateCronTabXmlFileWithSchedule() { |
| 29 | + final String filePath = this.getFixturePath(CrontabXmlTemplate.FILE_NAME); |
| 30 | + final PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 31 | + final PsiFile cronJobFile = addCronJobToCronTabXml( |
| 32 | + DEFAULT_CRON_GROUP, |
| 33 | + CRONJOB_NAME_ONE, |
| 34 | + CRONJOB_INSTANCE_ONE, |
| 35 | + CRONJOB_SCHEDULE, |
| 36 | + null |
| 37 | + ); |
| 38 | + |
| 39 | + assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, cronJobFile); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test generating crontab with schedule config path. |
| 44 | + */ |
| 45 | + public void testGenerateCronTabXmlFileWithScheduleConfig() { |
| 46 | + final String filePath = this.getFixturePath(CrontabXmlTemplate.FILE_NAME); |
| 47 | + final PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 48 | + final PsiFile cronJobFile = addCronJobToCronTabXml( |
| 49 | + DEFAULT_CRON_GROUP, |
| 50 | + CRONJOB_NAME_TWO, |
| 51 | + CRONJOB_INSTANCE_TWO, |
| 52 | + null, |
| 53 | + CRONJOB_SCHEDULE_CONFIG_PATH |
| 54 | + ); |
| 55 | + |
| 56 | + assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, cronJobFile); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test adding two cronjobs to the crontab.xml with one cron groups. |
| 61 | + */ |
| 62 | + public void testAddTwoCronJobsToOneCronTab() { |
| 63 | + final String filePath = this.getFixturePath(CrontabXmlTemplate.FILE_NAME); |
| 64 | + final PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 65 | + addCronJobToCronTabXml( |
| 66 | + DEFAULT_CRON_GROUP, |
| 67 | + CRONJOB_NAME_ONE, |
| 68 | + CRONJOB_INSTANCE_ONE, |
| 69 | + CRONJOB_SCHEDULE, |
| 70 | + null |
| 71 | + ); |
| 72 | + final PsiFile cronJobFile = addCronJobToCronTabXml( |
| 73 | + DEFAULT_CRON_GROUP, |
| 74 | + CRONJOB_NAME_TWO, |
| 75 | + CRONJOB_INSTANCE_TWO, |
| 76 | + null, |
| 77 | + CRONJOB_SCHEDULE_CONFIG_PATH |
| 78 | + ); |
| 79 | + |
| 80 | + assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, cronJobFile); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test adding two cronjobs to the crontab.xml with different cron groups. |
| 85 | + */ |
| 86 | + public void testAddTwoCronJobsToDifferentCronTabs() { |
| 87 | + final String filePath = this.getFixturePath(CrontabXmlTemplate.FILE_NAME); |
| 88 | + final PsiFile expectedFile = myFixture.configureByFile(filePath); |
| 89 | + addCronJobToCronTabXml( |
| 90 | + DEFAULT_CRON_GROUP, |
| 91 | + CRONJOB_NAME_ONE, |
| 92 | + CRONJOB_INSTANCE_ONE, |
| 93 | + CRONJOB_SCHEDULE, |
| 94 | + null |
| 95 | + ); |
| 96 | + final PsiFile cronJobFile = addCronJobToCronTabXml( |
| 97 | + INDEX_CRON_GROUP, |
| 98 | + CRONJOB_NAME_TWO, |
| 99 | + CRONJOB_INSTANCE_TWO, |
| 100 | + null, |
| 101 | + CRONJOB_SCHEDULE_CONFIG_PATH |
| 102 | + ); |
| 103 | + |
| 104 | + assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, cronJobFile); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Add cronjob to crontab.xml. |
| 109 | + * |
| 110 | + * @param cronGroup Cron group name |
| 111 | + * @param cronjobName Cron job name |
| 112 | + * @param cronjobInstance Cron job instance FQN |
| 113 | + * @param cronjobSchedule Cron job schedule |
| 114 | + * @param cronjobScheduleConfigPath Cron job schedule config path |
| 115 | + * @return PsiFile |
| 116 | + */ |
| 117 | + private PsiFile addCronJobToCronTabXml( |
| 118 | + final String cronGroup, |
| 119 | + final String cronjobName, |
| 120 | + final String cronjobInstance, |
| 121 | + final String cronjobSchedule, |
| 122 | + final String cronjobScheduleConfigPath |
| 123 | + ) { |
| 124 | + final Project project = myFixture.getProject(); |
| 125 | + final CrontabXmlData crontabXmlData = new CrontabXmlData( |
| 126 | + MODULE_NAME, |
| 127 | + cronGroup, |
| 128 | + cronjobName, |
| 129 | + cronjobInstance, |
| 130 | + cronjobSchedule, |
| 131 | + cronjobScheduleConfigPath |
| 132 | + ); |
| 133 | + final CrontabXmlGenerator cronjobClassGenerator = new CrontabXmlGenerator( |
| 134 | + project, |
| 135 | + crontabXmlData |
| 136 | + ); |
| 137 | + |
| 138 | + return cronjobClassGenerator.generate("test"); |
| 139 | + } |
| 140 | +} |
0 commit comments