Skip to content

Commit

Permalink
[VarDumper] chore: removed chai dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Apr 23, 2023
1 parent 190ca60 commit 8cca635
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 94 deletions.
168 changes: 80 additions & 88 deletions test/Constraints/CollectionValidatorTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Assert = Jymfony.Component.Testing.Framework.Assert;
const Collection = Jymfony.Component.Validator.Constraints.Collection;
const CollectionValidator = Jymfony.Component.Validator.Constraints.CollectionValidator;
const Constraint = Jymfony.Component.Validator.Constraint;
Expand All @@ -7,57 +8,48 @@ const ExecutionContext = Jymfony.Component.Validator.Context.ExecutionContext;
const Email = Jymfony.Component.Validator.Constraints.Email;
const NotNull = Jymfony.Component.Validator.Constraints.NotNull;
const Optional = Jymfony.Component.Validator.Constraints.Optional;
const Prophet = Jymfony.Component.Testing.Prophet;
const Range = Jymfony.Component.Validator.Constraints.Range;
const Required = Jymfony.Component.Validator.Constraints.Required;
const TestCase = Jymfony.Component.Testing.Framework.TestCase;
const UnexpectedValueException = Jymfony.Component.Validator.Exception.UnexpectedValueException;
const Valid = Jymfony.Component.Validator.Constraints.Valid;
const { expect } = require('chai');

describe('[Validator] Constraints.CollectionValidator', function () {
/**
* @type {Jymfony.Component.Testing.Prophet}
*/
let prophet;

beforeEach(() => {
prophet = new Prophet();
});

afterEach(() => {
if ('failed' === this.ctx.currentTest.state) {
return;
}

prophet.checkPredictions();
});

it ('should reject invalid fields options', async () => {
await expect(() => new Collection({ fields: 'foo' }))
.to.throw(ConstraintDefinitionException);
});

it ('should reject invalid options', async () => {
await expect(() => new Collection({ foo: 'bar' }))
.to.throw(ConstraintDefinitionException);
});

it ('should reject valid constraints', async () => {
await expect(() => new Collection({ foo: new Valid() }))
.to.throw(ConstraintDefinitionException);
});

it ('should reject valid constraint within optional', async () => {
await expect(() => new Collection({ foo: new Optional(new Valid()) }))
.to.throw(ConstraintDefinitionException);
});

it ('should reject valid constraint within required', async () => {
await expect(() => new Collection({ foo: new Required(new Valid()) }))
.to.throw(ConstraintDefinitionException);
});

it ('should accept optional constraint as one element', async () => {
export default class CollectionValidatorTest extends TestCase {
get testCaseName() {
return '[Validator] ' + super.testCaseName;
}

testShouldRejectInvalidFieldsOptions() {
this.expectException(ConstraintDefinitionException);
new Collection({ fields: 'foo' });
}

testShouldRejectInvalidOptions() {
this.expectException(ConstraintDefinitionException);
this.expectExceptionMessage('The value "bar" is not an instance of Constraint in constraint "Jymfony.Component.Validator.Constraints.Required".');
new Collection({ foo: 'bar' });
}

testShouldRejectValidConstraint() {
this.expectException(ConstraintDefinitionException);
this.expectExceptionMessage('The constraint Valid cannot be nested inside constraint "Jymfony.Component.Validator.Constraints.Required". You can only declare the Valid constraint directly on a field or method.');
new Collection({ foo: new Valid() });
}

testShouldRejectValidConstraintWithinOptional() {
this.expectException(ConstraintDefinitionException);
this.expectExceptionMessage('The constraint Valid cannot be nested inside constraint "Jymfony.Component.Validator.Constraints.Optional". You can only declare the Valid constraint directly on a field or method.');
new Collection({ foo: new Optional(new Valid()) });
}

testShouldRejectValidConstraintWithinRequired() {
this.expectException(ConstraintDefinitionException);
this.expectExceptionMessage('The constraint Valid cannot be nested inside constraint "Jymfony.Component.Validator.Constraints.Required". You can only declare the Valid constraint directly on a field or method.');
new Collection({ foo: new Required(new Valid()) });
}

testShouldAcceptOptionalConstraintAsOneElement() {
const constraint1 = new Collection({
fields: {
alternate_email: [
Expand All @@ -72,10 +64,10 @@ describe('[Validator] Constraints.CollectionValidator', function () {
},
});

await expect(constraint1).to.dump.as(constraint2);
});
__self.assertEquals(constraint2, constraint1);
}

it ('should accept required constraint as one element', async () => {
testShouldAcceptRequiredConstraintAsOneElement() {
const constraint1 = new Collection({
fields: {
alternate_email: [
Expand All @@ -90,38 +82,38 @@ describe('[Validator] Constraints.CollectionValidator', function () {
},
});

await expect(constraint1).to.dump.as(constraint2);
});
__self.assertEquals(constraint2, constraint1);
}

it ('should not raise any violation on null value', async () => {
async testShouldNotRaiseAnyViolationOnNullValue() {
await expect(null).to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({ fields: {
foo: [ new Range({ min: 5 }) ],
} })).and.raise.no.violations();
});
}

it ('should not raise any violation on undefined', async () => {
async testShouldNotRaiseAnyViolationOnUndefined() {
await expect(undefined).to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({ fields: {
foo: [ new Range({ min: 5 }) ],
} })).and.raise.no.violations();
});
}

it ('should use fields as default option', async () => {
async testShouldUseFieldsAsDefaultOption() {
let initializeCalled = 0;
const inner = prophet.prophesize(Constraint);
const inner = this.prophesize(Constraint);

const innerValidator = class extends implementationOf(ConstraintValidatorInterface) {
initialize(context) {
expect(context).to.be.instanceOf(ExecutionContext);
expect(context.getPropertyPath()).to.be.equal('foo');
Assert.assertInstanceOf(ExecutionContext, context);
Assert.assertEquals('foo', context.getPropertyPath());

++initializeCalled;
}

validate(value, constraint) {
expect(value).to.be.equal('foobar');
expect(constraint).to.be.equal(inner.reveal());
Assert.assertEquals('foobar', value);
Assert.assertEquals(inner.reveal(), constraint);
}
};

Expand All @@ -132,17 +124,17 @@ describe('[Validator] Constraints.CollectionValidator', function () {
.with.constraint(new Collection({ foo: inner.reveal() }))
.and.raise.no.violations();

expect(initializeCalled).to.be.equal(1, 'initialized not called');
});
__self.assertEquals(1, initializeCalled, 'initialized not called');
}

it ('should throw if value is not a simple object', async () => {
async testShouldThrowIfValueIsNotASimpleObject() {
await expect('foobar').to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({ foo: new Range({ min: 4 }) }))
.and.throw(UnexpectedValueException);
});
}

it ('should validate a single inner constraint', async () => {
const inner = prophet.prophesize(Constraint);
async testShouldValidateASingleInnerConstraint() {
const inner = this.prophesize(Constraint);

const validateCalls = [];
const innerValidator = class extends implementationOf(ConstraintValidatorInterface) {
Expand All @@ -161,15 +153,15 @@ describe('[Validator] Constraints.CollectionValidator', function () {
.with.constraint(new Collection({ foo: inner.reveal(), bar: inner.reveal() }))
.and.raise.no.violations();

expect(validateCalls).to.be.deep.equal([
__self.assertEquals([
[ 3, inner.reveal() ],
[ 5, inner.reveal() ],
]);
});
], validateCalls);
}

it ('should walk multiple constraints', async () => {
const inner1 = prophet.prophesize(Constraint);
const inner2 = prophet.prophesize(Constraint);
async testShouldWalkMultipleConstraints() {
const inner1 = this.prophesize(Constraint);
const inner2 = this.prophesize(Constraint);

const validate1Calls = [];
const validate2Calls = [];
Expand Down Expand Up @@ -207,18 +199,18 @@ describe('[Validator] Constraints.CollectionValidator', function () {
],
})).and.raise.no.violations();

expect(validate1Calls).to.be.deep.equal([
__self.assertEquals([
[ 3, inner1.reveal() ],
[ 5, inner1.reveal() ],
]);
], validate1Calls);

expect(validate2Calls).to.be.deep.equal([
__self.assertEquals([
[ 3, inner2.reveal() ],
[ 5, inner2.reveal() ],
]);
});
], validate2Calls);
}

it ('should raise violations on extra fields', async () => {
async testShouldRaiseViolationsOnExtraFields() {
const obj = { foo: 3, bar: 5 };

await expect(obj).to.be.validated.by(CollectionValidator)
Expand All @@ -234,19 +226,19 @@ describe('[Validator] Constraints.CollectionValidator', function () {
propertyPath: 'bar',
invalidValue: 5,
} ]);
});
}

it ('should not raise violations if extra fields are allowed', async () => {
async testShouldNotRaiseViolationsIfExtraFieldsAreAllowed() {
const obj = { foo: 3, bar: 5 };

await expect(obj).to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({
fields: { foo: new NotNull() },
allowExtraFields: true,
})).and.raise.no.violations();
});
}

it ('should raise violations if required field is missing', async () => {
async testShouldRaiseViolationsIfRequiredFieldIsMissing() {
await expect({}).to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({
fields: { foo: new NotNull() },
Expand All @@ -260,17 +252,17 @@ describe('[Validator] Constraints.CollectionValidator', function () {
propertyPath: 'foo',
invalidValue: undefined,
} ]);
});
}

it ('should not raise violations if missing fields are allowed', async () => {
async testShouldNotRaiseViolationsIfMissingFieldsAreAllowed() {
await expect({}).to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({
fields: { foo: new NotNull() },
allowMissingFields: true,
})).and.raise.no.violations();
});
}

it ('should work with optional field', async () => {
async testShouldWorkWithOptionalField() {
await expect({ foo: undefined }).to.be.validated.by(CollectionValidator)
.with.constraint(new Collection({
fields: { foo: new Optional() },
Expand All @@ -282,5 +274,5 @@ describe('[Validator] Constraints.CollectionValidator', function () {
fields: { foo: new Optional() },
allowMissingFields: true,
})).and.raise.no.violations();
});
});
}
}
13 changes: 7 additions & 6 deletions test/Mapping/Loader/JsonFileLoaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const Constraints = Jymfony.Component.Validator.Constraints;
const Fixtures = Jymfony.Component.Validator.Fixtures;
const JsonFileLoader = Jymfony.Component.Validator.Mapping.Loader.JsonFileLoader;
const TestCase = Jymfony.Component.Testing.Framework.TestCase;
const { expect } = require('chai');
const VarDumperTestTrait = Jymfony.Component.VarDumper.Test.VarDumperTestTrait;

export default class JsonFileLoaderTest extends TestCase {
export default class JsonFileLoaderTest extends mix(TestCase, VarDumperTestTrait) {
get testCaseName() {
return '[Validator] ' + super.testCaseName;
}
Expand All @@ -22,7 +22,7 @@ export default class JsonFileLoaderTest extends TestCase {
const r = (new ReflectionClass(loader)).getField('_classes');
r.accessible = true;

expect(r.getValue(loader)).to.be.deep.equal({});
__self.assertEquals({}, r.getValue(loader));
}

provideEmptyMapping() {
Expand All @@ -38,7 +38,8 @@ export default class JsonFileLoaderTest extends TestCase {
const loader = this._createLoader(file);
const metadata = new ClassMetadata(new ReflectionClass(Fixtures.Entity));

expect(() => loader.loadClassMetadata(metadata)).to.throw(InvalidArgumentException);
this.expectException(InvalidArgumentException);
loader.loadClassMetadata(metadata);
}

* provideInvalidFiles() {
Expand Down Expand Up @@ -84,7 +85,7 @@ export default class JsonFileLoaderTest extends TestCase {
expected.addGetterConstraint('valid', new Constraints.IsTrue());
expected.addGetterConstraint('permissions', new Constraints.IsTrue());

expect(metadata).to.be.dump.as(expected);
this.assertDumpEquals(expected, metadata);
}

testLoadGroupSequenceProvider() {
Expand All @@ -96,7 +97,7 @@ export default class JsonFileLoaderTest extends TestCase {
const expected = new ClassMetadata(new ReflectionClass(Fixtures.GroupSequenceProviderEntity));
expected.setGroupSequenceProvider(true);

expect(metadata).to.be.dump.as(expected);
this.assertDumpEquals(expected, metadata);
}

_createLoader(file) {
Expand Down

0 comments on commit 8cca635

Please sign in to comment.